{"id":610,"date":"2021-05-17T18:15:39","date_gmt":"2021-05-17T09:15:39","guid":{"rendered":"http:\/\/localhost:8000\/?p=610"},"modified":"2021-05-17T18:15:39","modified_gmt":"2021-05-17T09:15:39","slug":"share-state-between-other-routes","status":"publish","type":"post","link":"http:\/\/localhost:8000\/2021\/05\/share-state-between-other-routes.html","title":{"rendered":"Flutter\u3067\u5225\u753b\u9762\uff08route\uff09\u3067\u72b6\u614b\uff08state\uff09\u3092\u5171\u6709\u3059\u308b"},"content":{"rendered":"
<\/p>\n
Flutter \u3067\u304a\u52c9\u5f37\u6642\u9593\u7ba1\u7406\u7528\u306e\u30bf\u30a4\u30de\u30fc\u30a2\u30d7\u30ea\u3092\u4f5c\u3063\u3066\u3044\u308b\u306e\u3067\u3059\u304c\u3001\u983b\u7e41\u306b\u30bf\u30a4\u30de\u30fc\u306e\u8d77\u52d5\uff0f\u505c\u6b62\u3057\u5fd8\u308c\u304c\u767a\u751f\u3059\u308b\u306e\u3067\u3001\u624b\u4f5c\u696d\u3067\u7d4c\u904e\u6642\u9593\u3092\u7de8\u96c6\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u307e\u3057\u305f\u3002
\n\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u5225\u753b\u9762\u3067\u7de8\u96c6\u3059\u308b\u4f5c\u308a\u306a\u306e\u3067\u3059\u304c\u3001\u5225\u753b\u9762\uff08route\uff09\u3067\u4e0a\u624b\u304f\u5024\u3092\u3084\u308a\u53d6\u308a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u305d\u306e\u5b9f\u88c5\u65b9\u6cd5\u3092\u4e8c\u3064\u307b\u3069\u8a66\u3057\u305f\u306e\u3067\u7d39\u4ecb\u3057\u307e\u3059\u3002
\n<\/p>\n
\u4e00\u3064\u76ee\u306e\u65b9\u6cd5\u306f\u3001 \u753b\u9762\u9077\u79fb\u306e\u5b9f\u88c5\u306f\u3001Flutter cookbook \u306e Navigate to a new screen and back<\/a> \u3092\u53c2\u7167\u304f\u3060\u3055\u3044\u3002<\/p>\n \u4eca\u56de\u306e\u30a2\u30d7\u30ea\u306e\u9077\u79fb\u5143\u753b\u9762\u306e\u753b\u9762\u9077\u79fb\u306b\u95a2\u9023\u3059\u308b\u5b9f\u88c5\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n Whether the route should remain in memory when it is inactive. \u9077\u79fb\u5148\u753b\u9762\u5074\u306e\u5b9f\u88c5\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002 <\u9077\u79fb\u5143\u753b\u9762\u306b\u623b\u308b><\/p>\n \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u5168\u6587\u306f\u3053\u3061\u3089<\/a>\u3092\u53c2\u7167\u304f\u3060\u3055\u3044\u3002<\/p>\n \u4eca\u56de\u306f\u3001\u9077\u79fb\u304c\u4e00\u3064\u3060\u3051\u3060\u3057\u53d7\u3051\u6e21\u3059\u30d1\u30e9\u30e1\u30fc\u30bf\u3082\u5c11\u306a\u3044\u306e\u3067\u3053\u308c\u3067\u3044\u3044\u3068\u601d\u3044\u307e\u3059\u304c\u3001\u8907\u6570\u306e\u753b\u9762\uff08Route\uff09\u306b\u8de8\u308b\u9077\u79fb\u3084\u30d1\u30e9\u30e1\u30fc\u30bf\u304c\u591a\u3044\u30b1\u30fc\u30b9\u306f\u3053\u306e\u65b9\u6cd5\u3060\u3068\u9762\u5012\u3060\u3068\u601d\u3044\u307e\u3059\u3002 \u3053\u3061\u3089<\/a> \u3067\u7d39\u4ecb\u3055\u308c\u3066\u3044\u308b Provider<\/a> \u306f\u3001\u540c\u4e00Route\u5185\u306e Widget \u9593\u3067\u72b6\u614b\u3092\u5171\u6709\u3059\u308b\u305f\u3081\u306e\u65b9\u6cd5\u306a\u306e\u3067\u3059\u304c\u3001\u7570\u306a\u308b Route \u3092\u4f5c\u6210\u3059\u308b\u969b\u306b\u3001\u65e2\u5b58\u306e\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u3092\u6e21\u3059\u3053\u3068\u304c\u3067\u304d\u308b\u306e\u3067\u3001\u7570\u306a\u308b Route \u3067\u3082\u5171\u6709\u3067\u304d\u308b\u3088\u3046\u3067\u3059\u3002<\/p>\n \u3053\u308c\u307e\u3067 \u307e\u305a\u306f\u3001 pubspec.yaml \u306b\u4f9d\u5b58\u95a2\u4fc2\u3092\u8ffd\u8a18\u3057\u3066\u3001Provider \u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002<\/p>\n \u6b21\u306b\u3001\u72b6\u614b\u7ba1\u7406\u7528\u306b \u6b21\u306b\u3001Provider\u3092\u4f7f\u3063\u305f\u30da\u30fc\u30b8\u3092\u4f5c\u308a\u307e\u3059\u3002<\/p>\n \u30dc\u30bf\u30f3\u3092\u30af\u30ea\u30c3\u30af\u3057\u3066\u3001\u753b\u9762\u9077\u79fb\u3092\u884c\u3046\u51e6\u7406\u3092\u8a18\u8f09\u3057\u307e\u3059\u3002<\/p>\n \u6700\u5f8c\u306b\u9077\u79fb\u5148\u753b\u9762\uff08\u7de8\u96c6\u753b\u9762\uff09\u3092\u5b9f\u88c5\u3057\u307e\u3057\u305f\u3002 \u5c11\u3057\u5927\u5909\u3067\u3057\u305f\u304c\u3001Provider \u3092\u5229\u7528\u3057\u3066\u3044\u3066\u3082\u3001\u7570\u306a\u308b Route \u3067\u72b6\u614b\u3092\u5171\u6709\u3059\u308b\u3053\u3068\u306f\u554f\u984c\u306a\u304f\u3067\u304d\u307e\u3057\u305f\u3002 \u4eca\u56de\u306e\u8a18\u4e8b\u3068\u76f4\u63a5\u95a2\u4fc2\u306a\u3044\u3067\u3059\u304c\u3001 pause - resume \u3057\u305f\u969b\u306e\u51e6\u7406\u3092 \u3068\u3044\u3046\u306e\u3082\u3001 \u3057\u3087\u3046\u304c\u306a\u3044\u306e\u3067\u3001 \u4eca\u56de\u3001\u7570\u306a\u308b\u753b\u9762\uff08Route\uff09\u3067\u72b6\u614b\u3092\u5171\u6709\u3059\u308b\u3068\u3053\u308d\u306b\u30d5\u30a9\u30fc\u30ab\u30b9\u3057\u3066\u8abf\u67fb\u3057\u307e\u3057\u305f\u304c\u3001\u7d50\u5c40\u3001\u72b6\u614b\u7ba1\u7406\u3068\u3057\u3066\u3001 \u3061\u306a\u307f\u306b\u3001\u72b6\u614b\u7ba1\u7406\u306b\u3064\u3044\u3066\u306f\u3001Provider \u3092\u9032\u5316\u3055\u305b\u305f\u3088\u3046\u306a Riverpod<\/a> \u3068\u3044\u3046\u30d1\u30c3\u30b1\u30fc\u30b8\u304c\u3042\u308b\u306e\u3067\u3001\u6b63\u5f0f\u7248\u304c\u30ea\u30ea\u30fc\u30b9\u3055\u308c\u305f\u3089\u8a66\u3057\u3066\u307f\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":" Flutter \u3067\u304a\u52c9\u5f37\u6642\u9593\u7ba1\u7406\u7528\u306e\u30bf\u30a4\u30de\u30fc\u30a2\u30d7\u30ea\u3092\u4f5c\u3063\u3066\u3044\u308b\u306e\u3067\u3059\u304c\u3001\u983b\u7e41\u306b\u30bf\u30a4\u30de\u30fc\u306e\u8d77\u52d5\uff0f\u505c\u6b62\u3057\u5fd8\u308c\u304c\u767a\u751f\u3059\u308b\u306e\u3067\u3001\u624b\u4f5c\u696d\u3067\u7d4c\u904e\u6642\u9593\u3092\u7de8\u96c6\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u307e\u3057\u305f\u3002 \u4ee5\u4e0b\u306e\u3088\u3046\u306b\u5225\u753b\u9762\u3067\u7de8\u96c6\u3059\u308b\u4f5c\u308a\u306a\u306e\u3067\u3059\u304c\u3001\u5225\u753b\u9762\uff08route\uff09\u3067\u4e0a\u624b\u304f\u5024\u3092\u3084\u308a\u53d6\u308a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u305d\u306e\u5b9f\u88c5\u65b9\u6cd5\u3092\u4e8c\u3064\u307b\u3069\u8a66\u3057\u305f\u306e\u3067\u7d39\u4ecb\u3057\u307e\u3059\u3002 StatefullWidget + \u753b\u9762\u9077\u79fb\u6642\u306b\u30d1\u30e9\u30e1\u30fc\u30bf\u6e21\u3057\u3059\u308b\u65b9\u6cd5 \u4e00\u3064\u76ee\u306e\u65b9\u6cd5\u306f\u3001StatefullWidget\u3067\u9077\u79fb\u5143\u753b\u9762\uff08route\uff09\u306e\u72b6\u614b\uff08state\uff09\u3092\u7ba1\u7406\u3057\u3064\u3064\u3001\u5225\u753b\u9762\u306b\u9077\u79fb\u3059\u308b\u969b\u306b\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u6e21\u3059\u65b9\u6cd5\u3067\u3059\u3002\u3053\u306e\u65b9\u6cd5\u3060\u3068\u9077\u79fb\u5143\u753b\u9762\u306e\u72b6\u614b\uff08state\uff09\u304c\u753b\u9762\u9077\u79fb\u6642\u306b\u7834\u68c4\u3055\u308c\u308b\u3082\u306e\u3060\u3068\u601d\u3063\u3066\u3044\u305f\u306e\u3067\u3059\u304c\u3001maintainState=true\u306e\u8a2d\u5b9a\u3067\u3042\u308c\u3070\u7834\u68c4\u3055\u308c\u306a\u3044\u3088\u3046\u306a\u306e\u3067\u3001\u7d50\u69cb\u4f7f\u3044\u52dd\u624b\u304c\u826f\u3055\u305d\u3046\u3067\u3059\u3002 \u753b\u9762\u9077\u79fb\u306e\u5b9f\u88c5\u306f\u3001Flutter cookbook \u306e Navig <\/span>Continue Reading<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[38],"tags":[],"_links":{"self":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts\/610"}],"collection":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/comments?post=610"}],"version-history":[{"count":0,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts\/610\/revisions"}],"wp:attachment":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/media?parent=610"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/categories?post=610"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/tags?post=610"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}StatefullWidget<\/code>\u3067\u9077\u79fb\u5143\u753b\u9762\uff08route\uff09\u306e\u72b6\u614b\uff08state\uff09\u3092\u7ba1\u7406\u3057\u3064\u3064\u3001\u5225\u753b\u9762\u306b\u9077\u79fb\u3059\u308b\u969b\u306b\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u6e21\u3059\u65b9\u6cd5\u3067\u3059\u3002\u3053\u306e\u65b9\u6cd5\u3060\u3068\u9077\u79fb\u5143\u753b\u9762\u306e\u72b6\u614b\uff08state\uff09\u304c\u753b\u9762\u9077\u79fb\u6642\u306b\u7834\u68c4\u3055\u308c\u308b\u3082\u306e\u3060\u3068\u601d\u3063\u3066\u3044\u305f\u306e\u3067\u3059\u304c\u3001
maintainState=true<\/code>\u306e\u8a2d\u5b9a\u3067\u3042\u308c\u3070\u7834\u68c4\u3055\u308c\u306a\u3044\u3088\u3046\u306a\u306e\u3067\u3001\u7d50\u69cb\u4f7f\u3044\u52dd\u624b\u304c\u826f\u3055\u305d\u3046\u3067\u3059\u3002<\/p>\n
child: TextButton(\n child: Text('edit'),\n onPressed: () async {\n \/\/ EditPage\u306b\u9077\u79fb\u3057\u305f\u3057\u3001\u623b\u3063\u3066\u304d\u305f\u969b\u306b\u7d50\u679c\u3092result\u3067\u53d7\u3051\u53d6\u3063\u3066\u3044\u308b\n final result = await Navigator.push(\n context,\n MaterialPageRoute(builder: (context) => EditPage(_studyTime, _playTime), maintainState: false, fullscreenDialog: true), \/\/ EditPage\u306b\u9077\u79fb\u3059\u308b\u969b\u306b\u3001 _studyTime \u3068 _playTime \u3092\u6e21\u3057\u3066\u3044\u308b\n );\n\n \/\/ \u7d50\u679c\u304c\u306a\u3051\u308c\u3070\u4f55\u3082\u3057\u306a\u3044\n if (result == null) return;\n\n \/\/ \u7d50\u679c\u3092state\u306b\u53cd\u6620\u3057\u3066\u753b\u9762\u306b\u53cd\u6620\n setState(() {\n _studyTime = result['studyTime'];\n _playTime = result['playTime'];\n _differenceTime = DateTime.utc(0, 0, 0).add(_studyTime.difference(_playTime));\n });\n },\n),<\/code><\/pre>\n
\n
MaterialPageRoute<\/code>\u306e\u5f15\u6570
maintainState<\/code>\u306e\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u304c
true<\/code>\u306a\u306e\u3067\u3001\u9077\u79fb\u5143\u753b\u9762\u306f\u7834\u68c4\u3055\u308c\u305a\u306b\u30e1\u30e2\u30ea\u306b\u30ad\u30fc\u30d7\u3055\u308c\u308b\u3089\u3057\u3044
\n\n
\nIf this is true, then the route is maintained, so that any futures it is holding from the next route will properly resolve when the next route pops. If this is not necessary, this can be set to false to allow the framework to entirely discard the route's widget hierarchy when it is not visible.<\/p>\n<\/blockquote>\n<\/li>\nEditPage<\/code>\u306e\u30a4\u30cb\u30b7\u30e3\u30e9\u30a4\u30b6\u306b\u5fc5\u8981\u306a\u30d1\u30e9\u30e1\u30fc\u30bf\uff08
_studyTime<\/code>\u30fb
_playTime<\/code>\uff09\u3092\u6e21\u3057\u3066\u3044\u308b<\/li>\n
Navigator.pup()<\/code>\u3067\u623b\u3063\u305f\u969b\u306e\u51e6\u7406\u7d50\u679c\u3092\u3001
async-await<\/code>\u3067\u540c\u671f\u7684\u306b\u53d7\u3051\u53d6\u3063\u3066\u3044\u308b\n
\n
\n<\u30a4\u30cb\u30b7\u30e3\u30e9\u30a4\u30b6><\/p>\nclass EditPage extends StatefulWidget {\n final DateTime studyTime;\n final DateTime playTime;\n EditPage(this.studyTime, this.playTime);\n}<\/code><\/pre>\n
child: ElevatedButton(\n child: Text('Save'),\n onPressed: () => {\n Navigator.pop(context, {'studyTime': _studyTime, 'playTime': _playTime}) \/\/ \u51e6\u7406\u7d50\u679c\u3092\u6307\u5b9a\n },\n),<\/code><\/pre>\n
\n
Navigator.pup()<\/code>\u306e\u7b2c\u4e8c\u5f15\u6570\u306b\u51e6\u7406\u7d50\u679c\u3092\u6307\u5b9a\u3059\u308b<\/li>\n<\/ul>\n
\n\u305d\u306e\u3088\u3046\u306a\u5834\u5408\u306f\u3001Provider\u3067\u72b6\u614b\u7ba1\u7406\u3057\u3064\u3064\u3001\u7570\u306a\u308bRoute\u3067\u5171\u6709\u3059\u308b\u6b21\u306b\u7d39\u4ecb\u3059\u308b\u65b9\u6cd5\u304c\u826f\u3055\u305d\u3046\u3067\u3059\u3002<\/p>\nProvider\u3067\u72b6\u614b\u7ba1\u7406\u3057\u3001\u7570\u306a\u308bRoute\u3067\u5171\u6709\u3059\u308b\u65b9\u6cd5<\/h2>\n
Provider \u3067\u72b6\u614b\u7ba1\u7406\u3059\u308b<\/h3>\n
StatelessWidget<\/code>\u3067\u72b6\u614b\u7ba1\u7406\u3057\u3066\u3044\u305f\u306e\u3067\u3001Provider \u3067\u72b6\u614b\u7ba1\u7406\u3059\u308b\u3088\u3046\u306b\u5909\u66f4\u3057\u3066\u3044\u304d\u307e\u3059\u3002
\n\u203b \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u306f\u629c\u7c8b\u3067\u3059\u306e\u3067\u3001\u5168\u6587\u3092\u307f\u305f\u3044\u5834\u5408\u306f \u3053\u3061\u3089<\/a> \u3068 \u3053\u3061\u3089<\/a> \u3092\u53c2\u7167\u304f\u3060\u3055\u3044\u3002<\/p>\ndependencies:\n provider: ^5.0.0<\/code><\/pre>\n
ChangeNotifier<\/code>\u3092\u7d99\u627f\u3057\u305f\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u3092\u4f5c\u308a\u307e\u3059\u3002<\/p>\n
\n
notifyListeners()<\/code> \u3092\u547c\u3073\u51fa\u3057\u3001\u5909\u66f4\u3092 Provider \u306b\u4f1d\u3048\u308b<\/li>\n<\/ul>\n
class TimeKeeper extends ChangeNotifier {\n DateTime _studyTime = DateTime.utc(0, 0, 0);\n DateTime _playTime = DateTime.utc(0, 0, 0);\n\n DateTime get studyTime => _studyTime;\n set studyTime(DateTime datetime) {\n _studyTime = datetime;\n notifyListeners();\n }\n\n DateTime get playTime => _playTime;\n set playTime(DateTime datetime) {\n _playTime = datetime;\n notifyListeners();\n }\n}<\/code><\/pre>\n
\n
MaterialApp<\/code>\u306e
home<\/code>\u306b Widget \u306e\u4ee3\u308f\u308a\u306b\u3001
ChangeNotifierProvider<\/code> \u3092\u6307\u5b9a\u3059\u308b<\/li>\n
create<\/code>\u306b\u3001
ChangeNotifier<\/code>\u3092\u7d99\u627f\u3057\u305f\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u3092\u521d\u671f\u5316\u3059\u308b\u95a2\u6570\u3092\u6307\u5b9a\u3066\u3044\u308b<\/li>\n
StatelessWidget<\/code>\u306e
build()<\/code>\u5185\u306e
TimeKeeper timeKeeper = context.watch<TimeKeeper>();<\/code>\u3067Provider\u3067\u7ba1\u7406\u3057\u3066\u3044\u308b\u30e2\u30c7\u30eb\u3092\u3001\u5909\u66f4\u3092\u76e3\u8996\u3059\u308b\u5f62\u3067\u53d6\u5f97\u3057\u3066\u3044\u308b\n
\n
context.read()<\/code>\u3067\u53d6\u5f97\u3059\u308b<\/li>\n
build()<\/code> \u4ee5\u5916\u3067\u30a2\u30af\u30bb\u30b9\u3057\u305f\u3044\u5834\u5408\u306f\u3001
Provider.of<\/code>\u3092\u5229\u7528\u3059\u308b<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n
void main() async {\n runApp(\n MaterialApp(\n home: ChangeNotifierProvider( \/\/ Widget \u3067\u306f\u306a\u304f\u3001ChangeNotifierProvider\u3092\u6307\u5b9a\n create: (context) => TimeKeeper(), \/\/ ChangeNotifier \u3092\u7d99\u627f\u3057\u305f\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\n child: TimerPage(),\n )));\n}\n\n\/\/\/ \u30bf\u30a4\u30de\u30fc\u30da\u30fc\u30b8\nclass TimerPage extends StatelessWidget {\n @override\n Widget build(BuildContext context) {\n TimeKeeper timeKeeper = context.watch<TimeKeeper>(); \/\/ \u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u3092\u53d6\u5f97\n return Column(\n children: <Widget>[\n Text(timeKeeper.studyTime), \/\/ \u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u306bgetter\u3092\u901a\u3058\u3066\u30a2\u30af\u30bb\u30b9\n Text(timeKeeper.playTime), \/\/ \u540c\u4e0a\n ])\n );\n}<\/code><\/pre>\n
\u753b\u9762\u9077\u79fb\u6642\u306b Provider \u3092\u6e21\u3059<\/h3>\n
\n
ChangeNotifierProvider.value()<\/code>\u3092\u4f7f\u3063\u3066\u3001\u65e2\u5b58\u306e\u30e2\u30c7\u30eb\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u6307\u5b9a\u3057\u3066\u3001Provider \u3092\u4f5c\u6210\u3059\u308b\n
\n
Widget build(BuildContext context) {\n TimeKeeper timeKeeper = context.watch<TimeKeeper>();\n\n \/\/ \u3044\u308d\u3044\u308d\u7701\u7565\n\n TextButton(\n child: Text('edit'),\n onPressed: () {\n Navigator.push(\n context,\n MaterialPageRoute(\n builder: (context) => ChangeNotifierProvider.value(\n value: timeKeeper, \/\/ \u65e2\u5b58\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u6307\u5b9a\n child: EditPage(),\n )),\n );\n }\n )\n)<\/code><\/pre>\n
\n\u4eca\u56de\u306f\u3001\u753b\u9762\u5185\u3067\u7de8\u96c6\u3057\u3066\u6700\u7d42\u7684\u306b Save \u30dc\u30bf\u30f3\u3092\u30af\u30ea\u30c3\u30af\u3057\u305f\u3089\u53cd\u6620\u3059\u308b\u4ed5\u7d44\u307f\u306a\u306e\u3067\u3001StatefulWidget<\/code>\u3092\u4f7f\u3063\u3066\u5b9f\u88c5\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n
\n
didChangeDependencies<\/code>\u306b\u3066\u884c\u3046\n
\n
StatelessWidget<\/code>\u3092\u4f7f\u3046\u5834\u5408\u306f\u3001
build()<\/code>\u306e\u6700\u521d\u3067\u521d\u671f\u5316\u51e6\u7406\u3092\u884c\u3048\u3070OK\u3067\u3059<\/li>\n<\/ul>\n<\/li>\n
build()<\/code>\u3067 Widget\u30c4\u30ea\u30fc\u3092\u69cb\u7bc9\u3059\u308b\u969b\u306b\u3001
context.watch<TimeKeeper>()<\/code>\u3067 Provider \u304b\u3089\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u3092\u3001\u76e3\u8996\u3059\u308b\u5f62\u3067\u53d6\u5f97\u3057\u3066\u304a\u304f<\/li>\n
Save<\/code> \u30dc\u30bf\u30f3\u3092\u30bf\u30c3\u30d7\u3057\u305f\u969b\u306b\u3001\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u306e setter \u3092\u547c\u3073\u51fa\u3057\uff08
timeKeeper.studyTime = _studyTime<\/code>\uff09\u3066\u3001\u30e2\u30c7\u30eb\u306b\u53cd\u6620\u3057\u3066\u3044\u308b<\/li>\n
notifyListeners()<\/code> \u3057\u3066\u308b\u306e\u3067\u3001\u3053\u306e\u30e2\u30c7\u30eb\u3092 Listen \u3057\u3066\u3044\u308b Widget \u306b\u5909\u66f4\u304c\u901a\u77e5\u3055\u308c\u308b<\/li>\n<\/ul>\n
class EditPage extends StatefulWidget {\n @override\n _EditPageState createState() => _EditPageState();\n}\n\nclass _EditPageState extends State<EditPage> {\n DateTime _studyTime; \/\/ \u3053\u306e\u30da\u30fc\u30b8\u5185\u3067\u7ba1\u7406\u3059\u308b\u72b6\u614b\n DateTime _playTime; \/\/ \u540c\u4e0a\n\n @override\n void didChangeDependencies() {\n TimeKeeper timeKeeper = context.read<TimeKeeper>(); \/\/ \u3053\u3053\u3067context\u304c\u5fc5\u8981\u3002\u53c2\u7167\u3060\u3051\u306a\u306e\u3067`read`\u3092\u5229\u7528\n _studyTime = timeKeeper.studyTime;\n _playTime = timeKeeper.playTime;\n super.didChangeDependencies();\n }\n\n @override\n Widget build(BuildContext context) {\n TimeKeeper timeKeeper = context.watch<TimeKeeper>();\n\n \/\/ \u3044\u308d\u3044\u308d\u7701\u7565\n\n child: ElevatedButton(\n child: Text('Save'),\n onPressed: () {\n timeKeeper.studyTime = _studyTime; \/\/ \u3053\u3053\u3067Provider\u3067\u7ba1\u7406\u3059\u308b\u30e2\u30c7\u30eb\u306b\u53cd\u6620\n timeKeeper.playTime = _playTime;\n Navigator.pop(context); \/\/ \u5143\u753b\u9762\u306b\u623b\u308b\uff08\u51e6\u7406\u7d50\u679c\u306f\u8fd4\u3055\u306a\u3044\uff09\n },\n )\n}<\/code><\/pre>\n
\nProvider \u81ea\u4f53\u3082\u3001\u5c0e\u5165\u3059\u308b\u3053\u3068\u3067\u753b\u9762\u3068\u30ed\u30b8\u30c3\u30af\u304c\u5f37\u5236\u7684\u306b\u5206\u96e2\u3055\u308c\u3066\u3001\u30b3\u30fc\u30c9\u306e\u898b\u901a\u3057\u304c\u826f\u304f\u306a\u3063\u305f\u6c17\u304c\u3059\u308b\u306e\u3067\u3088\u304b\u3063\u305f\u3067\u3059\u3002<\/p>\n\u53c2\u8003: Provider \u3068 WidgetsBindingObserver \u3068\u4f75\u7528\u3059\u308b<\/h2>\n
WidgetsBindingObserver<\/code>\u3092\u4f7f\u3063\u3066\u57cb\u3081\u8fbc\u3093\u3067\u3044\u305f\u306e\u3067\u3059\u304c\u3001Provider \u3068\u4f75\u7528\u3059\u308b\u969b\u306b\u5c11\u3057\u5de5\u592b\u304c\u5fc5\u8981\u3067\u3057\u305f\u3002<\/p>\n
StatelessWidget<\/code>\u3067\u306f
didChangeAppLifecycleState()<\/code>\u306e\u30bf\u30a4\u30df\u30f3\u30b0\u3067
context<\/code>\u306b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u305a\u3001\u7d50\u679c\u3068\u3057\u3066 Provider \u3067\u7ba1\u7406\u3057\u3066\u3044\u308b\u30e2\u30c7\u30eb\u306b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u306a\u3044\u305f\u3081\u3067\u3059\u3002<\/p>\n
StatefullWidget<\/code> \u3068 Provider \u3092\u5171\u5b58\u3055\u305b\u308b\u5f62\u306b\u3057\u307e\u3057\u305f\u3002<\/p>\n
\n
WidgetsBindingObserver<\/code>\u3092 mix-in \u3059\u308b<\/li>\n
initState()<\/code>\u3067
WidgetsBinding.instance.addObserver(this)<\/code>\u3092\u3001
dispose()<\/code>\u3067
WidgetsBinding.instance.removeObserver(this)<\/code>\u3092\u5b9f\u884c\u3059\u308b<\/li>\n
didChangeAppLifecycleState()<\/code>\u3067 Provider \u304b\u3089\u30e2\u30c7\u30eb\u3092\u53d6\u5f97\u3059\u308b<\/li>\n<\/ul>\n
class TimerApp extends StatelessWidget {\n @override\n Widget build(BuildContext context) {\n return MaterialApp(\n title: 'Study Timer',\n home: ChangeNotifierProvider(\n create: (context) => TimeKeeper(),\n child: TimerPage(),\n ));\n }\n}\n\n\/\/\/ \u30bf\u30a4\u30de\u30fc\u30da\u30fc\u30b8\nclass TimerPage extends StatefulWidget {\n @override\n _TimerPageState createState() => _TimerPageState();\n}\n\nclass _TimerPageState extends State<TimerPage> with WidgetsBindingObserver {\n @override\n void initState() {\n super.initState();\n WidgetsBinding.instance.addObserver(this);\n }\n\n @override\n void dispose() {\n WidgetsBinding.instance.removeObserver(this);\n super.dispose();\n }\n\n @override\n void didChangeAppLifecycleState(AppLifecycleState state) {\n TimeKeeper timeKeeper = context.read<TimeKeeper>(); \/\/ \u3053\u3053\u3067Provider\u304b\u3089\u30e2\u30c7\u30eb\u3092\u53d6\u5f97\n if (state == AppLifecycleState.paused) {\n timeKeeper.handleOnPaused();\n } else if (state == AppLifecycleState.resumed) {\n timeKeeper.handleOnResumed();\n }\n }\n\n @override\n Widget build(BuildContext context) {\n TimeKeeper timeKeeper = context.watch<TimeKeeper>();\n return Scaffold(\n \/\/ \u7701\u7565\n )\n }\n}<\/code><\/pre>\n
\u3055\u3044\u3054\u306b<\/h2>\n
StatefullWidget<\/code>\u3068
Provider<\/code>\u3069\u3063\u3061\u304c\u3044\u3044\u304b\uff1f\u3068\u3044\u3046\u3053\u3068\u306a\u6c17\u304c\u3057\u307e\u3057\u305f\u3002
\n\u305d\u3046\u3044\u3046\u610f\u5473\u3067\u306f\u3001\u7d50\u5c40\u8907\u96d1\u306b\u306a\u3063\u3066\u304d\u305f\u3089\u3001Provider<\/code>\u3067\u7ba1\u7406\u3057\u305f\u304f\u306a\u308b\u3053\u3068\u3092\u8003\u3048\u308b\u3068\u3001\u6700\u521d\u304b\u3089
Provider<\/code>\u3067\u5b9f\u88c5\u3057\u3066\u304a\u304f\u306e\u304c\u826f\u3055\u305d\u3046\u3060\u306a\u3001\u3001\u3068\u3044\u3046\u306e\u304c\u6240\u611f\u3067\u3059\u3002<\/p>\n