• 自学之路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开发

  • 相关阅读:
    ICPC World Finals 2020 ‘S No Problem (树形dp) (k 条不相交路径覆盖最值问题)
    到底选择Vite还是Webpack?
    2.3 为何使用Pthreads
    css:img引入svg后修改颜色
    公开的mqtt服务器如何获得等问题
    每日刷题记录 (九)
    漏洞复现----41、Spring Data Rest 远程命令执行漏洞(CVE-2017-8046)
    用HTML+CSS做一个漂亮简单的花店网页【免费的学生网页设计成品】
    gradle使用笔记整理
    阿里内部目前最完整“Spring全线笔记”,不止是全家桶,太完整了
  • 原文地址:https://blog.csdn.net/weixin_44619684/article/details/139289420