Flutter状态监听插件listenablestate的使用

Flutter状态监听插件listenablestate的使用

listenablestate 是一个用于状态管理的 Flutter 包,它提供了可自定义的 listenablestateStateBuilder 来构建反应式用户界面。该包还包含诸如使用 buildWhen 进行条件重建和状态变化监听器等功能。

目录

安装

要使用此包,在你的 pubspec.yaml 文件中添加 listenablestate 作为依赖:

dependencies:
  listenablestate: ^0.0.1

然后运行:

flutter pub get

使用

基本示例
import 'package:flutter/material.dart';
import 'package:listenablestate/listenablestate.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: CounterPage(),
    );
  }
}

class CounterPage extends StatelessWidget {
  final counterNotifier = listenablestate(0);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Counter')),
      body: Center(
        child: StateBuilder<int>(
          listenable: counterNotifier,
          builder: (context, count) {
            return Text('Count: $count');
          },
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => counterNotifier.update(counterNotifier.state + 1),
        child: Icon(Icons.add),
      ),
    );
  }
}
条件重建(使用 buildWhen
StateBuilder<int>(
  listenable: counterNotifier,
  buildWhen: (oldState, newState) => newState % 2 == 0,
  builder: (context, count) {
    return Text('Even Count: $count');
  },
);
状态变化监听
StateBuilder<int>(
  listenable: counterNotifier,
  listen: (context, count) {
    if (count == 10) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('Reached 10!')),
      );
    }
  },
  builder: (context, count) {
    return Text('Count: $count');
  },
);

API 参考

  • listenablestate: 一个持有状态并在状态变化时通知监听器的类。

    listenablestate(T state): 创建一个初始状态为 `state` 的 `listenablestate`。
    T get state: 返回当前状态。
    void update(T state): 更新状态并通知监听器。
    
  • StateBuilder: 一个根据 listenablestate 状态进行重建的组件。

    StateBuilder({required listenablestate<T> listenable, required Widget Function(BuildContext, T) builder}): 创建一个监听 `listenablestate` 的 `StateBuilder`。
    

更多关于Flutter状态监听插件listenablestate的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter状态监听插件listenablestate的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


在Flutter中,ListenableState 并不是一个官方的插件或类。可能你指的是某种状态管理方案,或者是某个第三方库提供的功能。不过,Flutter 提供了 ChangeNotifierValueNotifier 等类,它们实现了 Listenable 接口,可以用来监听状态的变化。

如果你想要监听状态的变化,可以使用 ChangeNotifierValueNotifier,并结合 ListenableBuilderAnimatedBuilder 来构建 UI。

使用 ChangeNotifierListenableBuilder 的示例

  1. 创建一个 ChangeNotifier
class Counter with ChangeNotifier {
  int _count = 0;

  int get count => _count;

  void increment() {
    _count++;
    notifyListeners();
  }
}
  1. 在 UI 中使用 ListenableBuilder
class MyApp extends StatelessWidget {
  final Counter counter = Counter();

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('ListenableState Example')),
        body: Center(
          child: ListenableBuilder(
            listenable: counter,
            builder: (context, child) {
              return Text('Count: ${counter.count}');
            },
          ),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () {
            counter.increment();
          },
          child: Icon(Icons.add),
        ),
      ),
    );
  }
}

使用 ValueNotifierValueListenableBuilder 的示例

  1. 创建一个 ValueNotifier
final counter = ValueNotifier<int>(0);
  1. 在 UI 中使用 ValueListenableBuilder
class MyApp extends StatelessWidget {
  final ValueNotifier<int> counter = ValueNotifier<int>(0);

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('ValueListenable Example')),
        body: Center(
          child: ValueListenableBuilder<int>(
            valueListenable: counter,
            builder: (context, value, child) {
              return Text('Count: $value');
            },
          ),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () {
            counter.value++;
          },
          child: Icon(Icons.add),
        ),
      ),
    );
  }
}
回到顶部