From af1c629def7ac38b2428ac1837e8c6a3ec98b547 Mon Sep 17 00:00:00 2001 From: Shi-Hao Hong Date: Wed, 23 Dec 2020 02:19:04 +0800 Subject: [PATCH] [State Restoration] Adds remaining Restorable{Property}N (#72708) --- .../src/widgets/restoration_properties.dart | 96 +++++++++++++++++-- .../widgets/restorable_property_test.dart | 30 +++++- 2 files changed, 117 insertions(+), 9 deletions(-) diff --git a/packages/flutter/lib/src/widgets/restoration_properties.dart b/packages/flutter/lib/src/widgets/restoration_properties.dart index a4ba8dfe7e..ae69f919cc 100644 --- a/packages/flutter/lib/src/widgets/restoration_properties.dart +++ b/packages/flutter/lib/src/widgets/restoration_properties.dart @@ -209,6 +209,10 @@ class _RestorablePrimitiveValue extends _RestorablePrimitiveVa /// Instead of using the more generic [RestorableNum] directly, consider using /// one of the more specific subclasses (e.g. [RestorableDouble] to store a /// [double] and [RestorableInt] to store an [int]). +/// +/// See also: +/// +/// * [RestorableNumN] for the nullable version of this class. class RestorableNum extends _RestorablePrimitiveValue { /// Creates a [RestorableNum]. /// @@ -222,6 +226,10 @@ class RestorableNum extends _RestorablePrimitiveValue { /// A [RestorableProperty] that knows how to store and restore a [double]. /// /// {@macro flutter.widgets.RestorableNum} +/// +/// See also: +/// +/// * [RestorableDoubleN] for the nullable version of this class. class RestorableDouble extends RestorableNum { /// Creates a [RestorableDouble]. /// @@ -232,6 +240,10 @@ class RestorableDouble extends RestorableNum { /// A [RestorableProperty] that knows how to store and restore an [int]. /// /// {@macro flutter.widgets.RestorableNum} +/// +/// See also: +/// +/// * [RestorableIntN] for the nullable version of this class. class RestorableInt extends RestorableNum { /// Creates a [RestorableInt]. /// @@ -242,6 +254,10 @@ class RestorableInt extends RestorableNum { /// A [RestorableProperty] that knows how to store and restore a [String]. /// /// {@macro flutter.widgets.RestorableNum} +/// +/// See also: +/// +/// * [RestorableStringN] for the nullable version of this class. class RestorableString extends _RestorablePrimitiveValue { /// Creates a [RestorableString]. /// @@ -252,14 +268,14 @@ class RestorableString extends _RestorablePrimitiveValue { /// A [RestorableProperty] that knows how to store and restore a [bool]. /// /// {@macro flutter.widgets.RestorableNum} +/// +/// See also: +/// +/// * [RestorableBoolN] for the nullable version of this class. class RestorableBool extends _RestorablePrimitiveValue { /// Creates a [RestorableBool]. /// /// {@macro flutter.widgets.RestorableNum.constructor} - /// - /// See also: - /// - /// * [RestorableBoolN] for the nullable version of this class. RestorableBool(bool defaultValue) : assert(defaultValue != null), super(defaultValue); } @@ -267,17 +283,81 @@ class RestorableBool extends _RestorablePrimitiveValue { /// nullable. /// /// {@macro flutter.widgets.RestorableNum} +/// +/// See also: +/// +/// * [RestorableBool] for the non-nullable version of this class. class RestorableBoolN extends _RestorablePrimitiveValueN { /// Creates a [RestorableBoolN]. /// /// {@macro flutter.widgets.RestorableNum.constructor} - /// - /// See also: - /// - /// * [RestorableBool] for the non-nullable version of this class. 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 extends _RestorablePrimitiveValueN { + /// 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 { + /// Creates a [RestorableDoubleN]. + /// + /// {@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 { + /// Creates a [RestorableIntN]. + /// + /// {@macro flutter.widgets.RestorableNum.constructor} + 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 { + /// 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 /// [Listenable]. /// diff --git a/packages/flutter/test/widgets/restorable_property_test.dart b/packages/flutter/test/widgets/restorable_property_test.dart index a605bb52f3..028134a257 100644 --- a/packages/flutter/test/widgets/restorable_property_test.dart +++ b/packages/flutter/test/widgets/restorable_property_test.dart @@ -13,6 +13,10 @@ void main() { expect(() => RestorableInt(1).value, throwsAssertionError); expect(() => RestorableString('hello').value, throwsAssertionError); expect(() => RestorableBool(true).value, throwsAssertionError); + expect(() => RestorableNumN(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(() => RestorableTextEditingController().value, throwsAssertionError); expect(() => _TestRestorableValue().value, throwsAssertionError); @@ -126,6 +130,10 @@ void main() { state.intValue.value = 10; state.stringValue.value = 'guten tag'; 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.controllerValue.value.text = 'blabla'; state.objectValue.value = 53; @@ -142,6 +150,10 @@ void main() { state.intValue.value = 20; state.stringValue.value = 'ciao'; 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.controllerValue.value.text = 'blub'; state.objectValue.value = 20; @@ -157,6 +169,10 @@ void main() { expect(state.intValue.value, 10); expect(state.stringValue.value, 'guten tag'); 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.controllerValue.value.text, 'blabla'); expect(state.objectValue.value, 53); @@ -170,6 +186,10 @@ void main() { expect(state.intValue.value, 42); expect(state.stringValue.value, 'hello world'); 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.controllerValue.value.text, 'FooBar'); expect(state.objectValue.value, 55); @@ -328,6 +348,10 @@ class _RestorableWidgetState extends State<_RestorableWidget> with RestorationMi final RestorableInt intValue = RestorableInt(42); final RestorableString stringValue = RestorableString('hello world'); final RestorableBool boolValue = RestorableBool(false); + final RestorableNumN nullableNumValue = RestorableNumN(null); + final RestorableDoubleN nullableDoubleValue = RestorableDoubleN(null); + final RestorableIntN nullableIntValue = RestorableIntN(null); + final RestorableStringN nullableStringValue = RestorableStringN(null); final RestorableBoolN nullableBoolValue = RestorableBoolN(null); final RestorableTextEditingController controllerValue = RestorableTextEditingController(text: 'FooBar'); final _TestRestorableValue objectValue = _TestRestorableValue(); @@ -339,6 +363,10 @@ class _RestorableWidgetState extends State<_RestorableWidget> with RestorationMi registerForRestoration(intValue, 'int'); registerForRestoration(stringValue, 'string'); registerForRestoration(boolValue, 'bool'); + registerForRestoration(nullableNumValue, 'nullableNum'); + registerForRestoration(nullableDoubleValue, 'nullableDouble'); + registerForRestoration(nullableIntValue, 'nullableInt'); + registerForRestoration(nullableStringValue, 'nullableString'); registerForRestoration(nullableBoolValue, 'nullableBool'); registerForRestoration(controllerValue, 'controller'); registerForRestoration(objectValue, 'object'); @@ -350,7 +378,7 @@ class _RestorableWidgetState extends State<_RestorableWidget> with RestorationMi @override Widget build(BuildContext context) { - return Text(stringValue.value, textDirection: TextDirection.ltr,); + return Text(stringValue.value, textDirection: TextDirection.ltr); } @override