Commit e382701a authored by Dragoș Tiselice's avatar Dragoș Tiselice Committed by GitHub

Changed ExpandIcon to accept default state. (#5521)

Changed the ExpandIcon constructor to take a default expansion state.
If the widget gets rebuilt with a different expansion value, the
animation will trigger but the callback will not.
parent cc600fc0
...@@ -23,21 +23,29 @@ class ExpandIcon extends StatefulWidget { ...@@ -23,21 +23,29 @@ class ExpandIcon extends StatefulWidget {
/// triggered when the icon is pressed. /// triggered when the icon is pressed.
ExpandIcon({ ExpandIcon({
Key key, Key key,
this.isExpanded: false,
this.size: 24.0, this.size: 24.0,
@required this.onPressed, @required this.onPressed,
this.padding: const EdgeInsets.all(8.0) this.padding: const EdgeInsets.all(8.0)
}) : super(key: key) { }) : super(key: key) {
assert(this.isExpanded != null);
assert(this.size != null); assert(this.size != null);
assert(this.padding != null); assert(this.padding != null);
} }
/// Whether the icon is in an expanded state.
///
/// Rebuilding the widget with a different [isExpanded] value will trigger
/// the animation, but will not trigger the [onPressed] callback.
final bool isExpanded;
/// The size of the icon. /// The size of the icon.
/// ///
/// This property must not be null. It defaults to 24.0. /// This property must not be null. It defaults to 24.0.
final double size; final double size;
/// The callback triggered when the icon is pressed and the state changes /// The callback triggered when the icon is pressed and the state changes
/// between expanded and collapsed. /// between expanded and collapsed. The value passed to the current state.
/// ///
/// If this is set to null, the button will be disabled. /// If this is set to null, the button will be disabled.
final ValueChanged<bool> onPressed; final ValueChanged<bool> onPressed;
...@@ -55,7 +63,6 @@ class ExpandIcon extends StatefulWidget { ...@@ -55,7 +63,6 @@ class ExpandIcon extends StatefulWidget {
class _ExpandIconState extends State<ExpandIcon> { class _ExpandIconState extends State<ExpandIcon> {
AnimationController _controller; AnimationController _controller;
Animation<double> _iconTurns; Animation<double> _iconTurns;
bool _isExpanded = false;
@override @override
void initState() { void initState() {
...@@ -75,17 +82,20 @@ class _ExpandIconState extends State<ExpandIcon> { ...@@ -75,17 +82,20 @@ class _ExpandIconState extends State<ExpandIcon> {
super.dispose(); super.dispose();
} }
void _handlePressed() { @override
setState(() { void didUpdateConfig(ExpandIcon oldConfig) {
_isExpanded = !_isExpanded; if (config.isExpanded != oldConfig.isExpanded) {
if (_isExpanded) if (config.isExpanded) {
_controller.forward(); _controller.forward();
else } else {
_controller.reverse(); _controller.reverse();
}); }
}
}
void _handlePressed() {
if (config.onPressed != null) if (config.onPressed != null)
config.onPressed(_isExpanded); config.onPressed(config.isExpanded);
} }
@override @override
......
...@@ -43,4 +43,36 @@ void main() { ...@@ -43,4 +43,36 @@ void main() {
IconTheme iconTheme = tester.firstWidget(find.byType(IconTheme)); IconTheme iconTheme = tester.firstWidget(find.byType(IconTheme));
expect(iconTheme.data.color, equals(Colors.black26)); expect(iconTheme.data.color, equals(Colors.black26));
}); });
testWidgets('ExpandIcon test isExpanded does not trigger callback', (WidgetTester tester) async {
bool expanded = false;
await tester.pumpWidget(
new Material(
child: new Center(
child: new ExpandIcon(
isExpanded: false,
onPressed: (bool isExpanded) {
expanded = !expanded;
}
)
)
)
);
await tester.pumpWidget(
new Material(
child: new Center(
child: new ExpandIcon(
isExpanded: true,
onPressed: (bool isExpanded) {
expanded = !expanded;
}
)
)
)
);
expect(expanded, isFalse);
});
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment