Skip to content

Commit

Permalink
[auto_animated] Added constructor AutoAnimatedIconButton.externalStat…
Browse files Browse the repository at this point in the history
…e with iconState property
  • Loading branch information
SergeShkurko committed Oct 31, 2019
1 parent 4faa09d commit 278feb8
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 11 deletions.
4 changes: 4 additions & 0 deletions packages/auto_animated/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.2.0

* Added constructor `AutoAnimatedIconButton.externalState` with `iconState` property

## 1.1.0

* Added `AutoAnimatedListState.sepparated`
Expand Down
20 changes: 17 additions & 3 deletions packages/auto_animated/example/lib/screens/icon_button.dart
Original file line number Diff line number Diff line change
@@ -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<AutoAnimatedIconButtonExample> {
bool _externalState = false;

@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(
Expand All @@ -18,9 +27,14 @@ class AutoAnimatedIconButtonExample extends StatelessWidget {
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
AutoAnimatedIconButton(
AutoAnimatedIconButton.externalState(
icon: AnimatedIcons.arrow_menu,
onPressed: () {},
onPressed: () {
setState(() {
_externalState = !_externalState;
});
},
iconState: !_externalState ? IconState.first : IconState.second,
),
AutoAnimatedIconButton(
icon: AnimatedIcons.play_pause,
Expand Down
2 changes: 1 addition & 1 deletion packages/auto_animated/example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ packages:
path: ".."
relative: true
source: path
version: "1.1.0"
version: "1.2.0"
boolean_selector:
dependency: transitive
description:
Expand Down
53 changes: 47 additions & 6 deletions packages/auto_animated/lib/src/auto_animated_icon_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -72,13 +96,30 @@ class _AutoAnimatedIconButtonState extends State<AutoAnimatedIconButton>
_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();
}

Expand Down
2 changes: 1 addition & 1 deletion packages/auto_animated/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
homepage: https://github.com/rbcprolabs/flutter_plugins/tree/master/packages/auto_animated

Expand Down

0 comments on commit 278feb8

Please sign in to comment.