📅  最后修改于: 2022-03-11 14:56:43.536000             🧑  作者: Mango
void main() {
runApp(
RootRestorationScope(
restorationId: 'root',
child: MaterialApp(home: HomePage()),
),
);
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State with RestorationMixin {
final RestorableInt _counter = RestorableInt(0);
@override
String get restorationId => 'HomePage';
@override
void restoreState(RestorationBucket oldBucket, bool initialRestore) {
registerForRestoration(_counter, 'counter');
}
@override
void dispose() {
_counter.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ElevatedButton(
child: Text('${_counter.value}'),
onPressed: () => setState(() => ++_counter.value),
),
),
);
}
}