Skip to content

Instantly share code, notes, and snippets.

@niwatly
Created December 23, 2019 09:22
Show Gist options
  • Save niwatly/be6dbf82462c3fd470790a09827a4e6b to your computer and use it in GitHub Desktop.
Save niwatly/be6dbf82462c3fd470790a09827a4e6b to your computer and use it in GitHub Desktop.
ChangeNotifierProvider
import 'package:collabo_base_app/helper/route_helper.dart';
import 'package:collabo_base_app/view/space_box.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../app.dart';
class _TestNotifier = ValueNotifier<int> with Type;
class TestScreen extends StatefulWidget {
final TestScreenArguments _arguments;
const TestScreen(this._arguments);
@override
State<StatefulWidget> createState() => _TestScreenState();
}
class _TestScreenState extends State<TestScreen> {
final _notifier = _TestNotifier(0);
@override
void dispose() {
_notifier.dispose();
super.dispose();
}
// // ボタンを推してもbuild child は増えず、TextにCounterの値は反映されない
// @override
// Widget build(BuildContext context) {
// return ChangeNotifierProvider<_TestNotifier>.value(
// value: _notifier,
// child: () {
// logger.info("build child");
//
// return Column(
// mainAxisAlignment: MainAxisAlignment.center,
// children: [
// Text(_notifier.value.toString()),
// const SpaceBox.height(32),
// FloatingActionButton(
// onPressed: () => _notifier.value++,
// ),
// ],
// );
// }(),
// );
// }
// // ボタンを押すたびにbuild child が増え、TextにCounterの値が反映される
// @override
// Widget build(BuildContext context) {
// return ChangeNotifierProvider<_TestNotifier>.value(
// value: _notifier,
// child: Builder(
// builder: (context) {
// logger.info("build child");
//
// return Column(
// mainAxisAlignment: MainAxisAlignment.center,
// children: [
// Text(Provider.of<_TestNotifier>(context).value.toString()),
// const SpaceBox.height(32),
// FloatingActionButton(
// onPressed: () => _notifier.value++,
// ),
// ],
// );
// },
// ),
// );
// }
// // 実行時エラー(指定のContextに指定のProviderは見つかりません)
// @override
// Widget build(BuildContext context) {
// return ChangeNotifierProvider<_TestNotifier>.value(
// value: _notifier,
// child: () {
// logger.info("build child");
//
// return Column(
// mainAxisAlignment: MainAxisAlignment.center,
// children: [
// Text(Provider.of<_TestNotifier>(context).value.toString()),
// const SpaceBox.height(32),
// FloatingActionButton(
// onPressed: () => _notifier.value++,
// ),
// ],
// );
// }(),
// );
// }
// ボタンを押すたびにbuild child は増えないが、TextにCounterの値が反映される
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<_TestNotifier>.value(
value: _notifier,
child: () {
logger.info("build child");
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Builder(
builder: (context) => Text(Provider.of<_TestNotifier>(context).value.toString()),
),
const SpaceBox.height(32),
FloatingActionButton(
onPressed: () => _notifier.value++,
),
],
);
}(),
);
}
}
class TestScreenArguments implements IScreenArguments {
const TestScreenArguments();
@override
Route generateRoute() => MaterialPageRoute(
builder: (context) => TestScreen(this),
settings: RouteSettings(name: "/$screenName"),
);
@override
String get screenName => "test";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment