Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduction of SwitchEffect() #128

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ text.animate().fadeOut(300.ms) // fade out & then...
.swap(builder: (_, child) => child.animate().fadeIn())
```

See also SwitchEffect() for an alternative implementation of animations which switches between two or more childs.

ShaderEffect
----------------------------------------
`ShaderEffect` makes it easy to apply animated GLSL fragment shaders to widgets.
Expand Down
5 changes: 3 additions & 2 deletions lib/flutter_animate.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export 'src/adapters/adapters.dart';
export 'src/animate.dart';
export 'src/animate_list.dart';
export 'src/effect_list.dart';
export 'src/flutter_animate.dart';
export 'src/adapters/adapters.dart';
export 'src/effects/effects.dart';
export 'src/extensions/extensions.dart';
export 'src/flutter_animate.dart';
export 'src/switch_effect.dart';
89 changes: 89 additions & 0 deletions lib/src/switch_effect.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter_animate/flutter_animate.dart';

/// A widget for implementing switch effects. When calling the first time the given [child] will be animated with [inEffects].
/// When the widget is called again with a different child the [outEffects] of the previous widget instance will be applied to
/// the [child] handled by the previous widget immediately following with the animation of the new child by using the
/// [inEffects] of the new widget. Before switching the childs the optional callback method is triggered.
class SwitchEffect extends StatefulWidget {
/// the child for this animation
final Widget child;

/// a list of effects to apply to the child when the child will be shown. Default is fadeIn()
final List<Effect>? inEffects;

/// a list of effects to apply to the child when the child should be replaced by another child. Default is fadeOut()
final List<Effect>? outEffects;

/// Callback function which will be triggered right after the outEffects are completed and before the new child will be swapped in for the inEffects
final void Function(BuildContext)? onSwap;

/// Callbak function which will be triggered after all effects are completed
final void Function(BuildContext)? onComplete;

const SwitchEffect(
{super.key,
required this.child,
this.inEffects,
this.outEffects,
this.onSwap,
this.onComplete});

@override
State<StatefulWidget> createState() {
return _SwitchEffectState();
}
}

////////////////////////////////////////////////////////////////////////////////

class _SwitchEffectState extends State {
@override
SwitchEffect get widget => super.widget as SwitchEffect;

/// when changing the child of the widget this field will be filled with the child of the previous widget.
Widget? _lastChild;

List<Effect>? _lastOutEffects;

@override
Widget build(BuildContext context) {
if (_lastChild != null) {
return Animate(
key: GlobalKey(),
effects: _lastOutEffects ?? EffectList().fadeOut(),
onComplete: (_) {
if (widget.onSwap != null) widget.onSwap!(context);
},
child: _lastChild!,
).then().swap(
duration: 0.seconds,
delay: 0.seconds,
builder: (BuildContext context, _) => Animate(
effects: widget.inEffects ?? EffectList().fadeIn(),
onComplete: (_) {
if (widget.onComplete != null) widget.onComplete!(context);
},
child: widget.child,
));
}

return Animate(
key: GlobalKey(),
effects: widget.inEffects ?? EffectList().fadeIn(),
onComplete: (_) {
if (widget.onComplete != null) widget.onComplete!(context);
},
child: widget.child,
);
}

@override
void didUpdateWidget(covariant SwitchEffect oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.child != widget.child) {
_lastChild = oldWidget.child;
_lastOutEffects = oldWidget.outEffects;
}
}
}