icons_demo.dart 3.65 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
Hans Muller's avatar
Hans Muller committed
2 3 4 5 6
// 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';

7 8
import '../../gallery/demo.dart';

9
class IconsDemo extends StatefulWidget {
10
  static const String routeName = '/material/icons';
11

12
  @override
13
  IconsDemoState createState() => IconsDemoState();
Hans Muller's avatar
Hans Muller committed
14 15 16
}

class IconsDemoState extends State<IconsDemo> {
17
  static final List<MaterialColor> iconColors = <MaterialColor>[
Hans Muller's avatar
Hans Muller committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
    Colors.red,
    Colors.pink,
    Colors.purple,
    Colors.deepPurple,
    Colors.indigo,
    Colors.blue,
    Colors.lightBlue,
    Colors.cyan,
    Colors.teal,
    Colors.green,
    Colors.lightGreen,
    Colors.lime,
    Colors.yellow,
    Colors.amber,
    Colors.orange,
    Colors.deepOrange,
    Colors.brown,
    Colors.grey,
36
    Colors.blueGrey,
Hans Muller's avatar
Hans Muller committed
37 38
  ];

39
  int iconColorIndex = 8; // teal
Hans Muller's avatar
Hans Muller committed
40

41
  Color get iconColor => iconColors[iconColorIndex];
Hans Muller's avatar
Hans Muller committed
42 43 44

  void handleIconButtonPress() {
    setState(() {
45
      iconColorIndex = (iconColorIndex + 1) % iconColors.length;
Hans Muller's avatar
Hans Muller committed
46 47 48
    });
  }

49 50
  @override
  Widget build(BuildContext context) {
51 52
    return Scaffold(
      appBar: AppBar(
53 54
        title: const Text('Icons'),
        actions: <Widget>[MaterialDemoDocumentationButton(IconsDemo.routeName)],
55
      ),
56 57 58
      body: IconTheme(
        data: IconThemeData(color: iconColor),
        child: SafeArea(
Ian Hickson's avatar
Ian Hickson committed
59 60
          top: false,
          bottom: false,
61 62 63 64 65 66 67 68 69
          child: Scrollbar(
            child: ListView(
              padding: const EdgeInsets.all(24.0),
              children: <Widget>[
                _IconsDemoCard(handleIconButtonPress, Icons.face), // direction-agnostic icon
                const SizedBox(height: 24.0),
                _IconsDemoCard(handleIconButtonPress, Icons.battery_unknown), // direction-aware icon
              ],
            ),
70 71 72 73 74 75 76 77
          ),
        ),
      ),
    );
  }
}

class _IconsDemoCard extends StatelessWidget {
Ian Hickson's avatar
Ian Hickson committed
78
  const _IconsDemoCard(this.handleIconButtonPress, this.icon);
79 80

  final VoidCallback handleIconButtonPress;
Ian Hickson's avatar
Ian Hickson committed
81
  final IconData icon;
82 83

  Widget _buildIconButton(double iconSize, IconData icon, bool enabled) {
84 85
    return IconButton(
      icon: Icon(icon),
86
      iconSize: iconSize,
87
      tooltip: "${enabled ? 'Enabled' : 'Disabled'} icon button",
88
      onPressed: enabled ? handleIconButtonPress : null,
Hans Muller's avatar
Hans Muller committed
89 90 91
    );
  }

92
  Widget _centeredText(String label) =>
93
    Padding(
94 95
      // Match the default padding of IconButton.
      padding: const EdgeInsets.all(8.0),
96
      child: Text(label, textAlign: TextAlign.center),
97 98 99
    );

  TableRow _buildIconRow(double size) {
100
    return TableRow(
101 102
      children: <Widget> [
        _centeredText(size.floor().toString()),
Ian Hickson's avatar
Ian Hickson committed
103 104
        _buildIconButton(size, icon, true),
        _buildIconButton(size, icon, false),
105
      ],
Hans Muller's avatar
Hans Muller committed
106 107 108
    );
  }

109
  @override
Hans Muller's avatar
Hans Muller committed
110 111
  Widget build(BuildContext context) {
    final ThemeData theme = Theme.of(context);
112
    final TextStyle textStyle = theme.textTheme.subhead.copyWith(color: theme.textTheme.caption.color);
113 114
    return Card(
      child: DefaultTextStyle(
115
        style: textStyle,
116
        child: Semantics(
117
          explicitChildNodes: true,
118
          child: Table(
119 120
            defaultVerticalAlignment: TableCellVerticalAlignment.middle,
            children: <TableRow> [
121
              TableRow(
122 123 124 125 126 127 128 129 130 131 132 133
                children: <Widget> [
                  _centeredText('Size'),
                  _centeredText('Enabled'),
                  _centeredText('Disabled'),
                ]
              ),
              _buildIconRow(18.0),
              _buildIconRow(24.0),
              _buildIconRow(36.0),
              _buildIconRow(48.0),
            ],
          ),
134 135
        ),
      ),
Hans Muller's avatar
Hans Muller committed
136 137 138
    );
  }
}