Flutter RootRestorationScope未恢复最后一个根



我使用的是这个插件https://pub.dev/packages/lastrongtate,但当我更新到flutter 1.22时,出现了一个新功能RootRestorationScope,它似乎可以取代那个外部插件,但在测试它并遵循一些教程(tuto1,tuto2(后,我注意到它恢复了状态,但没有恢复路线!我是不是错过了什么?有一个例子:

import 'package:flutter/material.dart';
void main() => runApp(
RootRestorationScope( // Register a restoration scope for the entire app!
restorationId: 'root',
child: MyApp(),
),
);
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: DummyPage(),
);
}
}
class DummyPage extends StatelessWidget {
static const route = "/intermediate";
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Another page'),
),
body: RaisedButton(
child: Text("Onwards"),
onPressed: () {
Navigator.of(context, rootNavigator: false).push(
MaterialPageRoute<bool>(
fullscreenDialog: true,
builder: (BuildContext context) => HomePage(),
),
);
},
),
);
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
// Our state should be mixed-in with RestorationMixin
class _HomePageState extends State<HomePage> with RestorationMixin {
// For each state, we need to use a restorable property
final RestorableInt _index = RestorableInt(0);
@override
Widget build(BuildContext context) {
return RestorationScope(
restorationId: 'tess',
child: Scaffold(
body: Center(child: Text('Index is ${_index.value}')),
bottomNavigationBar: BottomNavigationBar(
currentIndex: _index.value,
onTap: (i) => setState(() => _index.value = i),
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home'
),
BottomNavigationBarItem(
icon: Icon(Icons.notifications),
label: 'Notifications'
),
BottomNavigationBarItem(
icon: Icon(Icons.settings),
label: 'Settings'
),
],
),
),
);
}
@override
// The restoration bucket id for this page,
// let's give it the name of our page!
String get restorationId => 'home_page';
@override
void restoreState(RestorationBucket oldBucket, bool initialRestore) {
// Register our property to be saved every time it changes,
// and to be restored every time our app is killed by the OS!
registerForRestoration(_index, 'nav_bar_index');
}
}

使用restorablePush而不是push,并为每条路由提供一个restorationId

添加了经典命令式API(推送、推命名和朋友(永远无法恢复他们的状态。添加了可恢复的命令式API(可恢复的Push、可恢复的PUshNamed和所有其它命令式方法;可恢复的";以他们的名义(恢复如果其下方的所有路线(包括第一条(将恢复其下方基于页面的路由。如果没有基于页面路由到它下面,只有当它下面的所有路由都在时,它才会恢复其状态恢复他们的。

https://api.flutter.dev/flutter/widgets/Navigator-class.html

最新更新