当前位置:网站首页>Flutter Builder & FutureBuilder组件
Flutter Builder & FutureBuilder组件
2022-06-25 03:53:00 【xiangxiongfly915】
Flutter Builder & FutureBuilder组件
Builder组件
Builder组件起闭包作用,可以限制期使用范围。
问题
class BuilderPage extends StatelessWidget {
const BuilderPage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Builder组件"),
),
body: Center(
child: RaisedButton(
color: Colors.red,
textColor: Colors.white,
onPressed: () {
showSnackBar(context);
},
child: const Text('show SnackBar'),
),
),
);
}
showSnackBar(BuildContext context) {
const snackBar = SnackBar(content: Text('老孟'));
Scaffold.of(context).showSnackBar(snackBar);
}
}
点击按钮后,会提示异常信息:Scaffold.of() called with a context that does not contain a Scaffold.。
这是因为你传入的BuildContext是当前parent Widget的,不是Scaffold的,所以会报错。
解决
可以借助Builder组件解决。
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Builder组件"),
),
body: Builder(
builder: (context) {
return Center(
child: RaisedButton(
color: Colors.red,
textColor: Colors.white,
onPressed: () {
showSnackBar(context);
},
child: const Text('show SnackBar'),
),
);
}
),
);
}
FutureBuilder组件
展示异步任务状态,当有一个Future异步任务需要展示给用户时,可以使用FutureBuilder组件来实现。

final Future<String> _futureSuccess = Future.delayed(const Duration(seconds: 3), () {
return "成功了";
});
final Future<dynamic> _futureError = Future.delayed(const Duration(seconds: 3), () {
return Future.error("失败了");
});
String _msg = "加载中";
String _msg2 = "加载中";
FutureBuilder(
future: _futureSuccess,
builder: (context, snapshot) {
late Widget widget;
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasError) {
widget = const Icon(Icons.error, color: Colors.red, size: 50);
_msg = snapshot.error.toString();
} else {
widget = const Icon(Icons.check_circle, color: Colors.green, size: 50);
_msg = snapshot.data.toString();
}
} else {
widget = const CircularProgressIndicator();
_msg = "加载中";
}
return Container(
height: 100,
width: 100,
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
borderRadius: const BorderRadius.all(Radius.circular(10)),
),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
widget,
Text(_msg),
],
),
),
);
},
),
FutureBuilder(
future: _futureError,
builder: (context, snapshot) {
late Widget widget;
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasError) {
widget = const Icon(Icons.error, color: Colors.red, size: 50);
_msg2 = snapshot.error.toString();
} else {
widget = const Icon(Icons.check_circle, color: Colors.green, size: 50);
_msg2 = snapshot.data.toString();
}
} else {
widget = const CircularProgressIndicator();
_msg2 = "加载中";
}
return Container(
height: 100,
width: 100,
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
borderRadius: const BorderRadius.all(Radius.circular(10)),
),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
widget,
Text(_msg2),
],
),
),
);
},
)
边栏推荐
- ICML 2022 | ByteDance AI Lab proposes a multimodal model: x-vlm, learning multi granularity alignment of vision and language
- 【Harmony OS】【ArkUI】ets开发 图形与动画绘制
- Mstp+vrrp+ospf implements a three-tier architecture
- Time management understood after working at home | community essay solicitation
- Configuration source code
- Development of trading system (VIII) -- Construction of low delay network
- Peking University has a new president! Gongqihuang, academician of the Chinese Academy of Sciences, took over and was admitted to the Physics Department of Peking University at the age of 15
- 佐喃社区
- Qt编译数据库插件通用步骤说明
- MySQL modifies and deletes tables in batches according to the table prefix
猜你喜欢

用CPU方案打破内存墙?学PayPal堆傲腾扩容量,漏查欺诈交易量可降至1/30

Amazon's other side in China

Cesium 拖拽3D模型

La gamme NFT Color, qui représente la diversité, est en ligne sur la plate - forme du marché Sandbox

Xidian AI ranked higher than Qingbei in terms of AI majors, and Nantah ranked the first in China in 2022 in terms of soft science majors

Development of trading system (VII) -- Analysis of trading delay

Dr. Sun Jian was commemorated at the CVPR conference. The best student thesis was awarded to Tongji Ali. Lifeifei won the huangxutao Memorial Award

(超详细onenet TCP协议接入)arduino+esp8266-01s接入物联网平台,上传实时采集数据/TCP透传(以及lua脚本如何获取和编写

DAP数据调度功能完善说明

《Missing Parts》NFT 作品集第 5 系列上线 The Sandbox 市场平台
随机推荐
Development of trading system (x) -- fix agreement
How to use ide to automatically sign and debug Hongmeng application
Work assessment of pharmaceutical polymer materials of Jilin University in March of the 22nd spring -00025
AI quantitative transaction (I) -- Introduction to quantitative transaction
Maintenant, les oreilles vont entrer dans le métacosme.
X86 CPU, critical! The latest vulnerability has caused heated discussion. Hackers can remotely steal keys. Intel "all processors" are affected
Development of trading system (VII) -- Analysis of trading delay
Development of trading system (II) -- market data
windows 2003 64位系统php运行报错:1% 不是有效的 win32 应用程序
MySQL根据表前缀批量修改、删除表
Understand (DI) dependency injection in PHP
Hello CTP (II) -- Introduction to CTP
Development of trading system (I) -- Introduction to trading system
Development of trading system (XII) - Official quickfix document
ICML 2022 | ByteDance AI Lab proposes a multimodal model: x-vlm, learning multi granularity alignment of vision and language
老叶的祝福
威马招股书拆解:电动竞争已结束,智能排位赛刚开始
Now, the ear is going into the metauniverse
Does it count as staying up late to sleep at 2:00 and get up at 10:00? Unless you can do it every day
Work assessment of Biopharmaceutics of Jilin University in March of the 22nd spring -00005