icons_demo.dart 3.74 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
  const IconsDemo({super.key});
11

12
  static const String routeName = '/material/icons';
13

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

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

41
  int iconColorIndex = 8; // teal
Hans Muller's avatar
Hans Muller committed
42

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

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

51 52
  @override
  Widget build(BuildContext context) {
53 54
    return Scaffold(
      appBar: AppBar(
55 56
        title: const Text('Icons'),
        actions: <Widget>[MaterialDemoDocumentationButton(IconsDemo.routeName)],
57
      ),
58 59 60
      body: IconTheme(
        data: IconThemeData(color: iconColor),
        child: SafeArea(
Ian Hickson's avatar
Ian Hickson committed
61 62
          top: false,
          bottom: false,
63 64
          child: Scrollbar(
            child: ListView(
65
              primary: true,
66 67 68 69 70 71 72
              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
              ],
            ),
73 74 75 76 77 78 79 80
          ),
        ),
      ),
    );
  }
}

class _IconsDemoCard extends StatelessWidget {
Ian Hickson's avatar
Ian Hickson committed
81
  const _IconsDemoCard(this.handleIconButtonPress, this.icon);
82 83

  final VoidCallback handleIconButtonPress;
Ian Hickson's avatar
Ian Hickson committed
84
  final IconData icon;
85 86

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

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

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

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