【问题标题】:Change the value of a Text widget by animating the opacity down to 0 and back to 1 in Flutter通过在 Flutter 中将不透明度设置为 0 并返回 1 来更改 Text 小部件的值
【发布时间】:2020-03-06 13:19:13
【问题描述】:

我如何在 Flutter 中做到这一点:

我想在按下FloatingActionButton 时更新Text 小部件的值:

  • 将Text 小部件的不透明度设置为 0 并持续 1 秒(隐藏旧值)
  • 更改Text 小部件的值(设置一个新值 - setState?)
  • 将 Text 小部件的不透明度设置为 1 并持续 1 秒(显示新值)

我可以使用AnimatedOpacity 执行此操作吗?

【问题讨论】:

    标签: animation flutter opacity flutter-animation


    【解决方案1】:

    是的,你可以,在调用第二个 setState 之前不要忘记等待以重新激活文本。

      String text = 'Text Initial';
      double opacity = 1.0;
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            floatingActionButton: FloatingActionButton(
              onPressed: () async {
                setState(() {
                  opacity = 0.0;
                });
                await Future.delayed(const Duration(seconds: 1));
                setState(() {
                  text = 'New Text';
                  opacity = 1.0;
                });
              },
            ),
            body: Center(
              child: AnimatedOpacity(
                duration: const Duration(seconds: 1),
                opacity: opacity,
                child: Text(text),
              ),
            ),
          ),
        );
      }
    

    【讨论】:

      猜你喜欢
      • 2012-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-28
      • 1970-01-01
      • 2013-06-15
      • 1970-01-01
      相关资源
      最近更新 更多