mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00

This auto-formats all *.dart files in the repository outside of the `engine` subdirectory and enforces that these files stay formatted with a presubmit check. **Reviewers:** Please carefully review all the commits except for the one titled "formatted". The "formatted" commit was auto-generated by running `dev/tools/format.sh -a -f`. The other commits were hand-crafted to prepare the repo for the formatting change. I recommend reviewing the commits one-by-one via the "Commits" tab and avoiding Github's "Files changed" tab as it will likely slow down your browser because of the size of this PR. --------- Co-authored-by: Kate Lovett <katelovett@google.com> Co-authored-by: LongCatIsLooong <31859944+LongCatIsLooong@users.noreply.github.com>
90 lines
2.7 KiB
Dart
90 lines
2.7 KiB
Dart
// Copyright 2014 The Flutter Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
/// Flutter code sample for [RestorationMixin].
|
|
|
|
void main() => runApp(const RestorationExampleApp());
|
|
|
|
class RestorationExampleApp extends StatelessWidget {
|
|
const RestorationExampleApp({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return const MaterialApp(
|
|
restorationScopeId: 'app',
|
|
title: 'Restorable Counter',
|
|
home: RestorableCounter(restorationId: 'counter'),
|
|
);
|
|
}
|
|
}
|
|
|
|
class RestorableCounter extends StatefulWidget {
|
|
const RestorableCounter({super.key, this.restorationId});
|
|
|
|
final String? restorationId;
|
|
|
|
@override
|
|
State<RestorableCounter> createState() => _RestorableCounterState();
|
|
}
|
|
|
|
// The [State] object uses the [RestorationMixin] to make the current value
|
|
// of the counter restorable.
|
|
class _RestorableCounterState extends State<RestorableCounter> with RestorationMixin {
|
|
// The current value of the counter is stored in a [RestorableProperty].
|
|
// During state restoration it is automatically restored to its old value.
|
|
// If no restoration data is available to restore the counter from, it is
|
|
// initialized to the specified default value of zero.
|
|
final RestorableInt _counter = RestorableInt(0);
|
|
|
|
// In this example, the restoration ID for the mixin is passed in through
|
|
// the [StatefulWidget]'s constructor.
|
|
@override
|
|
String? get restorationId => widget.restorationId;
|
|
|
|
@override
|
|
void restoreState(RestorationBucket? oldBucket, bool initialRestore) {
|
|
// All restorable properties must be registered with the mixin. After
|
|
// registration, the counter either has its old value restored or is
|
|
// initialized to its default value.
|
|
registerForRestoration(_counter, 'count');
|
|
}
|
|
|
|
void _incrementCounter() {
|
|
setState(() {
|
|
// The current value of the property can be accessed and modified via
|
|
// the value getter and setter.
|
|
_counter.value++;
|
|
});
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_counter.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(title: const Text('Restorable Counter')),
|
|
body: Center(
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: <Widget>[
|
|
const Text('You have pushed the button this many times:'),
|
|
Text('${_counter.value}', style: Theme.of(context).textTheme.headlineMedium),
|
|
],
|
|
),
|
|
),
|
|
floatingActionButton: FloatingActionButton(
|
|
onPressed: _incrementCounter,
|
|
tooltip: 'Increment',
|
|
child: const Icon(Icons.add),
|
|
),
|
|
);
|
|
}
|
|
}
|