flutter State管理
import 'package:flutter/material.dart'; import 'package:scoped_model/scoped_model.dart'; class StateManagementDemo extends StatelessWidget { @override Widget build(BuildContext context) { return ScopedModel( model: CounterModel(), child: Scaffold( appBar: AppBar( title: Text('StateManagementDemo'), elevation: 0.0, ), body: CounterWrapper(), floatingActionButton: ScopedModelDescendant<CounterModel>( rebuildOnChange: false, builder: (context, _, model) => FloatingActionButton( child: Icon(Icons.add), onPressed: model.increaseCount, ), ), ), ); } } class CounterWrapper extends StatelessWidget { @override Widget build(BuildContext context) { return Center( child: Counter(), ); } } class Counter extends StatelessWidget { @override Widget build(BuildContext context) { return ScopedModelDescendant<CounterModel>( builder: (context, _, model) => ActionChip( label: Text('${model.count}'), onPressed: model.increaseCount, ), ); } } class CounterProvider extends InheritedWidget { final int count; final VoidCallback increaseCount; final Widget child; CounterProvider({ this.count, this.increaseCount, this.child, }) : super(child: child); static CounterProvider of(BuildContext context) => context.inheritFromWidgetOfExactType(CounterProvider); @override bool updateShouldNotify(InheritedWidget oldWidget) { return true; } } class CounterModel extends Model { int _count = 0; int get count => _count; void increaseCount() { _count += 1; notifyListeners(); } }
scoped_model: ^0.3.0
效果:
最后,关注【码上加油站】微信公众号后,有疑惑有问题想加油的小伙伴可以码上加入社群,让我们一起码上加油吧!!!