icon_button.dart 2.32 KB
Newer Older
1 2 3 4
// Copyright 2015 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.

5
import 'package:flutter/widgets.dart';
6

7
import 'debug.dart';
8
import 'icon.dart';
9
import 'icons.dart';
10
import 'ink_well.dart';
11
import 'theme.dart';
Hixie's avatar
Hixie committed
12
import 'tooltip.dart';
13

14 15 16 17 18 19 20 21 22
/// A material design "icon button".
///
/// An icon button is a picture printed on a [Material] widget that reacts to
/// touches by filling with color.
///
/// Use icon buttons on toolbars.
///
/// If the [onPressed] callback is not specified or null, then the button will
/// be disabled, will not react to touch.
23
class IconButton extends StatelessWidget {
24 25
  const IconButton({
    Key key,
26
    this.size: 24.0,
27 28
    this.icon,
    this.color,
Hixie's avatar
Hixie committed
29 30
    this.onPressed,
    this.tooltip
31
  }) : super(key: key);
32

Adam Barth's avatar
Adam Barth committed
33 34 35 36
  /// The size of the icon inside the button.
  ///
  /// The button itself will be larger than the icon by 8.0 logical pixels in
  /// each direction.
37
  final double size;
Adam Barth's avatar
Adam Barth committed
38 39

  /// The icon to display inside the button.
40
  final IconData icon;
Adam Barth's avatar
Adam Barth committed
41 42

  /// The color to use for the icon inside the button.
Adam Barth's avatar
Adam Barth committed
43
  final Color color;
44 45 46 47

  /// The callback that is invoked when the button is tapped or otherwise activated.
  ///
  /// If this is set to null, the button will be disabled.
48
  final VoidCallback onPressed;
Adam Barth's avatar
Adam Barth committed
49 50 51 52 53

  /// Text that describes the action that will occur when the button is pressed.
  ///
  /// This text is displayed when the user long-presses on the button and is
  /// used for accessibility.
Hixie's avatar
Hixie committed
54
  final String tooltip;
55

56
  @override
57
  Widget build(BuildContext context) {
58
    assert(debugCheckHasMaterial(context));
Hixie's avatar
Hixie committed
59
    Widget result = new Padding(
60
      padding: const EdgeInsets.all(8.0),
Hixie's avatar
Hixie committed
61
      child: new Icon(
Hans Muller's avatar
Hans Muller committed
62
        size: size,
Hixie's avatar
Hixie committed
63
        icon: icon,
64
        color: onPressed != null ? color : Theme.of(context).disabledColor
65
      )
66
    );
Hixie's avatar
Hixie committed
67 68 69 70 71 72
    if (tooltip != null) {
      result = new Tooltip(
        message: tooltip,
        child: result
      );
    }
Hixie's avatar
Hixie committed
73 74 75 76
    return new InkResponse(
      onTap: onPressed,
      child: result
    );
77
  }
Hixie's avatar
Hixie committed
78

79
  @override
Hixie's avatar
Hixie committed
80 81 82
  void debugFillDescription(List<String> description) {
    super.debugFillDescription(description);
    description.add('$icon');
Hixie's avatar
Hixie committed
83 84 85 86
    if (onPressed == null)
      description.add('disabled');
    if (tooltip != null)
      description.add('tooltip: "$tooltip"');
Hixie's avatar
Hixie committed
87
  }
88
}