• 自学之路Flutter使用Provider进行状态管理


    使用前的准备

    首先在pubspec.yaml中配置,然后pub get,等待安装完成

    我们首先创建两个比较简单的控制器,测试页面跳转之间的数据传递。

    1. import 'package:flutter/material.dart';
    2. void main() {
    3. runApp(const MyApp());
    4. }
    5. class MyApp extends StatelessWidget {
    6. const MyApp({Key? key}) : super(key: key);
    7. @override
    8. Widget build(BuildContext context) {
    9. return MaterialApp(
    10. title: 'Flutter Demo',
    11. theme: ThemeData(
    12. primarySwatch: Colors.blue,
    13. ),
    14. home: const FirstPage(),
    15. );
    16. }
    17. }
    18. class FirstPage extends StatelessWidget {
    19. const FirstPage({Key? key}) : super(key: key);
    20. @override
    21. Widget build(BuildContext context) {
    22. return Scaffold(
    23. appBar: AppBar(title: Text('first page')),
    24. body: Center(
    25. child: Text('first page count='),
    26. ),
    27. floatingActionButton: FloatingActionButton(
    28. child: const Icon(Icons.polymer),
    29. onPressed: () => Navigator.of(context)
    30. .push(MaterialPageRoute(builder: (context) => SecondPage())),
    31. ),
    32. );
    33. }
    34. }
    35. class SecondPage extends StatelessWidget {
    36. const SecondPage({Key? key}) : super(key: key);
    37. @override
    38. Widget build(BuildContext context) {
    39. return Scaffold(
    40. appBar: AppBar(title: Text('second page')),
    41. body: Center(
    42. child: Text('Second page count='),
    43. ),
    44. floatingActionButton: FloatingActionButton(
    45. child: const Icon(Icons.add),
    46. onPressed: () => print('+++'),
    47. ),
    48. );
    49. }
    50. }

    实现要求:点击FirstPage按钮跳转到SecondPage在第二个页面修改值,然后的第一个页面的值也能相应的改变

    数据关联模型创建

    定义数据模型,通过混入 ChangeNotifier 管理监听者(通知模式),暴露读写方法,数据改变的时候通知监听者刷新

    1. // 定义数据模型,通过混入 ChangeNotifier 管理监听者(通知模式)
    2. class CountModel with ChangeNotifier {
    3. int _count = 0;
    4. // 读方法
    5. int get counter => _count;
    6. // 写方法
    7. void increment() {
    8. _count++;
    9. notifyListeners(); // 通知监听者刷新
    10. }
    11. }
    数据包装

    这里我们在根视图这里包装数据,这样子层级都可以使用。这里使用的是ChangeNotifierProvider.value指定Value

    1. class MyApp extends StatelessWidget {
    2. const MyApp({Key? key}) : super(key: key);
    3. @override
    4. Widget build(BuildContext context) {
    5. return ChangeNotifierProvider.value(
    6. value: CountModel(),
    7. child: MaterialApp(
    8. title: 'Flutter Demo',
    9. theme: ThemeData(
    10. primarySwatch: Colors.blue,
    11. ),
    12. home: const FirstPage(),
    13. ));
    14. }
    15. }
    数据使用

    首先取出模型数据Provider.of(context),然后就可以通过读方法和写方法来操作,还是比较简便的。

    1. @override
    2. Widget build(BuildContext context) {
    3. final _counter = Provider.of(context);
    4. return Scaffold(
    5. appBar: AppBar(title: Text('first page')),
    6. body: Center(
    7. child: Text('first page count=${_counter.counter}'),
    8. ),
    9. floatingActionButton: FloatingActionButton(
    10. child: const Icon(Icons.polymer),
    11. onPressed: () => Navigator.of(context)
    12. .push(MaterialPageRoute(builder: (context) => SecondPage())),
    13. ),
    14. );
    15. }

    这里有一个优化的地方,在我们修改数据的时候会重新走Build方法来渲染当前的页面,这里会涉及到多次渲染的问题,所以我们需要控制Flutter Widget的刷新粒度来达到优化性能的目的。

    Flutter中文网

    Flutter开发

  • 相关阅读:
    【WSL】单机大模型前的基础环境配置
    R语言在vector向量数据末尾追加新的元素(在已知向量末尾添加单个标量数据形成新的向量)
    uniapp地图手动控制地图scale
    基于科大讯飞AIGC创作平台,构建数字人虚拟主播
    JS前端开发框架常用的有哪些?
    栈与队列的简单实现(stack and queue)
    安防监控视频云存储平台EasyNVR对接EasyNVS时,一直不上线该如何解决?
    【RuoYi移动端】uni-app中实现生成二维码功能(代码示例)
    JAVA计算机毕业设计大学生兼职招聘网站计算机(附源码、数据库)
    SpringCloud笔记之入门
  • 原文地址:https://blog.csdn.net/weixin_44619684/article/details/139289420