網(wǎng)站首頁 編程語言 正文
本文主要介紹flutter中狀態(tài)管理組件provider,provider: ^6.0.3主要是用于我們系統(tǒng)InheritedWidge的封裝,用于數(shù)據(jù)的傳遞和管理。
1. provider的使用
網(wǎng)上也有很多關(guān)于provider說明,也可以看下官方的provider的 README。這里我記錄一下我自己學(xué)習(xí)。
我們對(duì)于簡單的數(shù)據(jù)共享可以設(shè)置參數(shù),之后子頁面進(jìn)行數(shù)據(jù)方法回調(diào),從而完成數(shù)據(jù)間的通信。但是比較麻煩,下面我們看下我們使用provider如何達(dá)到這個(gè)效果。
我們2個(gè)頁面使用同一個(gè)數(shù)據(jù),在第二個(gè)頁面使用點(diǎn)擊增加方法。之后返回在第一個(gè)頁面也顯示出增加后的數(shù)據(jù)count達(dá)到同步的效果。
點(diǎn)擊增加
看下代碼實(shí)現(xiàn)首先是使用StatelessWidget來顯示頁面,簡單的頁面跳轉(zhuǎn)就不展示了。我們定義一個(gè)model用來存儲(chǔ)我們的count,我么混入通過混入 ChangeNotifier 管理監(jiān)聽者(通知模式)。我們寫讀數(shù)據(jù),并且當(dāng)數(shù)據(jù)發(fā)生改變的時(shí)候,使用通知更新數(shù)據(jù)。
class CountModel with ChangeNotifier {
int _count = 0;
// 讀方法
int get counter => _count;
// 寫方法
void increment() {
_count++;
notifyListeners(); // 通知監(jiān)聽者刷新
}
}
我們2個(gè)頁面都要使用這個(gè)數(shù)據(jù),我們要把這個(gè)數(shù)據(jù)放在這2個(gè)子頁面的父節(jié)點(diǎn)上。通過 Provider 組件封裝數(shù)據(jù)資源,value就是需要共享的數(shù)據(jù)資源
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
// 通過 Provider 組件封裝數(shù)據(jù)資源
return ChangeNotifierProvider.value(
value: CountModel(), // 需要共享的數(shù)據(jù)資源
child: MaterialApp(
home: FirstPage(),
));
}
}
我們?cè)陧撁媸褂玫牡胤竭M(jìn)行取數(shù)據(jù)
final _counter = Provider.of<CountModel>(context);
上下文就包含了我們父節(jié)點(diǎn)中設(shè)置的value 即CountModel()。
// 第一個(gè)頁面
class FirstPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
// 取出數(shù)據(jù)
final _counter = Provider.of<CountModel>(context);
return Scaffold(
appBar: AppBar(
title: const Text('第一個(gè)頁面'),
),
body: Center(
child: Text('第一個(gè)頁面count:${_counter.counter}'),
),
// 跳轉(zhuǎn)到 SecondPage
floatingActionButton: FloatingActionButton(
child: const Icon(Icons.next_plan),
onPressed: () => Navigator.of(context)
.push(MaterialPageRoute(builder: (context) => SecondPage()))));
}
}
// 第二個(gè)頁面
class SecondPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
// 取出數(shù)據(jù)
final _counter = Provider.of<CountModel>(context);
return Scaffold(
appBar: AppBar(
title: const Text('第二個(gè)頁面'),
),
// 展示資源中的數(shù)據(jù)
body: Center(
child: Text('第二個(gè)頁面count:${_counter.counter}'),
),
floatingActionButton: FloatingActionButton(
onPressed: _counter.increment,
child: const Icon(Icons.add),
),
);
}
}
通過provider很簡單的達(dá)到了我們的數(shù)據(jù)共享,同時(shí)通過通知讓我們的頁面進(jìn)行了刷新。
2. 控制Widget的刷新顆粒度
我們上面的頁面中涉及的Widget變化也就是一個(gè)文字的展示,沒有必要整個(gè)頁面都刷新,這樣可以提高我們的性能。
我們把我們Icon換成自定義的,可以發(fā)現(xiàn)當(dāng)我們點(diǎn)擊的時(shí)候就會(huì)一直重新build,實(shí)際我們不需要他們變化,那么我們只要我們那個(gè)Widget使用了數(shù)據(jù),我們刷新該Widget即可。我們就要使用Consumer
final Widget Function(
BuildContext context,
T value,
Widget? child,
) builder;
定義child為我們包裹的widget,T為我們model的類型。
body: Center(
child: Consumer<CountModel>(
builder: (context,CountModel counter,_) => Text('第二個(gè)頁面count:${counter.counter}'),
),
),
這里我們的child為空直接每次刷新Consumer的Widget。那么我們的button怎么刷新呢
floatingActionButton: Consumer<CountModel>(
builder: (context, CountModel counter, child) => FloatingActionButton(
onPressed: counter.increment,
child: child,
),
child: MyIcon(),
)
這里我們使用child屬性,把我們的builder隔離開,這樣我們就可以是我們的數(shù)據(jù)和視圖做到隔離效果。
3. 小結(jié)
對(duì)于數(shù)據(jù)共享和數(shù)據(jù)傳遞provider組件確實(shí)提供了我們快捷的方式,我們?cè)谑褂玫倪^程中要注意provider組件位于父節(jié)點(diǎn)位置,這樣子節(jié)點(diǎn)才能共享數(shù)據(jù)狀態(tài),其次我們盡可能的減少我們刷新的顆粒度,最好在使用數(shù)據(jù)的地方進(jìn)行刷新組件。
原文鏈接:https://juejin.cn/post/7111335230358683684
相關(guān)推薦
- 2022-09-14 前端使用svg圖片改色實(shí)現(xiàn)示例_其它綜合
- 2022-04-05 svn使用命令忽略指定目錄 svn propset svn:ignore “要忽略的目錄“ .
- 2022-03-29 C#使用Twain協(xié)議實(shí)現(xiàn)掃描儀連續(xù)掃描功能_C#教程
- 2022-06-16 使用golang如何優(yōu)雅的關(guān)機(jī)或重啟操作示例_Golang
- 2023-01-03 C++特性之智能指針shared_ptr詳解_C 語言
- 2022-06-13 C語言指針超詳細(xì)講解上篇_C 語言
- 2023-01-05 C++模板?index_sequence使用示例詳解_C 語言
- 2021-04-07 Websocket免費(fèi)在線測(cè)試請(qǐng)求工具
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支