tabs_fab_demo.dart 3.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/material.dart';

class _Page {
  _Page({ this.label, this.colors, this.icon });

  final String label;
  final Map<int, Color> colors;
12
  final IconData icon;
13

14
  TabLabel get tabLabel => new TabLabel(text: label.toUpperCase());
15 16 17 18 19 20 21 22 23 24 25 26 27
  Color get labelColor => colors != null ? colors[300] : Colors.grey[300];
  bool get fabDefined => colors != null && icon != null;
  Color get fabColor => colors[400];
  Icon get fabIcon => new Icon(icon: icon);
  Key get fabKey => new ValueKey<Color>(fabColor);
}

const String _explanatoryText =
  "When the Scaffold's floating action button changes, the new button fades and "
  "turns into view. In this demo, changing tabs can cause the app to be rebuilt "
  "with a FloatingActionButton that the Scaffold distinguishes from the others "
  "by its key.";

28
class TabsFabDemo extends StatefulWidget {
29
  @override
30 31 32 33
  _TabsFabDemoState createState() => new _TabsFabDemoState();
}

class _TabsFabDemoState extends State<TabsFabDemo> {
Ian Hickson's avatar
Ian Hickson committed
34
  final GlobalKey<ScaffoldState> scaffoldKey = new GlobalKey<ScaffoldState>();
35
  final List<_Page> pages = <_Page>[
36 37
    new _Page(label: 'Blue', colors: Colors.indigo, icon: Icons.add),
    new _Page(label: 'Eco', colors: Colors.green, icon: Icons.create),
38
    new _Page(label: 'No'),
39 40
    new _Page(label: 'Teal', colors: Colors.teal, icon: Icons.add),
    new _Page(label: 'Red', colors: Colors.red, icon: Icons.create),
41 42 43
  ];
  _Page selectedPage;

44
  @override
45 46
  void initState() {
    super.initState();
47
    selectedPage = pages[0];
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
  }

  void _handleTabSelection(_Page page) {
    setState(() {
      selectedPage = page;
    });
  }

  void _showExplanatoryText() {
    scaffoldKey.currentState.showBottomSheet((BuildContext context) {
      return new Container(
        decoration: new BoxDecoration(
          border: new Border(top: new BorderSide(color: Theme.of(context).dividerColor))
        ),
        child: new Padding(
63
          padding: const EdgeInsets.all(32.0),
64
          child: new Text(_explanatoryText, style: Theme.of(context).textTheme.subhead)
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
        )
      );
    });
  }

  Widget buildTabView(_Page page) {
    return new Builder(
      builder: (BuildContext context) {
        final TextStyle textStyle = new TextStyle(
          color: page.labelColor,
          fontSize: 32.0,
          textAlign: TextAlign.center
        );

        return new Container(
          key: new ValueKey<String>(page.label),
81
          padding: const EdgeInsets.fromLTRB(48.0, 48.0, 48.0, 96.0),
82 83 84 85 86 87 88 89 90 91
          child: new Card(
            child: new Center(
              child: new Text(page.label, style: textStyle)
            )
          )
        );
      }
    );
  }

92
  @override
93 94
  Widget build(BuildContext context) {
    return new TabBarSelection<_Page>(
95
      values: pages,
96 97 98
      onChanged: _handleTabSelection,
      child: new Scaffold(
        key: scaffoldKey,
99
        appBar: new AppBar(
100
          title: new Text('FAB per tab'),
101
          tabBar: new TabBar<_Page>(
102
            labels: new Map<_Page, TabLabel>.fromIterable(pages, value: (_Page page) => page.tabLabel)
103 104 105 106
          )
        ),
        floatingActionButton: !selectedPage.fabDefined ? null : new FloatingActionButton(
          key: selectedPage.fabKey,
107
          tooltip: 'Show explanation',
108 109 110 111
          backgroundColor: selectedPage.fabColor,
          child: selectedPage.fabIcon,
          onPressed: _showExplanatoryText
        ),
112
        body: new TabBarView<_Page>(children: pages.map(buildTabView).toList())
113 114 115 116
      )
    );
  }
}