Skip to content

Commit

Permalink
更新了 provider example
Browse files Browse the repository at this point in the history
  • Loading branch information
Vadaski committed Dec 30, 2019
1 parent 42e646e commit 27df0d8
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 142 deletions.
Binary file added image/provider_example.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 5 additions & 1 deletion mecury_project/example/provider_example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
## 简介
使用 Google 官方推荐的状态管理 [Provider](https://pub.dev/packages/provider#-installing-tab-) 的 demo。详细了解请看这里:[Flutter | 状态管理指南篇——Provider](https://juejin.im/editor/posts/5d00a84fe51d455a2f22023f)



[更新]:Demo 新增单页面状态提供方式,供参考

## 样例
![](https://user-gold-cdn.xitu.io/2019/6/12/16b4c691828e5a7a?w=333&h=676&f=gif&s=235209)
![provider_example](../../../image/provider_example.gif)

## Getting Started

Expand Down
14 changes: 14 additions & 0 deletions mecury_project/example/provider_example/lib/color_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'package:flutter/material.dart';

class ColorModel with ChangeNotifier {
int _seed = 0xFFFF9000;
Color _color = Color(0xFFFF9000);

get color => _color;

changeColor() {
_seed += 30;
_color = Color(_seed);
notifyListeners();
}
}
122 changes: 0 additions & 122 deletions mecury_project/example/provider_example/lib/generated/i18n.dart

This file was deleted.

125 changes: 106 additions & 19 deletions mecury_project/example/provider_example/lib/screens.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'counter_model.dart';
import 'color_model.dart';

class FirstScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
final counter = Provider.of<CounterModel>(context,);
final counter = Provider.of<CounterModel>(
context,
);
final textSize = Provider.of<int>(context);

return Scaffold(
Expand All @@ -29,32 +32,116 @@ class FirstScreen extends StatelessWidget {
}
}

/// This is a single page provider,
/// it will be destroyed when the page is removed,
/// and the status will be reset.
class SecondScreenProvider extends StatefulWidget {
final Widget child;

SecondScreenProvider({@required this.child});

@override
_SecondScreenProviderState createState() => _SecondScreenProviderState();
}

class _SecondScreenProviderState extends State<SecondScreenProvider> {
ColorModel _colorModel = ColorModel();

@override
Widget build(BuildContext context) {
return ChangeNotifierProvider.value(
value: _colorModel,
child: widget.child,
);
}
}

class SecondScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Second Page'),
return SecondScreenProvider(
child: DefaultTabController(
length: 3,
child: Scaffold(
appBar: buildAppBar(),
body: TabBarView(children: [
buildCounterText(),
showIconWithColor(),
showIconWithColor(),
]),
floatingActionButton: buildActionButtons(),
),
),
body: Consumer2<CounterModel, int>(
builder: (context, CounterModel counter, int textSize, child) {
return Center(
child: Text(
'Value: ${counter.value}',
style: TextStyle(
fontSize: textSize.toDouble(),
),
);
}

Column buildActionButtons() {
return Column(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Consumer<ColorModel>(
builder: (context, ColorModel colorModel, child) {
return FloatingActionButton(
heroTag: 'no_hero',
child: Icon(Icons.swap_horizontal_circle),
onPressed: () {
colorModel.changeColor();
},
);
},
),
SizedBox(height: 20),
Consumer<CounterModel>(
builder: (context, CounterModel counter, child) =>
FloatingActionButton(
onPressed: () => counter.increment(),
child: child,
),
child: Icon(Icons.add),
),
],
);
}

Consumer2<CounterModel, int> buildCounterText() {
return Consumer2<CounterModel, int>(
builder: (context, CounterModel counter, int textSize, child) {
return Center(
child: Text(
'Value: ${counter.value}',
style: TextStyle(
fontSize: textSize.toDouble(),
),
),
);
},
);
}

AppBar buildAppBar() {
return AppBar(
title: Text('Second Page'),
bottom: TabBar(tabs: [
Tab(text: 'show counter'),
Tab(text: 'show color'),
Tab(text: 'show color'),
]),
);
}

Container showIconWithColor() {
return Container(
alignment: Alignment.center,
child: Consumer<ColorModel>(
builder: (context, ColorModel colorModel, child) {
return Icon(
Icons.stars,
color: colorModel.color,
size: 100,
);
},
),
floatingActionButton: Consumer<CounterModel>(
builder: (context, CounterModel counter, child) => FloatingActionButton(
onPressed: () => counter.increment(),
child: child,
),
child: Icon(Icons.add),
),
);
}
}

0 comments on commit 27df0d8

Please sign in to comment.