flutter - 如何在颤动中刷新用户界面?
问题描述
我正在构建一个简单的小部件,我想及时更新它。
class Example extends StatelessWidget {
final bool isWhite;
Example(this.isWhite)
Widget build(BuildContext context) {
return Container(
color: isWhite ? Colors.white : Colors.black
);
}
}
解决方案
您需要使用 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
);
}
}
推荐阅读
- reactjs - 是否可以将 Material UI 作为 peerDependency 但将其类型保持为 DevDependency?
- datatables - 数据表:当 Ajax 调用呈现数据时,可见选项不起作用
- python - 有没有办法在 Altair 中循环和制作情节?
- asp.net - 如何在 ASPX 页面上使用 Angular 和 WebMethod 上传文件?
- html - 为什么浮动框可以创建文本环绕效果而固定或绝对定位的框不能?
- sql - 如何为此动态 sql 添加分页
- python - 如果我们在 conftest 文件中使用 return,我们如何关闭驱动程序
- flutter - Flutter Textformfield 如何使文本在 Textformfield 中居中
- swift - 使用 flatMap 链接
- reactjs - Redux-Toolkit 查询拦截器