当前位置:网站首页>Flutter Builder & futurebuilder components
Flutter Builder & futurebuilder components
2022-06-25 04:10:00 【xiangxiongfly915】
List of articles
Flutter Builder & FutureBuilder Components
Builder Components
Builder Components act as closures , You can limit the scope of use .
problem
class BuilderPage extends StatelessWidget {
const BuilderPage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Builder Components "),
),
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(' Lao Meng '));
Scaffold.of(context).showSnackBar(snackBar);
}
}
After clicking the button , An exception message will be prompted :Scaffold.of() called with a context that does not contain a Scaffold..
This is because you sent in BuildContext It is the present. parent Widget Of , No Scaffold Of , So there's an error .
solve
Can use Builder Component solving .
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Builder Components "),
),
body: Builder(
builder: (context) {
return Center(
child: RaisedButton(
color: Colors.red,
textColor: Colors.white,
onPressed: () {
showSnackBar(context);
},
child: const Text('show SnackBar'),
),
);
}
),
);
}
FutureBuilder Components
Show asynchronous task status , When you have one Future When an asynchronous task needs to be presented to the user , have access to FutureBuilder Component to implement .

final Future<String> _futureSuccess = Future.delayed(const Duration(seconds: 3), () {
return " succeed ";
});
final Future<dynamic> _futureError = Future.delayed(const Duration(seconds: 3), () {
return Future.error(" failed ");
});
String _msg = " Loading ";
String _msg2 = " Loading ";
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 = " Loading ";
}
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 = " Loading ";
}
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),
],
),
),
);
},
)
边栏推荐
- 9 necessary soft skills for program ape career development
- Development of trading system (VI) -- HFT high frequency trading
- 【LeetCode】143. Rearrange linked list
- [proteus simulation] Arduino uno key controls the flashing increase / decrease display of nixie tube
- 学习码 滚动码 固定码 有什么区别重码数,编码容量滚动码的原理
- How to use ide to automatically sign and debug Hongmeng application
- How to use crawlers to capture bullet screen and comment data of station B?
- Serious PHP defects can lead to rce attacks on QNAP NAS devices
- 虽然传统意义上的互联网早已不复存在,但这并不代表互联网早已消失不再
- Color NFT series representing diversity launched on the sandbox market platform
猜你喜欢

ZABBIX installation pit avoidance Guide

9 necessary soft skills for program ape career development

Development of trading system (I) -- Introduction to trading system

Crawler grabs the idea of reading on wechat

"Comment positionner l'industrie" dans la planification industrielle locale / parc

opencv 红色区域在哪里?

严重的PHP缺陷可导致QNAP NAS 设备遭RCE攻击

Development of trading system (VI) -- HFT high frequency trading

How to quickly deliver high-value software

代錶多樣性的彩色 NFT 系列上線 The Sandbox 市場平臺
随机推荐
Watch out for the stolen face! So many risks of face recognition used every day?
Jilin University 22 spring March "official document writing" assignment assessment-00029
Development of trading system (II) -- market data
Jilin University 22 spring March "official document writing" assignment assessment-00034
佐喃社区
Serious PHP defects can lead to rce attacks on QNAP NAS devices
2D 照片变身 3D 模型,来看英伟达的 AI 新“魔法”!
Development of trading system (VI) -- HFT high frequency trading
Development of trading system (V) -- Introduction to Sinovel counter
The 5th series of NFT works of missing parts was launched on the sandbox market platform
2. play the chromatic harmonica
@RequestBody解决获取参数为null
1280_C语言求两个无符号整形的平均值
"Renaissance" in the digital age? The bottom digital collection makes people happy and sad
Cesium 拖拽3D模型
Development of trading system (III) - risk control system
numpy np tips:使用opencv对数组插值放缩到固定形状 cv2.resize(res, dsize=(64, 64), interpolation=cv2.INTER_CUBIC)
《悉达多》:一生之书,可以时常反刍
[harmony OS] [arkui] ETS development graphics and animation drawing
Mysql的order by