ios 页面抖动时按顺序淡入小部件

xvw2m8pv  于 11个月前  发布在  iOS
关注(0)|答案(2)|浏览(133)

这个问题和它听起来差不多。有两个屏幕,屏幕1屏幕2屏幕1有一个按钮,可以导航到屏幕2屏幕2有10个小部件,一个接一个地在一列中,我希望他们每个人都淡出后,最后一个已经淡出,让我们说3/4的方式。什么是最好的方式来做到这一点,在Flutter?

fzsnzjdm

fzsnzjdm1#

使用AnimatedOpacity Widget。
可能下面的代码片段会对你有帮助。

class Screen2 extends StatefulWidget {
  @override
  _Screen2State createState() => _Screen2State();
}

class _Screen2State extends State<Screen2> {
  Timer? timer;
  int currentStep = 0;

  @override
  void initState() {
    timer = Timer.periodic(const Duration(seconds: 1), (t) {
      setState(() {
        currentStep = t.tick;
      });

      if (t.tick > 5) {
        timer?.cancel();
      }
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Screen 2'),
      ),
      body: Container(
        width: MediaQuery.of(context).size.width,
        alignment: Alignment.center,
        child: Column(
          children: [
            const SizedBox(height: 16),
            AnimatedOpacity(
              opacity: currentStep >= 1 ? 1 : 0,
              duration: const Duration(milliseconds: 200),
              child: const Text(
                "First Widget",
                style: TextStyle(
                  fontWeight: FontWeight.bold,
                  fontSize: 22,
                ),
              ),
            ),
            const SizedBox(height: 50),
            AnimatedOpacity(
              opacity: currentStep >= 2 ? 1 : 0,
              duration: const Duration(milliseconds: 200),
              child: const Text(
                "Second Widget",
                style: TextStyle(
                  fontWeight: FontWeight.bold,
                  fontSize: 22,
                ),
              ),
            ),
            const SizedBox(height: 50),
            AnimatedOpacity(
              opacity: currentStep >= 3 ? 1 : 0,
              duration: const Duration(milliseconds: 200),
              child: const Column(
                children: [
                  Text(
                    "Third Widget",
                    style: TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: 22,
                    ),
                  ),
                  SizedBox(height: 50),
                  Text(
                    "Fourth Widget",
                    style: TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: 22,
                    ),
                  ),
                ],
              ),
            ),
            const SizedBox(height: 50),
            AnimatedOpacity(
              opacity: currentStep >= 4 ? 1 : 0,
              duration: const Duration(milliseconds: 200),
              child: ElevatedButton(
                style: const ButtonStyle(
                    backgroundColor: MaterialStatePropertyAll(Colors.cyan)),
                onPressed: () {},
                child: const Text(
                  "Fifth Widget",
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: 18,
                  ),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}

字符串

rslzwgfq

rslzwgfq2#

您可以通过使用OpacityAnimatedOpacity来实现这一点
关于Screen2

Column(
  children: [
    AnimatedOpacity(
      duration: Duration(seconds: 1),
      opacity: 1.0,
      child: YourWidget1(),
    ),
    AnimatedOpacity(
      duration: Duration(seconds: 1),
      opacity: 1.0,
      child: YourWidget2(),
    ),
    // Other widgets
  ],
)

字符串
现在,为了错开淡入,您可以使用Future.delayed,为每个小部件提供累积持续时间。

Future<void> _delayedFadeIn(int index) async {
  await Future.delayed(Duration(milliseconds: (index + 1) * 750));
}

// Use this function when building your widgets
AnimatedOpacity(
  duration: Duration(seconds: 1),
  opacity: 1.0,
  child: YourWidget1(),
).then((_) => _delayedFadeIn(1)),


冲洗并重复每个部件,相应地调整索引。

相关问题