Model { int _counter = 0 ; int get counter => _counter ; void increment() { // First, increment the counter _counter++ ; // Then notify all the listeners. notifyListeners() ; } } ScopedModel - ެࣜͷࣄྫ ঢ়ଶΛͭ.PEFMΛఆٛ
// First, create a `ScopedModel` widget. This will provide // the `model` to the children that request it. return new ScopedModel<CounterModel> ( model: new CounterModel() , child: new Column(children: [ // Create a ScopedModelDescendant. This widget will get the // CounterModel from the nearest ScopedModel<CounterModel>. // It will hand that model to our builder method, and rebuild // any time the CounterModel changes (i.e. after we // `notifyListeners` in the Model). new ScopedModelDescendant<CounterModel> ( builder: (context, child, model) => new Text('${model.counter}') , ) , new Text("Another widget that doesn't depend on the CounterModel" ) ] ) ) ; } } ScopedModel - ެࣜͷࣄྫ ར༻͢Δ 4DPQFE.PEFMΛఆٛ .PEFMΛར༻͍ͨ͠8JEHFUΛ 4DPQFE.PEFM%FTDFOEBOUͰ แΉ
(context) => CartModel(), // ChangeNoti fi e r child: const MyApp(), // Widge t ) , ) ; } Provider - ެࣜͷࣄྫ $IBOHF/PUJ fi FS1SPWJEFS Ͱ$IBOHF/PUJ fi FSͱ8JEHFUΛ ఆٛ
state of the cart. fi nal List<Item> _items = [] ; /// An unmodi fi able view of the items in the cart. Unmodi fi ableListView<Item> get items => Unmodi fi ableListView(_items) ; /// The current total price of all items (assuming all items cost $42). int get totalPrice => _items.length * 42 ; /// Adds [item] to cart. This and [removeAll] are the only ways to modify the /// cart from the outside. void add(Item item) { _items.add(item) ; // This call tells the widgets that are listening to this model to rebuild. notifyListeners() ; } /// Removes all items from the cart. void removeAll() { _items.clear() ; // This call tells the widgets that are listening to this model to rebuild. notifyListeners() ; } } Provider - ެࣜͷࣄྫ $IBOHF/PUJ fi FSͰΛఆٛ