icons_demo.dart 3.4 KB
Newer Older
Hans Muller's avatar
Hans Muller committed
1 2 3 4 5 6
// Copyright 2016 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';

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

10
  @override
Hans Muller's avatar
Hans Muller committed
11 12 13 14
  IconsDemoState createState() => new IconsDemoState();
}

class IconsDemoState extends State<IconsDemo> {
15
  static final List<MaterialColor> iconColors = <MaterialColor>[
Hans Muller's avatar
Hans Muller committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
    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,
34
    Colors.blueGrey,
Hans Muller's avatar
Hans Muller committed
35 36
  ];

37
  int iconColorIndex = 8; // teal
Hans Muller's avatar
Hans Muller committed
38

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

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

47 48 49 50 51 52 53 54 55 56
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: const Text('Icons')
      ),
      body: new IconTheme(
        data: new IconThemeData(color: iconColor),
        child: new Padding(
          padding: const EdgeInsets.all(24.0),
57 58 59 60 61 62 63 64
          child: new SafeArea(
            top: false,
            bottom: false,
            child: new Column(
              children: <Widget>[
                new _IconsDemoCard(handleIconButtonPress),
              ],
            ),
65 66 67 68 69 70 71 72 73 74 75 76 77 78
          ),
        ),
      ),
    );
  }
}

class _IconsDemoCard extends StatelessWidget {

  const _IconsDemoCard(this.handleIconButtonPress);

  final VoidCallback handleIconButtonPress;

  Widget _buildIconButton(double iconSize, IconData icon, bool enabled) {
Hans Muller's avatar
Hans Muller committed
79
    return new IconButton(
Ian Hickson's avatar
Ian Hickson committed
80
      icon: new Icon(icon),
81
      iconSize: iconSize,
82
      tooltip: "${enabled ? 'Enabled' : 'Disabled'} icon button",
Hans Muller's avatar
Hans Muller committed
83 84 85 86
      onPressed: enabled ? handleIconButtonPress : null
    );
  }

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

  TableRow _buildIconRow(double size) {
    return new TableRow(
      children: <Widget> [
        _centeredText(size.floor().toString()),
        _buildIconButton(size, Icons.face, true),
        _buildIconButton(size, Icons.face, false),
      ],
Hans Muller's avatar
Hans Muller committed
101 102 103
    );
  }

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