0 purchases
wrap riverpod
Wrap_Riverpod #
基于riverpod,在Provider的使用上做了一层包装。
Source #
river_pod基础操作 https://pub.dev/packages/flutter_riverpod
final counterProvider = StateNotifierProvider((ref) {
return Counter();
});
class Counter extends StateNotifier<int> {
Counter(): super(0);
void increment() => state++;
}
class Example extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final count = ref.watch(counterProvider);
return Text(count.toString());
}
}
copied to clipboard
Usage #
StateProvider可全局定义,层级跨度大的组件仍可通过同一个Provider驱动所依赖到的所有组件进行更新;
ViewModel提供一层包装,内部实现StateProvider
import 'package:wrap_riverpod/wrap_riverpod.dart';
class TestViewModel extends ViewModel<int> {
TestViewModel(int model) : super(model);
void increment() {
model++;
}
int get num => model;
}
class TestViewModelWidgetState extends State {
final viewModel = TestViewModel(0);
@override
Widget build(BuildContext context) {
return Column(
children: [
Spacer(),
Consumer(
builder: (context, ref, child) => CupertinoButton(
onPressed: () {
viewModel[ref] = viewModel.increment();
print('TestViewModelWidgetState.build ${viewModel[ref]}');
},
child: Container(
width: 120,
height: 40,
child: Text('increment ${viewModel[ref]}'),
),
),
),
],
);
}
}
copied to clipboard
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.