[State Restoration] Code sample templates, migrate existing code samples (#78757)

* Create state restoration templates, 

* Update existing state restoration samples to use the new templates

* Add to README, decided to require stateful widgets to include RestorationMixin
This commit is contained in:
Shi-Hao Hong
2021-03-23 09:24:34 +08:00
committed by GitHub
parent 7a88eb97ea
commit 49adef6792
9 changed files with 405 additions and 388 deletions

View File

@@ -65,7 +65,7 @@ follows:
This is a simple template for which you provide everything. It has no code of
its own, just the sections for `imports`, `main`, and `preamble`. You must
provide the `main` section to have a `main()`.
### WidgetsApp Templates
These templates create a `WidgetsApp` that encloses the snippet widget. These templates import
@@ -82,7 +82,7 @@ the widgets library.
`build()` method, and any state variables. It also has an `imports`
section to import additional packages. Please only import things that are part
of Flutter or part of default dependencies for a `flutter create` project.
- [`stateful_widget_ticker`](stateful_widget_ticker.tmpl) : Identical to the
`stateful_widget` template, with the addition of the `TickerProviderStateMixin`
class, enabling easy generation of animated samples.
@@ -104,11 +104,18 @@ These templates follow the same conventions as the `WidgetsApp` templates above
`MaterialApp` instead. These templates import the material library.
- [`stateful_widget_material`](stateful_widget_material.tmpl)
- [`stateful_widget_material_ticker`](stateful_widget_material_ticker.tmpl)
- [`stateless_widget_material`](stateless_widget_material.tmpl)
- [`stateful_widget_restoration_material`](stateful_widget_restoration_material.tmpl) :
Similar to the `stateful_widget_restoration` template, but for `MaterialApp`.
- [`stateless_widget_restoration_material`](stateful_widget_restoration_material.tmpl) :
Similar to the `stateless_widget` template, but the `MaterialApp` has `restorationScopeId`
defined.
- [`stateful_widget_scaffold`](stateful_widget_scaffold.tmpl) : Adds a `Scaffold` widget as the home
of the enclosing `MaterialApp` to wrap the stateful widget snippet. The `Scaffold` widget contains
an `AppBar`.
@@ -132,17 +139,24 @@ These templates follow the same conventions as the `WidgetsApp` templates above
These templates follow the same conventions as the `WidgetsApp` templates above but use a
`CupertinoApp` instead. These templates import the Cupertino library.
- [`stateful_widget_cupertino`](stateful_widget_cupertino.tmpl)
- [`stateful_widget_cupertino_ticker`](stateful_widget_cupertino_ticker.tmpl)
- [`stateless_widget_cupertino`](stateless_widget_cupertino.tmpl)
- [`stateful_widget_cupertinoPageScaffold`](stateful_widget_cupertino_page_scaffold.tmpl) : Similar to
`stateful_widget_cupertino`, except that it wraps the stateful widget with a
`CupertinoPageScaffold`.
- [`stateless_widget_cupertinoPageScaffold`](stateless_widget_cupertino_page_scaffold.tmpl) : Similar to
`stateless_widget_cupertino`, except that it wraps the stateless widget with a
`CupertinoPageScaffold`.
- [`stateless_widget_restoration_cupertino`](stateful_widget_restoration_material.tmpl) :
Similar to the `stateless_widget` template, but the `CupertinoApp` has `restorationScopeId`
defined.
- [`stateful_widget_restoration_cupertino`](stateful_widget_restoration_material.tmpl) :
Similar to the `stateful_widget_restoration` template, but for `CupertinoApp`.

View File

@@ -0,0 +1,49 @@
/// Flutter code sample for {{element}}
{{description}}
{{code-dartImports}}
import 'package:flutter/cupertino.dart';
{{code-imports}}
void main() => runApp(const MyApp());
/// This is the main application widget.
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static const String _title = 'Flutter Code Sample';
@override
Widget build(BuildContext context) {
return const CupertinoApp(
restorationScopeId: 'app',
title: _title,
home: MyStatefulWidget(restorationId: 'main'),
);
}
}
{{code-preamble}}
/// This is the stateful widget that the main application instantiates.
class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({Key? key, this.restorationId}) : super(key: key);
final String? restorationId;
@override
_MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}
/// This is the private State class that goes with MyStatefulWidget.
/// RestorationProperty objects can be used because of RestorationMixin.
class _MyStatefulWidgetState extends State<MyStatefulWidget> with RestorationMixin {
// In this example, the restoration ID for the mixin is passed in through
// the [StatefulWidget]'s constructor.
@override
String? get restorationId => widget.restorationId;
{{code}}
}

View File

@@ -0,0 +1,49 @@
/// Flutter code sample for {{element}}
{{description}}
{{code-dartImports}}
import 'package:flutter/material.dart';
{{code-imports}}
void main() => runApp(const MyApp());
/// This is the main application widget.
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static const String _title = 'Flutter Code Sample';
@override
Widget build(BuildContext context) {
return const MaterialApp(
restorationScopeId: 'app',
title: _title,
home: MyStatefulWidget(restorationId: 'main'),
);
}
}
{{code-preamble}}
/// This is the stateful widget that the main application instantiates.
class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({Key? key, this.restorationId}) : super(key: key);
final String? restorationId;
@override
_MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}
/// This is the private State class that goes with MyStatefulWidget.
/// RestorationProperty objects can be used because of RestorationMixin.
class _MyStatefulWidgetState extends State<MyStatefulWidget> with RestorationMixin {
// In this example, the restoration ID for the mixin is passed in through
// the [StatefulWidget]'s constructor.
@override
String? get restorationId => widget.restorationId;
{{code}}
}

View File

@@ -0,0 +1,36 @@
/// Flutter code sample for {{element}}
{{description}}
{{code-dartImports}}
import 'package:flutter/cupertino.dart';
{{code-imports}}
void main() => runApp(const MyApp());
/// This is the main application widget.
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static const String _title = 'Flutter Code Sample';
@override
Widget build(BuildContext context) {
return const CupertinoApp(
restorationScopeId: 'app',
title: _title,
home: MyStatelessWidget(),
);
}
}
{{code-preamble}}
/// This is the stateless widget that the main application instantiates.
class MyStatelessWidget extends StatelessWidget {
const MyStatelessWidget({Key? key}) : super(key: key);
@override
{{code}}
}

View File

@@ -0,0 +1,36 @@
/// Flutter code sample for {{element}}
{{description}}
{{code-dartImports}}
import 'package:flutter/material.dart';
{{code-imports}}
void main() => runApp(const MyApp());
/// This is the main application widget.
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static const String _title = 'Flutter Code Sample';
@override
Widget build(BuildContext context) {
return const MaterialApp(
restorationScopeId: 'app',
title: _title,
home: MyStatelessWidget(),
);
}
}
{{code-preamble}}
/// This is the stateless widget that the main application instantiates.
class MyStatelessWidget extends StatelessWidget {
const MyStatelessWidget({Key? key}) : super(key: key);
@override
{{code}}
}