tabs_fab_demo.dart 3.65 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
  Color get labelColor => colors != null ? colors[300] : Colors.grey[300];
  bool get fabDefined => colors != null && icon != null;
  Color get fabColor => colors[400];
Ian Hickson's avatar
Ian Hickson committed
18
  Icon get fabIcon => new Icon(icon);
19 20 21 22 23 24 25 26 27
  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 30
  static const String routeName = '/tabs-fab';

31
  @override
32 33 34 35
  _TabsFabDemoState createState() => new _TabsFabDemoState();
}

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

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

  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(
65
          padding: const EdgeInsets.all(32.0),
66
          child: new Text(_explanatoryText, style: Theme.of(context).textTheme.subhead)
67 68 69 70 71 72 73 74 75 76
        )
      );
    });
  }

  Widget buildTabView(_Page page) {
    return new Builder(
      builder: (BuildContext context) {
        return new Container(
          key: new ValueKey<String>(page.label),
77
          padding: const EdgeInsets.fromLTRB(48.0, 48.0, 48.0, 96.0),
78 79
          child: new Card(
            child: new Center(
80 81 82 83 84 85 86
              child: new Text(page.label,
                style: new TextStyle(
                  color: page.labelColor,
                  fontSize: 32.0
                ),
                textAlign: TextAlign.center
              )
87 88 89 90 91 92 93
            )
          )
        );
      }
    );
  }

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