{"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

StatefullWidget + \u753b\u9762\u9077\u79fb\u6642\u306b\u30d1\u30e9\u30e1\u30fc\u30bf\u6e21\u3057\u3059\u308b\u65b9\u6cd5<\/h2>\n

\u4e00\u3064\u76ee\u306e\u65b9\u6cd5\u306f\u3001StatefullWidget<\/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\u3001maintainState=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

\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

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