mirror of
https://github.com/flutter/samples.git
synced 2025-11-08 13:58:47 +00:00
Move scoped_model_counter to provider_counter (#82)
I used `flutter create` anew, so the project structure now has additional 2 `AndroidManifest.xml` files. Everything else stayed the same, so from git’s perspective, it’s just moved files.
This commit is contained in:
88
provider_counter/lib/main.dart
Normal file
88
provider_counter/lib/main.dart
Normal file
@@ -0,0 +1,88 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
void main() {
|
||||
// Initialize the model. Can be done outside a widget, like here.
|
||||
var counter = Counter();
|
||||
|
||||
// Setup a delayed interaction with the model (increment each 5 seconds),
|
||||
// outside of the Flutter widget tree.
|
||||
Timer.periodic(
|
||||
const Duration(seconds: 5),
|
||||
(timer) => counter.increment(),
|
||||
);
|
||||
|
||||
// Now we're ready to run the app...
|
||||
runApp(
|
||||
// ... and provide the model to all widgets within.
|
||||
ChangeNotifierProvider<Counter>.value(
|
||||
notifier: counter,
|
||||
child: MyApp(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// Simplest possible model, with just one field.
|
||||
class Counter extends ChangeNotifier {
|
||||
int value = 0;
|
||||
|
||||
void increment() {
|
||||
value += 1;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
title: 'Flutter Demo',
|
||||
theme: ThemeData(
|
||||
primarySwatch: Colors.blue,
|
||||
),
|
||||
home: MyHomePage(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class MyHomePage extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text('Flutter Demo Home Page'),
|
||||
),
|
||||
body: Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
Text('You have pushed the button this many times:'),
|
||||
// ScopedModelDescendant looks for an ancestor ScopedModel widget
|
||||
// and retrieves its model (Counter, in this case).
|
||||
// Then it uses that model to build widgets, and will trigger
|
||||
// rebuilds if the model is updated.
|
||||
Consumer<Counter>(
|
||||
builder: (context, counter, child) => Text(
|
||||
'${counter.value}',
|
||||
style: Theme.of(context).textTheme.display1,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
// ScopedModel.of is another way to access the model object held
|
||||
// by an ancestor ScopedModel. By default, it just returns
|
||||
// the current model and doesn't automatically trigger rebuilds.
|
||||
// Since this button always looks the same, though, no rebuilds
|
||||
// are needed.
|
||||
onPressed: () =>
|
||||
Provider.of<Counter>(context, listen: false).increment(),
|
||||
tooltip: 'Increment',
|
||||
child: Icon(Icons.add),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user