Unverified Commit af1c629d authored by Shi-Hao Hong's avatar Shi-Hao Hong Committed by GitHub

[State Restoration] Adds remaining Restorable{Property}N (#72708)

parent 0f5b79a2
...@@ -209,6 +209,10 @@ class _RestorablePrimitiveValue<T extends Object> extends _RestorablePrimitiveVa ...@@ -209,6 +209,10 @@ class _RestorablePrimitiveValue<T extends Object> extends _RestorablePrimitiveVa
/// Instead of using the more generic [RestorableNum] directly, consider using /// Instead of using the more generic [RestorableNum] directly, consider using
/// one of the more specific subclasses (e.g. [RestorableDouble] to store a /// one of the more specific subclasses (e.g. [RestorableDouble] to store a
/// [double] and [RestorableInt] to store an [int]). /// [double] and [RestorableInt] to store an [int]).
///
/// See also:
///
/// * [RestorableNumN] for the nullable version of this class.
class RestorableNum<T extends num> extends _RestorablePrimitiveValue<T> { class RestorableNum<T extends num> extends _RestorablePrimitiveValue<T> {
/// Creates a [RestorableNum]. /// Creates a [RestorableNum].
/// ///
...@@ -222,6 +226,10 @@ class RestorableNum<T extends num> extends _RestorablePrimitiveValue<T> { ...@@ -222,6 +226,10 @@ class RestorableNum<T extends num> extends _RestorablePrimitiveValue<T> {
/// A [RestorableProperty] that knows how to store and restore a [double]. /// A [RestorableProperty] that knows how to store and restore a [double].
/// ///
/// {@macro flutter.widgets.RestorableNum} /// {@macro flutter.widgets.RestorableNum}
///
/// See also:
///
/// * [RestorableDoubleN] for the nullable version of this class.
class RestorableDouble extends RestorableNum<double> { class RestorableDouble extends RestorableNum<double> {
/// Creates a [RestorableDouble]. /// Creates a [RestorableDouble].
/// ///
...@@ -232,6 +240,10 @@ class RestorableDouble extends RestorableNum<double> { ...@@ -232,6 +240,10 @@ class RestorableDouble extends RestorableNum<double> {
/// A [RestorableProperty] that knows how to store and restore an [int]. /// A [RestorableProperty] that knows how to store and restore an [int].
/// ///
/// {@macro flutter.widgets.RestorableNum} /// {@macro flutter.widgets.RestorableNum}
///
/// See also:
///
/// * [RestorableIntN] for the nullable version of this class.
class RestorableInt extends RestorableNum<int> { class RestorableInt extends RestorableNum<int> {
/// Creates a [RestorableInt]. /// Creates a [RestorableInt].
/// ///
...@@ -242,6 +254,10 @@ class RestorableInt extends RestorableNum<int> { ...@@ -242,6 +254,10 @@ class RestorableInt extends RestorableNum<int> {
/// A [RestorableProperty] that knows how to store and restore a [String]. /// A [RestorableProperty] that knows how to store and restore a [String].
/// ///
/// {@macro flutter.widgets.RestorableNum} /// {@macro flutter.widgets.RestorableNum}
///
/// See also:
///
/// * [RestorableStringN] for the nullable version of this class.
class RestorableString extends _RestorablePrimitiveValue<String> { class RestorableString extends _RestorablePrimitiveValue<String> {
/// Creates a [RestorableString]. /// Creates a [RestorableString].
/// ///
...@@ -252,14 +268,14 @@ class RestorableString extends _RestorablePrimitiveValue<String> { ...@@ -252,14 +268,14 @@ class RestorableString extends _RestorablePrimitiveValue<String> {
/// A [RestorableProperty] that knows how to store and restore a [bool]. /// A [RestorableProperty] that knows how to store and restore a [bool].
/// ///
/// {@macro flutter.widgets.RestorableNum} /// {@macro flutter.widgets.RestorableNum}
///
/// See also:
///
/// * [RestorableBoolN] for the nullable version of this class.
class RestorableBool extends _RestorablePrimitiveValue<bool> { class RestorableBool extends _RestorablePrimitiveValue<bool> {
/// Creates a [RestorableBool]. /// Creates a [RestorableBool].
/// ///
/// {@macro flutter.widgets.RestorableNum.constructor} /// {@macro flutter.widgets.RestorableNum.constructor}
///
/// See also:
///
/// * [RestorableBoolN] for the nullable version of this class.
RestorableBool(bool defaultValue) : assert(defaultValue != null), super(defaultValue); RestorableBool(bool defaultValue) : assert(defaultValue != null), super(defaultValue);
} }
...@@ -267,15 +283,79 @@ class RestorableBool extends _RestorablePrimitiveValue<bool> { ...@@ -267,15 +283,79 @@ class RestorableBool extends _RestorablePrimitiveValue<bool> {
/// nullable. /// nullable.
/// ///
/// {@macro flutter.widgets.RestorableNum} /// {@macro flutter.widgets.RestorableNum}
///
/// See also:
///
/// * [RestorableBool] for the non-nullable version of this class.
class RestorableBoolN extends _RestorablePrimitiveValueN<bool?> { class RestorableBoolN extends _RestorablePrimitiveValueN<bool?> {
/// Creates a [RestorableBoolN]. /// Creates a [RestorableBoolN].
/// ///
/// {@macro flutter.widgets.RestorableNum.constructor} /// {@macro flutter.widgets.RestorableNum.constructor}
RestorableBoolN(bool? defaultValue) : super(defaultValue);
}
/// A [RestorableProperty] that knows how to store and restore a [num]
/// that is nullable.
///
/// {@macro flutter.widgets.RestorableNum}
///
/// Instead of using the more generic [RestorableNumN] directly, consider using
/// one of the more specific subclasses (e.g. [RestorableDoubleN] to store a
/// [double] and [RestorableIntN] to store an [int]).
///
/// See also:
///
/// * [RestorableNum] for the non-nullable version of this class.
class RestorableNumN<T extends num?> extends _RestorablePrimitiveValueN<num?> {
/// Creates a [RestorableNumN].
///
/// {@macro flutter.widgets.RestorableNum.constructor}
RestorableNumN(num? defaultValue) : super(defaultValue);
}
/// A [RestorableProperty] that knows how to store and restore a [double]
/// that is nullable.
///
/// {@macro flutter.widgets.RestorableNum}
///
/// See also:
///
/// * [RestorableDouble] for the non-nullable version of this class.
class RestorableDoubleN extends RestorableNumN<double> {
/// Creates a [RestorableDoubleN].
/// ///
/// See also: /// {@macro flutter.widgets.RestorableNum.constructor}
RestorableDoubleN(double? defaultValue) : super(defaultValue);
}
/// A [RestorableProperty] that knows how to store and restore an [int]
/// that is nullable.
///
/// {@macro flutter.widgets.RestorableNum}
///
/// See also:
///
/// * [RestorableInt] for the non-nullable version of this class.
class RestorableIntN extends RestorableNumN<int> {
/// Creates a [RestorableIntN].
/// ///
/// * [RestorableBool] for the non-nullable version of this class. /// {@macro flutter.widgets.RestorableNum.constructor}
RestorableBoolN(bool? defaultValue) : super(defaultValue); RestorableIntN(int? defaultValue) : super(defaultValue);
}
/// A [RestorableProperty] that knows how to store and restore a [String]
/// that is nullable.
///
/// {@macro flutter.widgets.RestorableNum}
///
/// See also:
///
/// * [RestorableString] for the non-nullable version of this class.
class RestorableStringN extends _RestorablePrimitiveValueN<String?> {
/// Creates a [RestorableString].
///
/// {@macro flutter.widgets.RestorableNum.constructor}
RestorableStringN(String? defaultValue) : super(defaultValue);
} }
/// A base class for creating a [RestorableProperty] that stores and restores a /// A base class for creating a [RestorableProperty] that stores and restores a
......
...@@ -13,6 +13,10 @@ void main() { ...@@ -13,6 +13,10 @@ void main() {
expect(() => RestorableInt(1).value, throwsAssertionError); expect(() => RestorableInt(1).value, throwsAssertionError);
expect(() => RestorableString('hello').value, throwsAssertionError); expect(() => RestorableString('hello').value, throwsAssertionError);
expect(() => RestorableBool(true).value, throwsAssertionError); expect(() => RestorableBool(true).value, throwsAssertionError);
expect(() => RestorableNumN<num>(0).value, throwsAssertionError);
expect(() => RestorableDoubleN(1.0).value, throwsAssertionError);
expect(() => RestorableIntN(1).value, throwsAssertionError);
expect(() => RestorableStringN('hello').value, throwsAssertionError);
expect(() => RestorableBoolN(true).value, throwsAssertionError); expect(() => RestorableBoolN(true).value, throwsAssertionError);
expect(() => RestorableTextEditingController().value, throwsAssertionError); expect(() => RestorableTextEditingController().value, throwsAssertionError);
expect(() => _TestRestorableValue().value, throwsAssertionError); expect(() => _TestRestorableValue().value, throwsAssertionError);
...@@ -126,6 +130,10 @@ void main() { ...@@ -126,6 +130,10 @@ void main() {
state.intValue.value = 10; state.intValue.value = 10;
state.stringValue.value = 'guten tag'; state.stringValue.value = 'guten tag';
state.boolValue.value = true; state.boolValue.value = true;
state.nullableNumValue.value = 5.0;
state.nullableDoubleValue.value = 2.0;
state.nullableIntValue.value = 1;
state.nullableStringValue.value = 'hullo';
state.nullableBoolValue.value = false; state.nullableBoolValue.value = false;
state.controllerValue.value.text = 'blabla'; state.controllerValue.value.text = 'blabla';
state.objectValue.value = 53; state.objectValue.value = 53;
...@@ -142,6 +150,10 @@ void main() { ...@@ -142,6 +150,10 @@ void main() {
state.intValue.value = 20; state.intValue.value = 20;
state.stringValue.value = 'ciao'; state.stringValue.value = 'ciao';
state.boolValue.value = false; state.boolValue.value = false;
state.nullableNumValue.value = 20.0;
state.nullableDoubleValue.value = 20.0;
state.nullableIntValue.value = 20;
state.nullableStringValue.value = 'ni hao';
state.nullableBoolValue.value = null; state.nullableBoolValue.value = null;
state.controllerValue.value.text = 'blub'; state.controllerValue.value.text = 'blub';
state.objectValue.value = 20; state.objectValue.value = 20;
...@@ -157,6 +169,10 @@ void main() { ...@@ -157,6 +169,10 @@ void main() {
expect(state.intValue.value, 10); expect(state.intValue.value, 10);
expect(state.stringValue.value, 'guten tag'); expect(state.stringValue.value, 'guten tag');
expect(state.boolValue.value, true); expect(state.boolValue.value, true);
expect(state.nullableNumValue.value, 5.0);
expect(state.nullableDoubleValue.value, 2.0);
expect(state.nullableIntValue.value, 1);
expect(state.nullableStringValue.value, 'hullo');
expect(state.nullableBoolValue.value, false); expect(state.nullableBoolValue.value, false);
expect(state.controllerValue.value.text, 'blabla'); expect(state.controllerValue.value.text, 'blabla');
expect(state.objectValue.value, 53); expect(state.objectValue.value, 53);
...@@ -170,6 +186,10 @@ void main() { ...@@ -170,6 +186,10 @@ void main() {
expect(state.intValue.value, 42); expect(state.intValue.value, 42);
expect(state.stringValue.value, 'hello world'); expect(state.stringValue.value, 'hello world');
expect(state.boolValue.value, false); expect(state.boolValue.value, false);
expect(state.nullableNumValue.value, null);
expect(state.nullableDoubleValue.value, null);
expect(state.nullableIntValue.value, null);
expect(state.nullableStringValue.value, null);
expect(state.nullableBoolValue.value, null); expect(state.nullableBoolValue.value, null);
expect(state.controllerValue.value.text, 'FooBar'); expect(state.controllerValue.value.text, 'FooBar');
expect(state.objectValue.value, 55); expect(state.objectValue.value, 55);
...@@ -328,6 +348,10 @@ class _RestorableWidgetState extends State<_RestorableWidget> with RestorationMi ...@@ -328,6 +348,10 @@ class _RestorableWidgetState extends State<_RestorableWidget> with RestorationMi
final RestorableInt intValue = RestorableInt(42); final RestorableInt intValue = RestorableInt(42);
final RestorableString stringValue = RestorableString('hello world'); final RestorableString stringValue = RestorableString('hello world');
final RestorableBool boolValue = RestorableBool(false); final RestorableBool boolValue = RestorableBool(false);
final RestorableNumN<num> nullableNumValue = RestorableNumN<num>(null);
final RestorableDoubleN nullableDoubleValue = RestorableDoubleN(null);
final RestorableIntN nullableIntValue = RestorableIntN(null);
final RestorableStringN nullableStringValue = RestorableStringN(null);
final RestorableBoolN nullableBoolValue = RestorableBoolN(null); final RestorableBoolN nullableBoolValue = RestorableBoolN(null);
final RestorableTextEditingController controllerValue = RestorableTextEditingController(text: 'FooBar'); final RestorableTextEditingController controllerValue = RestorableTextEditingController(text: 'FooBar');
final _TestRestorableValue objectValue = _TestRestorableValue(); final _TestRestorableValue objectValue = _TestRestorableValue();
...@@ -339,6 +363,10 @@ class _RestorableWidgetState extends State<_RestorableWidget> with RestorationMi ...@@ -339,6 +363,10 @@ class _RestorableWidgetState extends State<_RestorableWidget> with RestorationMi
registerForRestoration(intValue, 'int'); registerForRestoration(intValue, 'int');
registerForRestoration(stringValue, 'string'); registerForRestoration(stringValue, 'string');
registerForRestoration(boolValue, 'bool'); registerForRestoration(boolValue, 'bool');
registerForRestoration(nullableNumValue, 'nullableNum');
registerForRestoration(nullableDoubleValue, 'nullableDouble');
registerForRestoration(nullableIntValue, 'nullableInt');
registerForRestoration(nullableStringValue, 'nullableString');
registerForRestoration(nullableBoolValue, 'nullableBool'); registerForRestoration(nullableBoolValue, 'nullableBool');
registerForRestoration(controllerValue, 'controller'); registerForRestoration(controllerValue, 'controller');
registerForRestoration(objectValue, 'object'); registerForRestoration(objectValue, 'object');
...@@ -350,7 +378,7 @@ class _RestorableWidgetState extends State<_RestorableWidget> with RestorationMi ...@@ -350,7 +378,7 @@ class _RestorableWidgetState extends State<_RestorableWidget> with RestorationMi
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Text(stringValue.value, textDirection: TextDirection.ltr,); return Text(stringValue.value, textDirection: TextDirection.ltr);
} }
@override @override
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment