Guest User

Untitled

a guest
Dec 14th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'dart:math' as math;
  3.  
  4. class MyIconButton extends StatefulWidget {
  5. const MyIconButton({
  6. Key key,
  7. @required this.icon,
  8. this.pressedIcon,
  9. this.disabledIcon,
  10. this.iconColor,
  11. this.pressedIconColor,
  12. this.disabledIconColor,
  13. this.splashColor,
  14. this.highlightColor,
  15. this.tooltip,
  16. @required this.onPressed,
  17. }) : assert(icon != null),
  18. assert(onPressed != null),
  19. super(key: key);
  20.  
  21. final Widget icon;
  22. final Widget pressedIcon;
  23. final Widget disabledIcon;
  24. final Color iconColor;
  25. final Color pressedIconColor;
  26. final Color disabledIconColor;
  27. final Color splashColor;
  28. final Color highlightColor;
  29. final String tooltip;
  30. final VoidCallback onPressed;
  31.  
  32. @override
  33. _MyIconButtonState createState() => _MyIconButtonState();
  34. }
  35.  
  36. class _MyIconButtonState extends State<MyIconButton> {
  37. bool _isPressed = false;
  38.  
  39. @override
  40. Widget build(BuildContext context) {
  41. assert(debugCheckHasMaterial(context));
  42.  
  43. final ThemeData theme = Theme.of(context);
  44. final bool enabled = widget.onPressed != null;
  45.  
  46. Widget icon;
  47. Color iconColor;
  48. if (enabled) {
  49. icon = _isPressed ? widget.pressedIcon ?? widget.icon : widget.icon;
  50. iconColor = (_isPressed ? widget.pressedIconColor : widget.iconColor) ?? theme.primaryColor;
  51. } else {
  52. icon = widget.disabledIcon ?? widget.icon;
  53. iconColor = widget.disabledIconColor ?? theme.disabledColor;
  54. }
  55.  
  56. Widget result = Semantics(
  57. button: true,
  58. enabled: enabled,
  59. child: Container(
  60. width: 48.0, // minium tap target size for a button
  61. height: 48.0,
  62. alignment: Alignment.center,
  63. child: IconTheme.merge(
  64. data: IconThemeData(
  65. size: 32.0,
  66. color: iconColor
  67. ),
  68. child: AnimatedSwitcher(
  69. child: KeyedSubtree(
  70. key: ValueKey<int>(!enabled ? 0 : (_isPressed ? 1 : 2)),
  71. child: icon,
  72. ),
  73. duration: kThemeChangeDuration, // used by all buttons
  74. ),
  75. ),
  76. ),
  77. );
  78.  
  79. if (widget.tooltip != null) {
  80. result = Tooltip(
  81. message: widget.tooltip,
  82. child: result,
  83. );
  84. }
  85. return InkResponse(
  86. onTap: widget.onPressed,
  87. highlightColor: widget.highlightColor ?? theme.highlightColor,
  88. splashColor: widget.splashColor ?? theme.splashColor,
  89. radius: 35.0,
  90. onHighlightChanged: (bool value) {
  91. setState(() { _isPressed = value; });
  92. },
  93. child: result,
  94. );
  95. }
  96. }
  97.  
  98.  
  99. class HomePage extends StatelessWidget {
  100. @override
  101. Widget build(BuildContext context) {
  102. return Scaffold(
  103. body: Center(
  104. child: MyIconButton(
  105. icon: Icon(Icons.add_circle_outline),
  106. pressedIcon: Icon(Icons.add_circle),
  107. onPressed: () {
  108. print('Hello World');
  109. },
  110. ),
  111. ),
  112. );
  113. }
  114. }
  115.  
  116. class MyApp extends StatelessWidget {
  117. @override
  118. Widget build(BuildContext context) => HomePage();
  119. }
  120.  
  121. void main() {
  122. runApp(MaterialApp(home: MyApp()));
  123. }
Add Comment
Please, Sign In to add comment