Skip to content

Commit

Permalink
update provider demo
Browse files Browse the repository at this point in the history
更新 provider 到 4.0

新增 selector 的使用样例
  • Loading branch information
Vadaski committed Feb 2, 2020
1 parent b77741a commit 373fda1
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 3 deletions.
22 changes: 22 additions & 0 deletions mecury_project/example/provider_example/lib/goods_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'package:flutter/material.dart' show ChangeNotifier;

class GoodsListProvider with ChangeNotifier {
List<Goods> _goodsList =
List.generate(10, (index) => Goods(false, 'Goods No. $index'));

get goodsList => _goodsList;
get total => _goodsList.length;

collect(int index) {
var good = _goodsList[index];
_goodsList[index] = Goods(!good.isCollection, good.goodsName);
notifyListeners();
}
}

class Goods {
final bool isCollection;
final String goodsName;

Goods(this.isCollection, this.goodsName);
}
24 changes: 22 additions & 2 deletions mecury_project/example/provider_example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ void main() {
runApp(
MultiProvider(providers: [
Provider<int>(
builder: (_) => textSize,
create: (_) => textSize,
),
ChangeNotifierProvider<CounterModel>.value(value: counter),
], child: MyApp()),
Expand All @@ -22,7 +22,27 @@ class MyApp extends StatelessWidget {
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark(),
home: FirstScreen(),
home: DefaultTabController(
length: 2,
child: Scaffold(
appBar: TabBar(tabs: [
Tab(
text: 'CounterDemo',
icon: Icon(Icons.create),
),
Tab(
text: 'SelectorDemo',
icon: Icon(Icons.shopping_cart),
),
]),
body: TabBarView(
children: [
FirstScreen(),
GoodsListScreen(),
],
),
),
),
);
}
}
36 changes: 36 additions & 0 deletions mecury_project/example/provider_example/lib/screens.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'counter_model.dart';
import 'color_model.dart';
import 'goods_model.dart';

class FirstScreen extends StatelessWidget {
@override
Expand Down Expand Up @@ -145,3 +146,38 @@ class SecondScreen extends StatelessWidget {
);
}
}

class GoodsListScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (_) => GoodsListProvider(),
child: Selector<GoodsListProvider, GoodsListProvider>(
shouldRebuild: (pre, next) => false,
selector: (context, provider) => provider,
builder: (context, provider, child) {
return ListView.builder(
itemCount: provider.total,
itemBuilder: (context, index) {
return Selector<GoodsListProvider, Goods>(
selector: (context, provider) => provider.goodsList[index],
builder: (context, data, child) {
print(('No.${index + 1} rebuild'));

return ListTile(
title: Text(data.goodsName),
trailing: GestureDetector(
onTap: () => provider.collect(index),
child: Icon(
data.isCollection ? Icons.star : Icons.star_border),
),
);
},
);
},
);
},
),
);
}
}
2 changes: 1 addition & 1 deletion mecury_project/example/provider_example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ dependencies:
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2
provider: ^3.0.0+1
provider: ^4.0.2

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 373fda1

Please sign in to comment.