icons_demo.dart 5.15 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 9
  static const String routeName = '/icons';

10
  @override
Hans Muller's avatar
Hans Muller committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
  IconsDemoState createState() => new IconsDemoState();
}

class IconsDemoState extends State<IconsDemo> {
  static final List<Map<int, Color>> iconColorSwatches = <Map<int, Color>>[
    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,
    Colors.blueGrey
  ];

37
  int iconColorIndex = 8; // teal
Hans Muller's avatar
Hans Muller committed
38 39 40 41 42 43 44 45 46 47
  double iconOpacity = 1.0;

  Color get iconColor => iconColorSwatches[iconColorIndex][400];

  void handleIconButtonPress() {
    setState(() {
      iconColorIndex = (iconColorIndex + 1) % iconColorSwatches.length;
    });
  }

48
  Widget buildIconButton(double size, IconData icon, bool enabled) {
Hans Muller's avatar
Hans Muller committed
49
    return new IconButton(
Ian Hickson's avatar
Ian Hickson committed
50
      icon: new Icon(icon),
Hans Muller's avatar
Hans Muller committed
51 52
      size: size,
      color: iconColor,
53
      tooltip: "${enabled ? 'Enabled' : 'Disabled'} icon button",
Hans Muller's avatar
Hans Muller committed
54 55 56 57 58 59 60 61 62 63 64 65 66
      onPressed: enabled ? handleIconButtonPress : null
    );
  }

  Widget buildSizeLabel(int size, TextStyle style) {
    return new SizedBox(
      height: size.toDouble() + 16.0, // to match an IconButton's padded height
      child: new Center(
        child: new Text('$size', style: style)
      )
    );
  }

67
  @override
Hans Muller's avatar
Hans Muller committed
68 69
  Widget build(BuildContext context) {
    final ThemeData theme = Theme.of(context);
70
    final TextStyle textStyle = theme.textTheme.subhead.copyWith(color: theme.textTheme.caption.color);
Hans Muller's avatar
Hans Muller committed
71 72

    return new Scaffold(
73 74
      appBar: new AppBar(
        title: new Text('Icons')
Hans Muller's avatar
Hans Muller committed
75 76 77 78
      ),
      body: new IconTheme(
        data: new IconThemeData(opacity: iconOpacity),
        child: new Padding(
79
          padding: const EdgeInsets.all(24.0),
Hans Muller's avatar
Hans Muller committed
80 81 82
          child: new Column(
            children: <Widget>[
              new Row(
83 84
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                crossAxisAlignment: CrossAxisAlignment.center,
Hans Muller's avatar
Hans Muller committed
85
                children: <Widget>[
86 87 88 89 90 91 92 93 94
                  new Column(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: <Widget>[
                      new Text('Size', style: textStyle),
                      buildSizeLabel(18, textStyle),
                      buildSizeLabel(24, textStyle),
                      buildSizeLabel(36, textStyle),
                      buildSizeLabel(48, textStyle)
                    ]
Hans Muller's avatar
Hans Muller committed
95
                  ),
96
                  new Expanded(
Hans Muller's avatar
Hans Muller committed
97
                    child: new Column(
98
                      crossAxisAlignment: CrossAxisAlignment.center,
Hans Muller's avatar
Hans Muller committed
99 100
                      children: <Widget>[
                        new Text('Enabled', style: textStyle),
101 102 103 104
                        buildIconButton(18.0, Icons.face, true),
                        buildIconButton(24.0, Icons.alarm, true),
                        buildIconButton(36.0, Icons.home, true),
                        buildIconButton(48.0, Icons.android, true)
Hans Muller's avatar
Hans Muller committed
105 106 107
                      ]
                    )
                  ),
108
                  new Expanded(
Hans Muller's avatar
Hans Muller committed
109
                    child: new Column(
110
                      crossAxisAlignment: CrossAxisAlignment.center,
Hans Muller's avatar
Hans Muller committed
111 112
                      children: <Widget>[
                        new Text('Disabled', style: textStyle),
113 114 115 116
                        buildIconButton(18.0, Icons.face, false),
                        buildIconButton(24.0, Icons.alarm, false),
                        buildIconButton(36.0, Icons.home, false),
                        buildIconButton(48.0, Icons.android, false)
Hans Muller's avatar
Hans Muller committed
117 118 119 120 121
                      ]
                    )
                  )
                ]
              ),
122
              new Expanded(
Hans Muller's avatar
Hans Muller committed
123 124 125 126
                child: new Center(
                  child: new IconTheme(
                    data: new IconThemeData(opacity: 1.0),
                    child: new Row(
127
                      mainAxisAlignment: MainAxisAlignment.center,
Hans Muller's avatar
Hans Muller committed
128 129
                      children: <Widget>[
                        new Icon(
Ian Hickson's avatar
Ian Hickson committed
130
                          Icons.brightness_7,
Hans Muller's avatar
Hans Muller committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144
                          color: iconColor.withAlpha(0x33) // 0.2 * 255 = 0x33
                        ),
                        new Slider(
                          value: iconOpacity,
                          min: 0.2,
                          max: 1.0,
                          activeColor: iconColor,
                          onChanged: (double newValue) {
                            setState(() {
                              iconOpacity = newValue;
                            });
                          }
                        ),
                        new Icon(
Ian Hickson's avatar
Ian Hickson committed
145
                          Icons.brightness_7,
Hans Muller's avatar
Hans Muller committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159
                          color: iconColor.withAlpha(0xFF)
                        ),
                      ]
                    )
                  )
                )
              )
            ]
          )
        )
      )
    );
  }
}