首页 > 解决方案 > 如何在颤动中刷新用户界面?

问题描述

我正在构建一个简单的小部件,我想及时更新它。

class Example extends StatelessWidget {

  final bool isWhite;

  Example(this.isWhite)

  Widget build(BuildContext context) {
    return Container(
      color: isWhite ? Colors.white : Colors.black
    );
  }
}

标签: flutter

解决方案


您需要使用 StatefulWidget 小部件并调用

class Example extends StatefulWidget {
  @override
  State createState() => ExampleState();
}

class ExampleState extends State<Example> {
   Widget build(BuildContext context) {
    Future<void>.delayed(const Duration(seconds: 0)).then(() {
        setState() {
        isWhite = !isWhite;
      });
    });
    return Container(
      color: isWhite ? Colors.white : Colors.black
    );
  }
}

推荐阅读