Skip to content

Instantly share code, notes, and snippets.

@d1y
Forked from diegoveloper/fade_indexed_stack.dart
Created June 14, 2021 15:50
Show Gist options
  • Save d1y/ff4986b6491f5a00b14610c197bd6b10 to your computer and use it in GitHub Desktop.
Save d1y/ff4986b6491f5a00b14610c197bd6b10 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
class FadeIndexedStack extends StatefulWidget {
final int index;
final List<Widget> children;
final Duration duration;
const FadeIndexedStack({
Key key,
this.index,
this.children,
this.duration = const Duration(
milliseconds: 800,
),
}) : super(key: key);
@override
_FadeIndexedStackState createState() => _FadeIndexedStackState();
}
class _FadeIndexedStackState extends State<FadeIndexedStack>
with SingleTickerProviderStateMixin {
AnimationController _controller;
@override
void didUpdateWidget(FadeIndexedStack oldWidget) {
if (widget.index != oldWidget.index) {
_controller.forward(from: 0.0);
}
super.didUpdateWidget(oldWidget);
}
@override
void initState() {
_controller = AnimationController(vsync: this, duration: widget.duration);
_controller.forward();
super.initState();
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return FadeTransition(
opacity: _controller,
child: IndexedStack(
index: widget.index,
children: widget.children,
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment