-
-
Save diegoveloper/1cd23e79a31d0c18a67424f0cbdfd7ad 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, | |
), | |
); | |
} | |
} |
Great, man! Really great!
Extremely Help full thank you. I will be modifying it for my needs
Thanks a lot!
Thanks bro it very useful and smart
Thanks a lot!
Thank a lot!
I have a question about changing that white flash during the transition
is there any way to change it?
Ive found the solution. All you need is to change the theme colors of the app
Here's an improved version with supporting all apis needed:
import 'package:flutter/material.dart';
class FadeIndexedStack extends StatefulWidget {
final int index;
final List<Widget> children;
final Duration duration;
final AlignmentGeometry alignment;
final TextDirection? textDirection;
final Clip clipBehavior;
final StackFit sizing;
const FadeIndexedStack({
super.key,
required this.index,
required this.children,
this.duration = const Duration(
milliseconds: 250,
),
this.alignment = AlignmentDirectional.topStart,
this.textDirection,
this.clipBehavior = Clip.hardEdge,
this.sizing = StackFit.loose,
});
@override
FadeIndexedStackState createState() => FadeIndexedStackState();
}
class FadeIndexedStackState extends State<FadeIndexedStack>
with SingleTickerProviderStateMixin {
late final AnimationController _controller =
AnimationController(vsync: this, duration: widget.duration);
@override
void didUpdateWidget(FadeIndexedStack oldWidget) {
if (widget.index != oldWidget.index) {
_controller.forward(from: 0.0);
}
super.didUpdateWidget(oldWidget);
}
@override
void initState() {
_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,
alignment: widget.alignment,
textDirection: widget.textDirection,
clipBehavior: widget.clipBehavior,
sizing: widget.sizing,
children: widget.children,
),
);
}
}
Thanks for the code. Nicely done.
Thank you very much
very useful
This was very useful, thank you. I made some changes to support lazy loading and published it as a package, to make it easier to use. The package is called fade_indexed_stack.
class FadeIndexedStack extends StatefulWidget {
final int index;
final List<Widget> children;
final Duration duration;
const FadeIndexedStack({
required this.index,
required this.children,
this.duration = const Duration(
milliseconds: 100,
),
super.key,
});
@override
_FadeIndexedStackState createState() => _FadeIndexedStackState();
}
class _FadeIndexedStackState extends State<FadeIndexedStack> with SingleTickerProviderStateMixin {
late AnimationController _controller;
@override
void didUpdateWidget(FadeIndexedStack oldWidget) {
if (!mounted) return super.didUpdateWidget(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,
// required for errors not to occur during rapid switching between tabs
child: RepaintBoundary(
child: IndexedStack(
index: widget.index,
children: widget.children,
),
),
);
}
}
If rapid switching is not working well for you
Using Dart 3.3.0:
class FadeIndexedStack extends StatefulWidget {
const FadeIndexedStack({
super.key,
required this.index,
required this.children,
this.duration = const Duration(milliseconds: 800),
});
final int index;
final List<Widget> children;
final Duration duration;
@override
_FadeIndexedStackState createState() => _FadeIndexedStackState();
}
class _FadeIndexedStackState extends State<FadeIndexedStack>
with SingleTickerProviderStateMixin {
late 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,
),
);
}
}
Thank you very much for your constant updates
If you need the previous widget to fade out rather than disappear abruptly, you need to do so 😁
import 'package:flutter/material.dart';
class FadeIndexedStack extends StatefulWidget {
const FadeIndexedStack({
super.key,
required this.index,
required this.children,
this.duration = const Duration(milliseconds: 200),
});
final int index;
final List<Widget> children;
final Duration duration;
@override
State<FadeIndexedStack> createState() => _FadeIndexedStackState();
}
class _FadeIndexedStackState extends State<FadeIndexedStack> with TickerProviderStateMixin {
late AnimationController _controller;
late Animation<double> _animation;
int _currentIndex = -1;
int? _previousIndex;
@override
void didUpdateWidget(FadeIndexedStack oldWidget) {
if (widget.index != oldWidget.index) {
setState(() {
_previousIndex = _currentIndex;
_currentIndex = widget.index;
_controller.forward(from: 0.0);
});
}
super.didUpdateWidget(oldWidget);
}
@override
void initState() {
super.initState();
_currentIndex = widget.index;
_controller = AnimationController(vsync: this, duration: widget.duration);
_animation = CurvedAnimation(parent: _controller, curve: Curves.easeInOut);
_controller.forward();
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Stack(
children: [
if (_previousIndex != null)
FadeTransition(
opacity:
Tween<double>(begin: 1.0, end: 0.0).animate(_animation),
child: IndexedStack(
index: _previousIndex,
children: widget.children,
),
),
FadeTransition(
opacity: Tween<double>(begin: 0.0, end: 1.0).animate(_animation),
child: IndexedStack(
index: _currentIndex,
children: widget.children,
),
),
],
);
}
}
Nice one, bro
Hey @diegoveloper, regarding null safety:
key
should be nullable:Key? key
required
AnimationController _controller
aslate
to avoid Dart warningnot_initialized_non_nullable_instance_field
.