Flutter 中使用Provide状态管理

2019-08-16  本文已影响0人  NightRainBreeze

关于Provide

pubspec

dependencies:

  provide: ^1.0.2

具体使用

/// ChangeNotifier
import 'package:flutter/material.dart’;
/// Provide
import 'package:provide/provide.dart’;

class HomePageCarProvide with ChangeNotifier {
  int _value = 0;

  int get value => _value;

  HomePageCarProvide(this._value);

  void increment() {
    _value++;
    notifyListeners();
  }

  void decrement() {
    _value—;
    notifyListeners();
  }
}
void main() {
  final providers = Providers()
    ..provide(Provider.function((context) => HomePageCarProvide(0)));

  runApp(ProviderNode(
    providers: providers,
    child: CounterApp(),
  ));
}
class MyText extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Provide<HomePageCarProvide>(builder: (context, child, provide) {
        return Text("${provide.value}”);
      }),
    );
  }
}
class MyBtn extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final p = Provide.value<HomePageCarProvide>(context);
    return Container(
      child: RaisedButton(
        onPressed: () {
          p.increment();
        },
        child: Text("+”),
      ),
    );
  }
}

效果演示

flutter_provide.gif
上一篇下一篇

猜你喜欢

热点阅读