Skip to content

Instantly share code, notes, and snippets.

@diegoveloper
Last active February 27, 2025 16:49

Revisions

  1. diegoveloper revised this gist May 6, 2020. 1 changed file with 2 additions and 8 deletions.
    10 changes: 2 additions & 8 deletions fade_indexed_stack.dart
    Original file line number Diff line number Diff line change
    @@ -45,14 +45,8 @@ class _FadeIndexedStackState extends State<FadeIndexedStack>

    @override
    Widget build(BuildContext context) {
    return AnimatedBuilder(
    animation: _controller,
    builder: (context, child) {
    return Opacity(
    opacity: _controller.value,
    child: child,
    );
    },
    return FadeTransition(
    opacity: _controller,
    child: IndexedStack(
    index: widget.index,
    children: widget.children,
  2. diegoveloper created this gist Dec 30, 2019.
    62 changes: 62 additions & 0 deletions fade_indexed_stack.dart
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    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 AnimatedBuilder(
    animation: _controller,
    builder: (context, child) {
    return Opacity(
    opacity: _controller.value,
    child: child,
    );
    },
    child: IndexedStack(
    index: widget.index,
    children: widget.children,
    ),
    );
    }
    }