当前位置:网站首页>Flutter中是使用RxDart代替Stateful
Flutter中是使用RxDart代替Stateful
2022-07-28 14:39:00 【我不是码神】
本文先列举一个最简单的例子,以后遇到比较典型的例子再分享:
先看一个常规的写法,首页上面有两个tab的例子:
class MainTabPage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return MainTabPageState();
}
}
class MainTabPageState extends State<MainTabPage> {
int _currentIndex = 0;
final pageList = [
TabInfo(Home(), "首页", Icons.home),
TabInfo(Text(""), "我的", Icons.person)
];
@override
Widget build(BuildContext context) {
return DefaultTabController(
child: Scaffold(
backgroundColor: Color(0xFFF5F5F5),
bottomNavigationBar: BottomNavigationBar(
items: pageList.map((info) => BottomNavigationBarItem(
title: Text(info.title), icon: Icon(info.icon))).toList(),
currentIndex: _currentIndex,
type: BottomNavigationBarType.fixed,
onTap: (int index) {
setState(() {
_currentIndex = index;
});
},
),
body: pageList[_currentIndex].widget),
length: pageList.length);
}
}其中_currentIndex记录了当前tab的索引,onTap中调用setState来更新视图,没毛病。
现在使用RxDart改写这个页面。
class MainTabPage extends StatelessWidget {
final pageList = [
TabInfo(Home(), "首页", Icons.home),
TabInfo(Text(""), "我的", Icons.person)
];
final changeTabStream = PublishSubject<int>();
@override
Widget build(BuildContext context) => DefaultTabController(
child: StreamBuilder<int>(
initialData: 0,
stream: changeTabStream,
builder: (context, snapshot) => Scaffold(
backgroundColor: Color(0xFFF5F5F5),
bottomNavigationBar: BottomNavigationBar(
items: pageList
.map((info) => BottomNavigationBarItem(
title: Text(info.title), icon: Icon(info.icon)))
.toList(),
currentIndex: snapshot.data,
type: BottomNavigationBarType.fixed,
onTap: changeTabStream.add,
),
body: pageList[snapshot.data].widget,
)),
length: pageList.length,
);
}我们看到不需要再定义_currentIndex,然后我们只需要继承StatelessWidget 。取而代之的是我们定义了changeTabStream,这是一个PublishSubject用来发送事件,Flutter官方提供了StreamBuilder来响应这种事件流,很贴心。
这个例子还看不出Rx的明显优势,但是大家可以思考一下,Rx的好处在哪里。
边栏推荐
- 数牍 X Rust,那些不得不说的事
- 迟来的2021年终总结
- Stateflow逻辑系统建模
- Daily news on July 28, 2022: Science: AI has made another breakthrough in protein design, and can design specific functional proteins
- try...except异常处理语句(6)
- 有道云笔记去除底部广告
- 使用Mock技术帮助提升测试效率的小tips,你知道几个?
- 9、相关数据累积任务定义
- Share the HR experience of the first and second tier companies
- VirturalBox解决kernel driver问题
猜你喜欢

EasyExcel复杂表头导出(一对多)

How many tips do you know about using mock technology to help improve test efficiency?

Execution process of SQL statement

生命的感悟

Endnote is associated with word

Deepfacelab model parameters collection

Introduction to grpc

机器学习的3大“疑难杂症”,因果学习是突破口 | 重庆大学刘礼

如何获取及嵌入Go二进制执行包信息

软件架构与设计(十)-----架构技术
随机推荐
给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。
Regular expression (4)
Baidu proposes a dynamic self distillation method to realize dense paragraph retrieval by combining interactive model and double tower model
How many tips do you know about using mock technology to help improve test efficiency?
How to write a JMeter script common to the test team
使用Mock技术帮助提升测试效率的小tips,你知道几个?
软件架构与设计(六)-----层次结构体
VS使用技巧
软件架构与设计(二)-----架构模型
如何获取及嵌入Go二进制执行包信息
[leetcode] binary search given an N-element ordered (ascending) integer array num and a target value target, write a function to search the target in num. if the target value exists, return the subscr
MIT pointed out that the public pre training model should not be used indiscriminately
CANoe使用教程
Pycharm - output exception of program run and default comment of added function
为赴美上市做准备?Arm宣布将剥离物联网服务业务:未来将聚焦芯片底层设计
[delete specified number leetcode]
20. Channel allocation task implementation
正则表达式(4)
Canoe tutorial
Rongyun real-time community solution