Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- import 'dart:math' as math;
- class MyIconButton extends StatefulWidget {
- const MyIconButton({
- Key key,
- @required this.icon,
- this.pressedIcon,
- this.disabledIcon,
- this.iconColor,
- this.pressedIconColor,
- this.disabledIconColor,
- this.splashColor,
- this.highlightColor,
- this.tooltip,
- @required this.onPressed,
- }) : assert(icon != null),
- assert(onPressed != null),
- super(key: key);
- final Widget icon;
- final Widget pressedIcon;
- final Widget disabledIcon;
- final Color iconColor;
- final Color pressedIconColor;
- final Color disabledIconColor;
- final Color splashColor;
- final Color highlightColor;
- final String tooltip;
- final VoidCallback onPressed;
- @override
- _MyIconButtonState createState() => _MyIconButtonState();
- }
- class _MyIconButtonState extends State<MyIconButton> {
- bool _isPressed = false;
- @override
- Widget build(BuildContext context) {
- assert(debugCheckHasMaterial(context));
- final ThemeData theme = Theme.of(context);
- final bool enabled = widget.onPressed != null;
- Widget icon;
- Color iconColor;
- if (enabled) {
- icon = _isPressed ? widget.pressedIcon ?? widget.icon : widget.icon;
- iconColor = (_isPressed ? widget.pressedIconColor : widget.iconColor) ?? theme.primaryColor;
- } else {
- icon = widget.disabledIcon ?? widget.icon;
- iconColor = widget.disabledIconColor ?? theme.disabledColor;
- }
- Widget result = Semantics(
- button: true,
- enabled: enabled,
- child: Container(
- width: 48.0, // minium tap target size for a button
- height: 48.0,
- alignment: Alignment.center,
- child: IconTheme.merge(
- data: IconThemeData(
- size: 32.0,
- color: iconColor
- ),
- child: AnimatedSwitcher(
- child: KeyedSubtree(
- key: ValueKey<int>(!enabled ? 0 : (_isPressed ? 1 : 2)),
- child: icon,
- ),
- duration: kThemeChangeDuration, // used by all buttons
- ),
- ),
- ),
- );
- if (widget.tooltip != null) {
- result = Tooltip(
- message: widget.tooltip,
- child: result,
- );
- }
- return InkResponse(
- onTap: widget.onPressed,
- highlightColor: widget.highlightColor ?? theme.highlightColor,
- splashColor: widget.splashColor ?? theme.splashColor,
- radius: 35.0,
- onHighlightChanged: (bool value) {
- setState(() { _isPressed = value; });
- },
- child: result,
- );
- }
- }
- class HomePage extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- body: Center(
- child: MyIconButton(
- icon: Icon(Icons.add_circle_outline),
- pressedIcon: Icon(Icons.add_circle),
- onPressed: () {
- print('Hello World');
- },
- ),
- ),
- );
- }
- }
- class MyApp extends StatelessWidget {
- @override
- Widget build(BuildContext context) => HomePage();
- }
- void main() {
- runApp(MaterialApp(home: MyApp()));
- }
Add Comment
Please, Sign In to add comment