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

使用前的准备

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

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

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const FirstPage(),
    );
  }
}

class FirstPage extends StatelessWidget {
  const FirstPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('first page')),
      body: Center(
        child: Text('first page count='),
      ),
      floatingActionButton: FloatingActionButton(
        child: const Icon(Icons.polymer),
        onPressed: () => Navigator.of(context)
            .push(MaterialPageRoute(builder: (context) => SecondPage())),
      ),
    );
  }
}

class SecondPage extends StatelessWidget {
  const SecondPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('second page')),
      body: Center(
        child: Text('Second page count='),
      ),
      floatingActionButton: FloatingActionButton(
        child: const Icon(Icons.add),
        onPressed: () => print('+++'),
      ),
    );
  }
}

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

数据关联模型创建

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

// 定义数据模型,通过混入 ChangeNotifier 管理监听者(通知模式)
class CountModel with ChangeNotifier {
  int _count = 0;
  // 读方法
  int get counter => _count;
  // 写方法
  void increment() {
    _count++;
    notifyListeners(); // 通知监听者刷新
  }
}
数据包装

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider.value(
        value: CountModel(),
        child: MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: const FirstPage(),
        ));
  }
}
数据使用

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

  @override
  Widget build(BuildContext context) {
    final _counter = Provider.of<CountModel>(context);
    return Scaffold(
      appBar: AppBar(title: Text('first page')),
      body: Center(
        child: Text('first page count=${_counter.counter}'),
      ),
      floatingActionButton: FloatingActionButton(
        child: const Icon(Icons.polymer),
        onPressed: () => Navigator.of(context)
            .push(MaterialPageRoute(builder: (context) => SecondPage())),
      ),
    );
  }

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

Flutter中文网

Flutter开发

相关推荐

  1. 自学Flutter使用Provider进行状态管理

    2024-06-06 21:02:05       31 阅读
  2. [Flutter]使用Provider进行状态管理

    2024-06-06 21:02:05       46 阅读
  3. Flutter 官方状态管理 Provider基本使用

    2024-06-06 21:02:05       58 阅读
  4. 深入探索Flutter中的状态管理使用Provider

    2024-06-06 21:02:05       23 阅读
  5. Flutter GetX 状态管理

    2024-06-06 21:02:05       56 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-06 21:02:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-06 21:02:05       101 阅读
  3. 在Django里面运行非项目文件

    2024-06-06 21:02:05       82 阅读
  4. Python语言-面向对象

    2024-06-06 21:02:05       91 阅读

热门阅读

  1. Oracle 如何定自增长数字列

    2024-06-06 21:02:05       24 阅读
  2. QNX Trusted Disk (QTD) 的原理及数据保护机制

    2024-06-06 21:02:05       23 阅读
  3. dolphinScheduler(海豚调度器)分布式机群安装

    2024-06-06 21:02:05       30 阅读
  4. WPF实现简单的3D图形

    2024-06-06 21:02:05       31 阅读
  5. 2024-05-30 问AI: 在深度学习中,什么叫early stopping ?

    2024-06-06 21:02:05       32 阅读
  6. 设计模式之单例模式

    2024-06-06 21:02:05       27 阅读
  7. Android R及以上版本中APP外部存储实现

    2024-06-06 21:02:05       28 阅读
  8. word模板内容替换

    2024-06-06 21:02:05       28 阅读
  9. python字典包连接mysql

    2024-06-06 21:02:05       31 阅读