Skip to content

Commit c945b30

Browse files
committed
update provider example
adding addAll use case
1 parent b3a1a2c commit c945b30

File tree

2 files changed

+50
-24
lines changed

2 files changed

+50
-24
lines changed

mecury_project/example/provider_example/lib/goods_model.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import 'package:flutter/material.dart' show ChangeNotifier;
22

33
class GoodsListProvider with ChangeNotifier {
4+
5+
bool shouldRebuild = false;
6+
47
List<Goods> _goodsList =
58
List.generate(10, (index) => Goods(false, 'Goods No. $index'));
69

@@ -12,6 +15,16 @@ class GoodsListProvider with ChangeNotifier {
1215
_goodsList[index] = Goods(!good.isCollection, good.goodsName);
1316
notifyListeners();
1417
}
18+
19+
addAll(){
20+
_goodsList.addAll(List.generate(10, (index)=>Goods(false, 'Goods No. $index')));
21+
shouldRebuild = true;
22+
notifyListeners();
23+
}
24+
25+
rebuild(){
26+
shouldRebuild = false;
27+
}
1528
}
1629

1730
class Goods {

mecury_project/example/provider_example/lib/screens.dart

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -152,31 +152,44 @@ class GoodsListScreen extends StatelessWidget {
152152
Widget build(BuildContext context) {
153153
return ChangeNotifierProvider(
154154
create: (_) => GoodsListProvider(),
155-
child: Selector<GoodsListProvider, GoodsListProvider>(
156-
shouldRebuild: (pre, next) => false,
157-
selector: (context, provider) => provider,
158-
builder: (context, provider, child) {
159-
return ListView.builder(
160-
itemCount: provider.total,
161-
itemBuilder: (context, index) {
162-
return Selector<GoodsListProvider, Goods>(
163-
selector: (context, provider) => provider.goodsList[index],
164-
builder: (context, data, child) {
165-
print(('No.${index + 1} rebuild'));
155+
child: Scaffold(
156+
body: Selector<GoodsListProvider, GoodsListProvider>(
157+
shouldRebuild: (pre, next) => pre.shouldRebuild,
158+
selector: (context, provider) => provider,
159+
builder: (context, provider, child) {
160+
provider.rebuild();
161+
return ListView.builder(
162+
itemCount: provider.total,
163+
itemBuilder: (context, index) {
164+
return Selector<GoodsListProvider, Goods>(
165+
selector: (context, provider) => provider.goodsList[index],
166+
builder: (context, data, child) {
167+
print(('No.${index + 1} rebuild'));
166168

167-
return ListTile(
168-
title: Text(data.goodsName),
169-
trailing: GestureDetector(
170-
onTap: () => provider.collect(index),
171-
child: Icon(
172-
data.isCollection ? Icons.star : Icons.star_border),
173-
),
174-
);
175-
},
176-
);
177-
},
178-
);
179-
},
169+
return ListTile(
170+
title: Text(data.goodsName),
171+
trailing: GestureDetector(
172+
onTap: () => provider.collect(index),
173+
child: Icon(
174+
data.isCollection ? Icons.star : Icons.star_border),
175+
),
176+
);
177+
},
178+
);
179+
},
180+
);
181+
},
182+
),
183+
floatingActionButton: Consumer<GoodsListProvider>(
184+
builder: (context, GoodsListProvider model, child) {
185+
return FloatingActionButton(
186+
child: Icon(Icons.add),
187+
onPressed: () {
188+
model.addAll();
189+
},
190+
);
191+
},
192+
),
180193
),
181194
);
182195
}

0 commit comments

Comments
 (0)