[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch simple state management page to provider #2686

Merged
merged 2 commits into from
May 22, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Switch simple state management page to provider
This is switching the example from ScopedModel to Provider.
  • Loading branch information
filiph committed May 22, 2019
commit e2fddaae3a55c4f81635c9f52c27770b06ccc251
30 changes: 21 additions & 9 deletions examples/state_mgmt/simple/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import 'package:flutter/material.dart';
import 'package:scoped_model/scoped_model.dart';
import 'package:provider/provider.dart';
import 'package:state_mgmt/src/passing_callbacks.dart' as callbacks;
import 'package:state_mgmt/src/performance.dart' as performance;
import 'package:state_mgmt/src/scoped_model.dart';
import 'package:state_mgmt/src/provider.dart';
import 'package:state_mgmt/src/set_state.dart' as set_state;

// #docregion main
void main() {
final cart = CartModel();

// You could optionally connect [cart] with some database here.

runApp(
ScopedModel<CartModel>(
model: cart,
ChangeNotifierProvider(
builder: (context) => CartModel(),
child: MyApp(),
),
);
Expand All @@ -22,11 +18,25 @@ void main() {

Map<String, WidgetBuilder> _routes = {
'/setstate': (context) => set_state.HelperScaffoldWrapper(),
'/scoped': (context) => MyHomepage(),
'/provider': (context) => MyHomepage(),
'/callbacks': (context) => callbacks.MyHomepage(),
'/perf': (context) => performance.MyHomepage(),
};

// #docregion multi-provider-main
void multiProviderMain() {
runApp(
MultiProvider(
providers: [
ChangeNotifierProvider(builder: (context) => CartModel()),
Provider(builder: (context) => SomeOtherClass()),
],
child: MyApp(),
),
);
}
// #enddocregion multi-provider-main

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
Expand All @@ -40,6 +50,8 @@ class MyApp extends StatelessWidget {
}
}

class SomeOtherClass {}

class _Menu extends StatelessWidget {
@override
Widget build(BuildContext context) {
Expand Down
18 changes: 9 additions & 9 deletions examples/state_mgmt/simple/lib/src/performance.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:flutter/material.dart';
import 'package:scoped_model/scoped_model.dart';
import 'package:state_mgmt/src/scoped_model.dart';
import 'package:provider/provider.dart';
import 'package:state_mgmt/src/provider.dart';

class AnotherMonstrousWidget extends SomeExpensiveWidget {
AnotherMonstrousWidget({Widget child}) : super(child: child);
Expand All @@ -10,8 +10,8 @@ class ChildUsingDescendant extends StatelessWidget {
@override
Widget build(BuildContext context) {
// #docregion child
return ScopedModelDescendant<CartModel>(
builder: (context, child, cart) => Stack(
return Consumer<CartModel>(
builder: (context, cart, child) => Stack(
children: [
// Use SomeExpensiveWidget here, without rebuilding every time.
child,
Expand All @@ -34,8 +34,8 @@ class DescendantInLeafNode_Good extends StatelessWidget {
// ...
child: AnotherMonstrousWidget(
// ...
child: ScopedModelDescendant<CartModel>(
builder: (context, child, cart) {
child: Consumer<CartModel>(
builder: (context, cart, child) {
return Text('Total price: ${cart.totalPrice}');
},
),
Expand All @@ -50,8 +50,8 @@ class DescendantNotInLeafNode_Bad extends StatelessWidget {
Widget build(BuildContext context) {
// #docregion nonLeafDescendant
// DON'T DO THIS
return ScopedModelDescendant<CartModel>(
builder: (context, child, cart) {
return Consumer<CartModel>(
builder: (context, cart, child) {
return HumongousWidget(
// ...
child: AnotherMonstrousWidget(
Expand Down Expand Up @@ -98,7 +98,7 @@ class NonRebuilding_Good extends StatelessWidget {
void _onPressed(BuildContext context) {
var item = Item('Dash');
// #docregion nonRebuilding
ScopedModel.of<CartModel>(context).add(item);
Provider.of<CartModel>(context, listen: false).add(item);
// #enddocregion nonRebuilding
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import 'dart:collection';

import 'package:flutter/material.dart';
import 'package:scoped_model/scoped_model.dart';
import 'package:provider/provider.dart';
import 'package:state_mgmt/src/common.dart';

CartModel somehowGetMyCartModel(BuildContext context) {
return ScopedModel.of<CartModel>(context, rebuildOnChange: true);
return Provider.of<CartModel>(context);
}

// #docregion model
class CartModel extends Model {
class CartModel extends ChangeNotifier {
/// Internal, private state of the cart.
final List<Item> _items = [];

Expand Down Expand Up @@ -42,8 +42,8 @@ class MyCartTotalWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
// #docregion descendant
return ScopedModelDescendant<CartModel>(
builder: (context, child, cart) {
return Consumer<CartModel>(
builder: (context, cart, child) {
return Text("Total price: ${cart.totalPrice}");
},
);
Expand Down
4 changes: 2 additions & 2 deletions examples/state_mgmt/simple/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ description: Sample state management code.
version: 1.0.0+1

environment:
sdk: ">=2.0.0-dev.68.0 <3.0.0"
sdk: ">=2.2.2 <3.0.0"

dependencies:
flutter:
sdk: flutter

cupertino_icons: ^0.1.2

scoped_model: ^1.0.0
provider: ^2.0.0

dev_dependencies:
flutter_test:
Expand Down
2 changes: 1 addition & 1 deletion examples/state_mgmt/simple/test/model_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:state_mgmt/src/scoped_model.dart';
import 'package:state_mgmt/src/provider.dart';
import 'package:test/test.dart';

void main() {
Expand Down
8 changes: 4 additions & 4 deletions examples/state_mgmt/simple/test/widget_test.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:scoped_model/scoped_model.dart';
import 'package:provider/provider.dart';
import 'package:state_mgmt/main.dart';
import 'package:state_mgmt/src/scoped_model.dart';
import 'package:state_mgmt/src/provider.dart';

void main() {
testWidgets('smoke test', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(
ScopedModel<CartModel>(
model: CartModel(),
Provider(
builder: (context) => CartModel(),
child: MyApp(),
),
);
Expand Down
26 changes: 13 additions & 13 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
name: analyzer
url: "https://pub.dartlang.org"
source: hosted
version: "0.36.2"
version: "0.36.3"
args:
dependency: transitive
description:
Expand Down Expand Up @@ -42,42 +42,42 @@ packages:
name: build_daemon
url: "https://pub.dartlang.org"
source: hosted
version: "0.5.0"
version: "0.6.1"
build_resolvers:
dependency: transitive
description:
name: build_resolvers
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.4"
version: "1.0.5"
build_runner:
dependency: "direct dev"
description:
name: build_runner
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.4"
version: "1.4.0"
build_runner_core:
dependency: transitive
description:
name: build_runner_core
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.4"
version: "3.0.5"
built_collection:
dependency: transitive
description:
name: built_collection
url: "https://pub.dartlang.org"
source: hosted
version: "4.2.0"
version: "4.2.2"
built_value:
dependency: transitive
description:
name: built_value
url: "https://pub.dartlang.org"
source: hosted
version: "6.4.0"
version: "6.5.0"
charcode:
dependency: transitive
description:
Expand Down Expand Up @@ -172,7 +172,7 @@ packages:
name: front_end
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.17"
version: "0.1.18"
github:
dependency: "direct dev"
description:
Expand Down Expand Up @@ -242,14 +242,14 @@ packages:
name: json_annotation
url: "https://pub.dartlang.org"
source: hosted
version: "2.2.0"
version: "2.4.0"
kernel:
dependency: transitive
description:
name: kernel
url: "https://pub.dartlang.org"
source: hosted
version: "0.3.17"
version: "0.3.18"
linkcheck:
dependency: "direct dev"
description:
Expand Down Expand Up @@ -312,7 +312,7 @@ packages:
name: pedantic
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.0"
version: "1.7.0"
pool:
dependency: transitive
description:
Expand Down Expand Up @@ -382,7 +382,7 @@ packages:
name: stream_transform
url: "https://pub.dartlang.org"
source: hosted
version: "0.0.18"
version: "0.0.19"
string_scanner:
dependency: transitive
description:
Expand Down Expand Up @@ -447,4 +447,4 @@ packages:
source: hosted
version: "2.1.15"
sdks:
dart: ">=2.2.0 <3.0.0"
dart: ">=2.3.0-dev.0.1 <3.0.0"
Loading