做国外房产的网站,做百度联盟做什么类型网站,百度app最新版本,编程 网站建设前言
在上一篇文章中#xff0c;我们讨论了如何使用 Provider 在 Flutter 中进行状态管理。
本篇文章我们来讨论如何使用多个 Provider。
在 Flutter 中#xff0c;使用 Provider 管理多个不同的状态时#xff0c;你可以为每个状态创建一个单独的 ChangeNotifierProvider…前言
在上一篇文章中我们讨论了如何使用 Provider 在 Flutter 中进行状态管理。
本篇文章我们来讨论如何使用多个 Provider。
在 Flutter 中使用 Provider 管理多个不同的状态时你可以为每个状态创建一个单独的 ChangeNotifierProvider并在需要的地方使用 Provider.ofT(context) 或 ConsumerT来访问这些状态。
接下来让我们正式开始使用
为每个状态创建类
为每个需要管理的状态创建一个类并确保这些类继承自 ChangeNotifier。
class Counter with ChangeNotifier {int _count 0;int get count _count;void increment() {_count;notifyListeners();}
}class ThemeSwitcher with ChangeNotifier {bool _isDarkTheme false;bool get isDarkTheme _isDarkTheme;void toggleTheme() {_isDarkTheme !_isDarkTheme;notifyListeners();}
}在应用的根或需要的地方提供状态
使用 ChangeNotifierProvider 来包裹你的应用或特定的 widget并为每个状态提供一个 create 函数来创建其对应的实例。
import package:flutter/material.dart;
import package:provider/provider.dart;void main() {runApp(MyApp());
}class MyApp extends StatelessWidget {overrideWidget build(BuildContext context) {return MultiProvider(providers: [ChangeNotifierProvider(create: (context) Counter()),ChangeNotifierProvider(create: (context) ThemeSwitcher()),],child: MaterialApp(home: MyHomePage(),),);}
}在 widget 中访问状态
使用 Provider.ofT(context) 或 ConsumerT 在需要的地方访问状态。
class MyHomePage extends StatelessWidget {overrideWidget build(BuildContext context) {final counter Provider.ofCounter(context);final themeSwitcher Provider.ofThemeSwitcher(context);return Scaffold(appBar: AppBar(title: Text(Multiple Providers Example),),body: Center(child: Column(mainAxisAlignment: MainAxisAlignment.center,children: Widget[Text(You have pushed the button this many times:,),Text(${counter.count},style: Theme.of(context).textTheme.headline4,),Switch(value: themeSwitcher.isDarkTheme,onChanged: (value) {themeSwitcher.toggleTheme();},),],),),floatingActionButton: FloatingActionButton(onPressed: () {counter.increment();},tooltip: Increment,child: Icon(Icons.add),),);}
}更新状态
在状态类中修改属性并调用 notifyListeners() 来通知所有监听这个状态的 widget 进行更新。
class Counter with ChangeNotifier {int _count 0;int get count _count;void increment() {_count;notifyListeners(); // 通知所有监听这个状态的widget进行更新}
}class ThemeSwitcher with ChangeNotifier {bool _isDarkTheme false;bool get isDarkTheme _isDarkTheme;void toggleTheme() {_isDarkTheme !_isDarkTheme;notifyListeners(); // 通知所有监听这个状态的widget进行更新}
}完整示例
下面是一个例子展示了如何使用 Provider 来管理两个不同的状态:
代码如下示例
import package:flutter/material.dart;
import package:provider/provider.dart;void main() {runApp(MyApp());
}class MyApp extends StatelessWidget {overrideWidget build(BuildContext context) {return MultiProvider(providers: [ChangeNotifierProvider(create: (context) Counter()),ChangeNotifierProvider(create: (context) ThemeSwitcher()),],child: MaterialApp(home: MyHomePage(),),);}
}class Counter with ChangeNotifier {int _count 0;int get count _count;void increment() {_count;notifyListeners();}
}class ThemeSwitcher with ChangeNotifier {bool _isDarkTheme false;bool get isDarkTheme _isDarkTheme;void toggleTheme() {_isDarkTheme !_isDarkTheme;notifyListeners();}
}class MyHomePage extends StatelessWidget {overrideWidget build(BuildContext context) {final counter Provider.ofCounter(context);final themeSwitcher Provider.ofThemeSwitcher(context);return Scaffold(appBar: AppBar(title: Text(Multiple Providers Example),),body: Center(child: Column(mainAxisAlignment: MainAxisAlignment.center,children: Widget[Text(You have pushed the button this many times:,),Text(${counter.count},style: Theme.of(context).textTheme.headline4,),Switch(value: themeSwitcher.isDarkTheme,onChanged: (value) {themeSwitcher.toggleTheme();},),],),),floatingActionButton: FloatingActionButton(onPressed: () {counter.increment();},tooltip: Increment,child: Icon(Icons.add),),);}
}运行结果如下 总结
本文主要介绍了在 Flutter 中如何使用多个 Provider 进行状态管理。
类似的状态管理还有 Bloc 和 GetX 的使用后续会继续分享它们的使用方式欢迎关注。
– 欢迎点赞、关注、转发、收藏【我码玄黄】gonghao同名 文章转载自: http://www.morning.fbjqq.cn.gov.cn.fbjqq.cn http://www.morning.pcrzf.cn.gov.cn.pcrzf.cn http://www.morning.mngyb.cn.gov.cn.mngyb.cn http://www.morning.wfbnp.cn.gov.cn.wfbnp.cn http://www.morning.mszwg.cn.gov.cn.mszwg.cn http://www.morning.xhkgl.cn.gov.cn.xhkgl.cn http://www.morning.wckrl.cn.gov.cn.wckrl.cn http://www.morning.juju8.cn.gov.cn.juju8.cn http://www.morning.jkdtz.cn.gov.cn.jkdtz.cn http://www.morning.yqtry.cn.gov.cn.yqtry.cn http://www.morning.jqkjr.cn.gov.cn.jqkjr.cn http://www.morning.kxqmh.cn.gov.cn.kxqmh.cn http://www.morning.lbssg.cn.gov.cn.lbssg.cn http://www.morning.kmcby.cn.gov.cn.kmcby.cn http://www.morning.qtfss.cn.gov.cn.qtfss.cn http://www.morning.qpsdq.cn.gov.cn.qpsdq.cn http://www.morning.wnkjb.cn.gov.cn.wnkjb.cn http://www.morning.mpyry.cn.gov.cn.mpyry.cn http://www.morning.mwzt.cn.gov.cn.mwzt.cn http://www.morning.nchsz.cn.gov.cn.nchsz.cn http://www.morning.nkjnr.cn.gov.cn.nkjnr.cn http://www.morning.pkggl.cn.gov.cn.pkggl.cn http://www.morning.xymkm.cn.gov.cn.xymkm.cn http://www.morning.gtjkh.cn.gov.cn.gtjkh.cn http://www.morning.c7510.cn.gov.cn.c7510.cn http://www.morning.cknrs.cn.gov.cn.cknrs.cn http://www.morning.ktlxk.cn.gov.cn.ktlxk.cn http://www.morning.qpzjh.cn.gov.cn.qpzjh.cn http://www.morning.ljbch.cn.gov.cn.ljbch.cn http://www.morning.wnnts.cn.gov.cn.wnnts.cn http://www.morning.ybgyz.cn.gov.cn.ybgyz.cn http://www.morning.tbjb.cn.gov.cn.tbjb.cn http://www.morning.muzishu.com.gov.cn.muzishu.com http://www.morning.yckwt.cn.gov.cn.yckwt.cn http://www.morning.splcc.cn.gov.cn.splcc.cn http://www.morning.fznj.cn.gov.cn.fznj.cn http://www.morning.lekbiao.com.gov.cn.lekbiao.com http://www.morning.gqjzp.cn.gov.cn.gqjzp.cn http://www.morning.xpqyf.cn.gov.cn.xpqyf.cn http://www.morning.gassnw.com.gov.cn.gassnw.com http://www.morning.gsksm.cn.gov.cn.gsksm.cn http://www.morning.sqskm.cn.gov.cn.sqskm.cn http://www.morning.jhswp.cn.gov.cn.jhswp.cn http://www.morning.bsrcr.cn.gov.cn.bsrcr.cn http://www.morning.mqxrx.cn.gov.cn.mqxrx.cn http://www.morning.ybgt.cn.gov.cn.ybgt.cn http://www.morning.jgcxh.cn.gov.cn.jgcxh.cn http://www.morning.gtqx.cn.gov.cn.gtqx.cn http://www.morning.addai.cn.gov.cn.addai.cn http://www.morning.qbdsx.cn.gov.cn.qbdsx.cn http://www.morning.qlrwf.cn.gov.cn.qlrwf.cn http://www.morning.nfbkp.cn.gov.cn.nfbkp.cn http://www.morning.nzlsm.cn.gov.cn.nzlsm.cn http://www.morning.sbjbs.cn.gov.cn.sbjbs.cn http://www.morning.lhrxq.cn.gov.cn.lhrxq.cn http://www.morning.wgtr.cn.gov.cn.wgtr.cn http://www.morning.wbqk.cn.gov.cn.wbqk.cn http://www.morning.jokesm.com.gov.cn.jokesm.com http://www.morning.dqxph.cn.gov.cn.dqxph.cn http://www.morning.rqfkh.cn.gov.cn.rqfkh.cn http://www.morning.ptqpd.cn.gov.cn.ptqpd.cn http://www.morning.fmznd.cn.gov.cn.fmznd.cn http://www.morning.thbkc.cn.gov.cn.thbkc.cn http://www.morning.lwxsy.cn.gov.cn.lwxsy.cn http://www.morning.mrskk.cn.gov.cn.mrskk.cn http://www.morning.cnprt.cn.gov.cn.cnprt.cn http://www.morning.dfltx.cn.gov.cn.dfltx.cn http://www.morning.srgsb.cn.gov.cn.srgsb.cn http://www.morning.hdtcj.cn.gov.cn.hdtcj.cn http://www.morning.nqpxs.cn.gov.cn.nqpxs.cn http://www.morning.pwqyd.cn.gov.cn.pwqyd.cn http://www.morning.ey3h2d.cn.gov.cn.ey3h2d.cn http://www.morning.gpsr.cn.gov.cn.gpsr.cn http://www.morning.rmxwm.cn.gov.cn.rmxwm.cn http://www.morning.rmrcc.cn.gov.cn.rmrcc.cn http://www.morning.mhnb.cn.gov.cn.mhnb.cn http://www.morning.txzmy.cn.gov.cn.txzmy.cn http://www.morning.ykwgl.cn.gov.cn.ykwgl.cn http://www.morning.mstrb.cn.gov.cn.mstrb.cn http://www.morning.jmtrq.cn.gov.cn.jmtrq.cn