"Fossies" - the Fresh Open Source Software Archive

Member "flutter-1.22.4/dev/integration_tests/flutter_gallery/lib/demo/material/tabs_fab_demo.dart" (13 Nov 2020, 4689 Bytes) of package /linux/misc/flutter-1.22.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Dart source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 // Copyright 2014 The Flutter Authors. All rights reserved.
    2 // Use of this source code is governed by a BSD-style license that can be
    3 // found in the LICENSE file.
    4 
    5 import 'package:flutter/material.dart';
    6 
    7 import '../../gallery/demo.dart';
    8 
    9 const String _explanatoryText =
   10   "When the Scaffold's floating action button changes, the new button fades and "
   11   'turns into view. In this demo, changing tabs can cause the app to be rebuilt '
   12   'with a FloatingActionButton that the Scaffold distinguishes from the others '
   13   'by its key.';
   14 
   15 class _Page {
   16   _Page({ this.label, this.colors, this.icon });
   17 
   18   final String label;
   19   final MaterialColor colors;
   20   final IconData icon;
   21 
   22   Color get labelColor => colors != null ? colors.shade300 : Colors.grey.shade300;
   23   bool get fabDefined => colors != null && icon != null;
   24   Color get fabColor => colors.shade400;
   25   Icon get fabIcon => Icon(icon);
   26   Key get fabKey => ValueKey<Color>(fabColor);
   27 }
   28 
   29 final List<_Page> _allPages = <_Page>[
   30   _Page(label: 'Pink', colors: Colors.pink, icon: Icons.add),
   31   _Page(label: 'Eco', colors: Colors.green, icon: Icons.create),
   32   _Page(label: 'No'),
   33   _Page(label: 'Teal', colors: Colors.teal, icon: Icons.add),
   34   _Page(label: 'Red', colors: Colors.red, icon: Icons.create),
   35 ];
   36 
   37 class TabsFabDemo extends StatefulWidget {
   38   static const String routeName = '/material/tabs-fab';
   39 
   40   @override
   41   _TabsFabDemoState createState() => _TabsFabDemoState();
   42 }
   43 
   44 class _TabsFabDemoState extends State<TabsFabDemo> with SingleTickerProviderStateMixin {
   45   final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
   46 
   47   TabController _controller;
   48   _Page _selectedPage;
   49   bool _extendedButtons = false;
   50 
   51   @override
   52   void initState() {
   53     super.initState();
   54     _controller = TabController(vsync: this, length: _allPages.length);
   55     _controller.addListener(_handleTabSelection);
   56     _selectedPage = _allPages[0];
   57   }
   58 
   59   @override
   60   void dispose() {
   61     _controller.dispose();
   62     super.dispose();
   63   }
   64 
   65   void _handleTabSelection() {
   66     setState(() {
   67       _selectedPage = _allPages[_controller.index];
   68     });
   69   }
   70 
   71   void _showExplanatoryText() {
   72     _scaffoldKey.currentState.showBottomSheet<void>((BuildContext context) {
   73       return Container(
   74         decoration: BoxDecoration(
   75           border: Border(top: BorderSide(color: Theme.of(context).dividerColor))
   76         ),
   77         child: Padding(
   78           padding: const EdgeInsets.all(32.0),
   79           child: Text(_explanatoryText, style: Theme.of(context).textTheme.subtitle1),
   80         ),
   81       );
   82     });
   83   }
   84 
   85   Widget buildTabView(_Page page) {
   86     return Builder(
   87       builder: (BuildContext context) {
   88         return Container(
   89           key: ValueKey<String>(page.label),
   90           padding: const EdgeInsets.fromLTRB(48.0, 48.0, 48.0, 96.0),
   91           child: Card(
   92             child: Center(
   93               child: Text(page.label,
   94                 style: TextStyle(
   95                   color: page.labelColor,
   96                   fontSize: 32.0,
   97                 ),
   98                 textAlign: TextAlign.center,
   99               ),
  100             ),
  101           ),
  102         );
  103       }
  104     );
  105   }
  106 
  107   Widget buildFloatingActionButton(_Page page) {
  108     if (!page.fabDefined)
  109       return null;
  110 
  111     if (_extendedButtons) {
  112       return FloatingActionButton.extended(
  113         key: ValueKey<Key>(page.fabKey),
  114         tooltip: 'Show explanation',
  115         backgroundColor: page.fabColor,
  116         icon: page.fabIcon,
  117         label: Text(page.label.toUpperCase()),
  118         onPressed: _showExplanatoryText,
  119       );
  120     }
  121 
  122     return FloatingActionButton(
  123       key: page.fabKey,
  124       tooltip: 'Show explanation',
  125       backgroundColor: page.fabColor,
  126       child: page.fabIcon,
  127       onPressed: _showExplanatoryText,
  128     );
  129   }
  130 
  131   @override
  132   Widget build(BuildContext context) {
  133     return Scaffold(
  134       key: _scaffoldKey,
  135       appBar: AppBar(
  136         title: const Text('FAB per tab'),
  137         bottom: TabBar(
  138           controller: _controller,
  139           tabs: _allPages.map<Widget>((_Page page) => Tab(text: page.label.toUpperCase())).toList(),
  140         ),
  141         actions: <Widget>[
  142           MaterialDemoDocumentationButton(TabsFabDemo.routeName),
  143           IconButton(
  144             icon: const Icon(Icons.sentiment_very_satisfied, semanticLabel: 'Toggle extended buttons'),
  145             onPressed: () {
  146               setState(() {
  147                 _extendedButtons = !_extendedButtons;
  148               });
  149             },
  150           ),
  151         ],
  152       ),
  153       floatingActionButton: buildFloatingActionButton(_selectedPage),
  154       body: TabBarView(
  155         controller: _controller,
  156         children: _allPages.map<Widget>(buildTabView).toList(),
  157       ),
  158     );
  159   }
  160 }