当前位置:网站首页>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的好处在哪里。
边栏推荐
- Explain the difference set, intersection set and union set of complex type set in detail.Net
- 融云实时社区解决方案
- 有奖活动分享:使用WordPress搭建一个专属自己的博客后最高可领取iPhone13
- 软件架构与设计(八)-----分布式架构
- Camera连拍自动化测试shell脚本
- 软件架构与设计(十)-----架构技术
- 爬虫入门(1)——requests(1)
- Sharing of award-winning activities: you can get up to iphone13 after using WordPress to build your own blog
- Regular expression (4)
- How many tips do you know about using mock technology to help improve test efficiency?
猜你喜欢

Vs dynamic library debugging

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

Explain the difference set, intersection set and union set of complex type set in detail.Net

How many of the top ten test tools in 2022 do you master

Tencent interview -- please design a thread pool to implement sequential execution

软件架构与设计(十)-----架构技术

生命的感悟
![[delete specified number leetcode]](/img/16/b40492d8414a363a3a24f00b4afd47.png)
[delete specified number leetcode]

爬虫入门(1)——requests(1)

Endnote 与word关联
随机推荐
Summary and arrangement of postgraduate entrance examination information of 985 colleges and universities nationwide
软件架构与设计(一)-----关键原则
samba服务器搭建指南
位运算的一些操作
2、开源GPS项目HD-GR GNSS的自叙
monkey压力测试
Framework定制系列(一)-----SystemUI NavigationBar导航栏上滑返回Launcher
20. Channel allocation task implementation
About the pictures inserted in the word document, only the following part is displayed
1. Author of the open source GPS project hd-gr GNSS
Common methods of qcustomplot drawing tools
22、电文处理任务实现
数牍 X Rust,那些不得不说的事
为赴美上市做准备?Arm宣布将剥离物联网服务业务:未来将聚焦芯片底层设计
Grpc protocol buffer
软件架构与设计(九)-----基于组件的架构
Gfpgan blurred photo repair artifact
Qt信号与槽的五种连接方式
如何通过adb打开和关闭飞行模式
String (3)