1
0
mirror of https://github.com/flutter/samples.git synced 2025-11-08 13:58:47 +00:00
Files
samples/provider_counter/lib/main.dart
Filip Hracek 31edec5378 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.
2019-05-20 15:59:51 -07:00

89 lines
2.5 KiB
Dart

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),
),
);
}
}