scrollable_tabs_demo.dart 3.21 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
// 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';

enum TabsDemoStyle {
  iconsAndText,
  iconsOnly,
  textOnly
}

class ScrollableTabsDemo extends StatefulWidget {
14 15
  static const String routeName = '/scrollable-tabs';

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
  @override
  ScrollableTabsDemoState createState() => new ScrollableTabsDemoState();
}

class ScrollableTabsDemoState extends State<ScrollableTabsDemo> {
  final List<IconData> icons = <IconData>[
    Icons.event,
    Icons.home,
    Icons.android,
    Icons.alarm,
    Icons.face,
    Icons.language,
  ];

  final Map<IconData, String> labels = <IconData, String>{
    Icons.event: 'EVENT',
    Icons.home: 'HOME',
    Icons.android: 'ANDROID',
    Icons.alarm: 'ALARM',
    Icons.face: 'FACE',
    Icons.language: 'LANGUAGE',
  };

  TabsDemoStyle _demoStyle = TabsDemoStyle.iconsAndText;

  void changeDemoStyle(TabsDemoStyle style) {
    setState(() {
      _demoStyle = style;
    });
  }

  @override
  Widget build(BuildContext context) {
    final Color iconColor = Theme.of(context).accentColor;
    return new TabBarSelection<IconData>(
      values: icons,
      child: new Scaffold(
        appBar: new AppBar(
54
          title: new Text('Scrollable tabs'),
55 56 57
          actions: <Widget>[
            new PopupMenuButton<TabsDemoStyle>(
              onSelected: changeDemoStyle,
58
              itemBuilder: (BuildContext context) => <PopupMenuItem<TabsDemoStyle>>[
59 60
                new PopupMenuItem<TabsDemoStyle>(
                  value: TabsDemoStyle.iconsAndText,
61
                  child: new Text('Icons and text')
62 63 64
                ),
                new PopupMenuItem<TabsDemoStyle>(
                  value: TabsDemoStyle.iconsOnly,
65
                  child: new Text('Icons only')
66 67 68
                ),
                new PopupMenuItem<TabsDemoStyle>(
                  value: TabsDemoStyle.textOnly,
69
                  child: new Text('Text only')
70 71 72 73
                ),
              ]
            )
          ],
74
          bottom: new TabBar<IconData>(
75 76 77 78 79 80
            isScrollable: true,
            labels: new Map<IconData, TabLabel>.fromIterable(
              icons,
              value: (IconData icon) {
                switch(_demoStyle) {
                  case TabsDemoStyle.iconsAndText:
Ian Hickson's avatar
Ian Hickson committed
81
                    return new TabLabel(text: labels[icon], icon: new Icon(icon));
82
                  case TabsDemoStyle.iconsOnly:
Ian Hickson's avatar
Ian Hickson committed
83
                    return new TabLabel(icon: new Icon(icon));
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
                  case TabsDemoStyle.textOnly:
                    return new TabLabel(text: labels[icon]);
                }
              }
            )
          )
        ),
        body: new TabBarView<IconData>(
          children: icons.map((IconData icon) {
            return new Container(
              key: new ObjectKey(icon),
              padding: const EdgeInsets.all(12.0),
              child:new Card(
                child: new Center(
                  child: new Icon(
Ian Hickson's avatar
Ian Hickson committed
99
                    icon,
100 101 102 103 104 105 106 107 108 109 110 111
                    color: iconColor,
                    size: 128.0
                  )
                )
              )
            );
          }).toList()
        )
      )
    );
  }
}