当前位置:网站首页>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),
],
),
),
);
},
)
边栏推荐
- Development of trading system (I) -- Introduction to trading system
- Maintenant, les oreilles vont entrer dans le métacosme.
- Crawler grabs the idea of reading on wechat
- [team learning] SQL programming language notes - task04
- 完美洗牌问题
- Configuration source code
- PHP代码审计2—这些函数必知必会
- Hello CTP (III) - CTP quotation API
- windows 2003 64位系统php运行报错:1% 不是有效的 win32 应用程序
- What is the difference between learning code, rolling code and fixed code? The number of repeated codes, coding capacity and the principle of rolling code
猜你喜欢

Musk: Twitter should learn from wechat and make 1billion people "live on it" into a super app

BSC parsing input data of transaction

居家办公之后才明白的时间管理 | 社区征文

cesium 图形标注圆形、正方形、多边形、椭圆等

Development of trading system (III) - risk control system

Maybe it's the wrong reason

代錶多樣性的彩色 NFT 系列上線 The Sandbox 市場平臺

client-go gin的简单整合十-Update

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

数学分析_笔记_第3章:极限
随机推荐
opencv 红色区域在哪里?
uniapp 制作手机app程序, 使用uni.chooseVideo录制视频,视频播放模糊分辨率低的原因
OpenSUSE environment PHP connection Oracle
Development of trading system (XII) - Official quickfix document
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
佐喃社区
DAP数据调度功能完善说明
9 necessary soft skills for program ape career development
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
Mstp+vrrp+ospf implements a three-tier architecture
Development of trading system (II) -- market data
[rust contribution] implement Message Oriented Middleware (6) -client from zero
Configuration source code
The problem that only the home page can be accessed under openSUSE Apache laravel
DevEco Studio 3.0编辑器配置技巧篇
Wuenda, the new course of machine learning is coming again! Free auditing, Xiaobai friendly
Crawler grabs the data of Douban group
Tensorflow, danger! Google itself is the one who abandoned it
Solution to the problem that Linux crontab timed operation Oracle does not execute (crontab environment variable problem)
2022-06-21-Flink-49(一. SQL手册)