From 278feb84357ac82d7482b225e8596aa968873779 Mon Sep 17 00:00:00 2001 From: Serge Shkurko Date: Tue, 1 Oct 2019 15:46:47 +0500 Subject: [PATCH] [auto_animated] Added constructor AutoAnimatedIconButton.externalState with iconState property --- packages/auto_animated/CHANGELOG.md | 4 ++ .../example/lib/screens/icon_button.dart | 20 +++++-- packages/auto_animated/example/pubspec.lock | 2 +- .../lib/src/auto_animated_icon_button.dart | 53 ++++++++++++++++--- packages/auto_animated/pubspec.yaml | 2 +- 5 files changed, 70 insertions(+), 11 deletions(-) diff --git a/packages/auto_animated/CHANGELOG.md b/packages/auto_animated/CHANGELOG.md index c83cc8c2..76cb529f 100644 --- a/packages/auto_animated/CHANGELOG.md +++ b/packages/auto_animated/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.2.0 + +* Added constructor `AutoAnimatedIconButton.externalState` with `iconState` property + ## 1.1.0 * Added `AutoAnimatedListState.sepparated` diff --git a/packages/auto_animated/example/lib/screens/icon_button.dart b/packages/auto_animated/example/lib/screens/icon_button.dart index a39c0ba8..98cd82a8 100644 --- a/packages/auto_animated/example/lib/screens/icon_button.dart +++ b/packages/auto_animated/example/lib/screens/icon_button.dart @@ -1,7 +1,16 @@ import 'package:flutter/material.dart'; import 'package:auto_animated/auto_animated.dart'; -class AutoAnimatedIconButtonExample extends StatelessWidget { +class AutoAnimatedIconButtonExample extends StatefulWidget { + @override + _AutoAnimatedIconButtonExampleState createState() => + _AutoAnimatedIconButtonExampleState(); +} + +class _AutoAnimatedIconButtonExampleState + extends State { + bool _externalState = false; + @override Widget build(BuildContext context) => Scaffold( appBar: AppBar( @@ -18,9 +27,14 @@ class AutoAnimatedIconButtonExample extends StatelessWidget { child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ - AutoAnimatedIconButton( + AutoAnimatedIconButton.externalState( icon: AnimatedIcons.arrow_menu, - onPressed: () {}, + onPressed: () { + setState(() { + _externalState = !_externalState; + }); + }, + iconState: !_externalState ? IconState.first : IconState.second, ), AutoAnimatedIconButton( icon: AnimatedIcons.play_pause, diff --git a/packages/auto_animated/example/pubspec.lock b/packages/auto_animated/example/pubspec.lock index d4e6b6be..783b1f35 100644 --- a/packages/auto_animated/example/pubspec.lock +++ b/packages/auto_animated/example/pubspec.lock @@ -14,7 +14,7 @@ packages: path: ".." relative: true source: path - version: "1.1.0" + version: "1.2.0" boolean_selector: dependency: transitive description: diff --git a/packages/auto_animated/lib/src/auto_animated_icon_button.dart b/packages/auto_animated/lib/src/auto_animated_icon_button.dart index 874e20d9..22dc4b22 100644 --- a/packages/auto_animated/lib/src/auto_animated_icon_button.dart +++ b/packages/auto_animated/lib/src/auto_animated_icon_button.dart @@ -34,6 +34,29 @@ class AutoAnimatedIconButton extends StatefulWidget { this.textDirection, this.firstToolip, this.secondToolip, + }) : iconState = null, + super(key: key); + + AutoAnimatedIconButton.externalState({ + @required this.icon, + @required this.onPressed, + @required this.iconState, + Key key, + this.duration = const Duration(milliseconds: 300), + this.splashColor, + this.hoverColor, + this.size = 24, + this.padding = const EdgeInsets.all(18), + this.alignment = Alignment.center, + this.color, + this.focusColor, + this.highlightColor, + this.disabledColor, + this.focusNode, + this.semanticLabel, + this.textDirection, + this.firstToolip, + this.secondToolip, }) : super(key: key); final AnimatedIconData icon; @@ -47,6 +70,7 @@ class AutoAnimatedIconButton extends StatefulWidget { final Color color, focusColor, highlightColor, disabledColor; final FocusNode focusNode; final TextDirection textDirection; + final IconState iconState; @override _AutoAnimatedIconButtonState createState() => _AutoAnimatedIconButtonState(); @@ -72,13 +96,30 @@ class _AutoAnimatedIconButtonState extends State _animationController.dispose(); } + @override + void didUpdateWidget(AutoAnimatedIconButton oldWidget) { + if (oldWidget.iconState != widget.iconState) { + if (oldWidget.iconState == IconState.first && + widget.iconState == IconState.second) { + _animationController.forward(); + } else if (oldWidget.iconState == IconState.second && + widget.iconState == IconState.first) { + _animationController.reverse(); + } + } + + super.didUpdateWidget(oldWidget); + } + void _onPressed() { - setState(() { - _isPressed = !_isPressed; - !_isPressed - ? _animationController.reverse() - : _animationController.forward(); - }); + if (widget.iconState == null) { + setState(() { + _isPressed = !_isPressed; + !_isPressed + ? _animationController.reverse() + : _animationController.forward(); + }); + } widget.onPressed(); } diff --git a/packages/auto_animated/pubspec.yaml b/packages/auto_animated/pubspec.yaml index 5fa1f5ed..644c6799 100644 --- a/packages/auto_animated/pubspec.yaml +++ b/packages/auto_animated/pubspec.yaml @@ -1,6 +1,6 @@ name: auto_animated description: Widgets starting auto play animation when mounted. It is already possible to animate the list and icons. -version: 1.1.0 +version: 1.2.0 author: Serge Shkurko homepage: https://github.com/rbcprolabs/flutter_plugins/tree/master/packages/auto_animated