1、在 pubspec.yaml 中添加依赖
dependencies:
provide: ^1.0.2
2、编写provide
import 'package:flutter/material.dart';
// with ChangeNotifier必须混入
class Counterwith ChangeNotifier {
intvalue=0;
increment(){
value++;
// 通知监听者 局部刷新widget
notifyListeners();
}
}
3、在项目的入口文件mian.dart修改
void main() {
var counter =Counter();
var childCategory =ChildCategory();
var providers =Providers();
providers
..provide(Provider.value(counter))
//添加多个时直接..
..provide(Provider.value(childCategory));
runApp(ProviderNode(
child:MyApp(),
providers: providers,
));
// runApp(MyApp());
}
4、需要用到的地方
class _NumberStateextends State {
@override
Widget build(BuildContext context) {
return Container(
child:Provide<ChildCategory>(
builder: (context,child,counter){
return Text('${counter.value}');
}
)
);
}
}
5、调用里面的方法
class MyButtonextends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
child:RaisedButton(
onPressed: () {
//Provide.value(context).increment();
Provide.value<CategoryGoodsListProvide>(context).getGoodsList(goodsList.data);
},
child:Text("递增"),
),
);
}
}
网友评论