icons_demo.dart 3.59 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 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
          child: ListView(
Ian Hickson's avatar
Ian Hickson committed
62 63
            padding: const EdgeInsets.all(24.0),
            children: <Widget>[
64
              _IconsDemoCard(handleIconButtonPress, Icons.face), // direction-agnostic icon
Ian Hickson's avatar
Ian Hickson committed
65
              const SizedBox(height: 24.0),
66
              _IconsDemoCard(handleIconButtonPress, Icons.battery_unknown), // direction-aware icon
Ian Hickson's avatar
Ian Hickson committed
67
            ],
68 69 70 71 72 73 74 75
          ),
        ),
      ),
    );
  }
}

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

  final VoidCallback handleIconButtonPress;
Ian Hickson's avatar
Ian Hickson committed
79
  final IconData icon;
80 81

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

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

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

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