restorable_property_test.dart 24 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// 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/widgets.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  testWidgets('value is not accessible when not registered', (WidgetTester tester) async {
    expect(() => RestorableNum<num>(0).value, throwsAssertionError);
    expect(() => RestorableDouble(1.0).value, throwsAssertionError);
    expect(() => RestorableInt(1).value, throwsAssertionError);
    expect(() => RestorableString('hello').value, throwsAssertionError);
    expect(() => RestorableBool(true).value, throwsAssertionError);
15
    expect(() => RestorableNumN<num?>(0).value, throwsAssertionError);
16 17 18
    expect(() => RestorableDoubleN(1.0).value, throwsAssertionError);
    expect(() => RestorableIntN(1).value, throwsAssertionError);
    expect(() => RestorableStringN('hello').value, throwsAssertionError);
19
    expect(() => RestorableBoolN(true).value, throwsAssertionError);
20
    expect(() => RestorableTextEditingController().value, throwsAssertionError);
21
    expect(() => RestorableDateTime(DateTime(2020, 4, 3)).value, throwsAssertionError);
22
    expect(() => RestorableDateTimeN(DateTime(2020, 4, 3)).value, throwsAssertionError);
23 24
    expect(() => RestorableEnumN<TestEnum>(TestEnum.one, values: TestEnum.values).value, throwsAssertionError);
    expect(() => RestorableEnum<TestEnum>(TestEnum.one, values: TestEnum.values).value, throwsAssertionError);
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
    expect(() => _TestRestorableValue().value, throwsAssertionError);
  });

  testWidgets('work when not in restoration scope', (WidgetTester tester) async {
    await tester.pumpWidget(const _RestorableWidget());

    expect(find.text('hello world'), findsOneWidget);
    final _RestorableWidgetState state = tester.state(find.byType(_RestorableWidget));

    // Initialized to default values.
    expect(state.numValue.value, 99);
    expect(state.doubleValue.value, 123.2);
    expect(state.intValue.value, 42);
    expect(state.stringValue.value, 'hello world');
    expect(state.boolValue.value, false);
40
    expect(state.dateTimeValue.value, DateTime(2021, 3, 16));
41
    expect(state.enumValue.value, TestEnum.one);
42 43 44 45 46 47
    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.nullableDateTimeValue.value, null);
48
    expect(state.nullableEnumValue.value, null);
49
    expect(state.controllerValue.value.text, 'FooBar');
50 51 52 53 54 55 56 57 58
    expect(state.objectValue.value, 55);

    // Modify values.
    state.setProperties(() {
      state.numValue.value = 42.2;
      state.doubleValue.value = 441.3;
      state.intValue.value = 10;
      state.stringValue.value = 'guten tag';
      state.boolValue.value = true;
59
      state.dateTimeValue.value = DateTime(2020, 7, 4);
60
      state.enumValue.value = TestEnum.two;
61 62 63 64 65 66
      state.nullableNumValue.value = 5.0;
      state.nullableDoubleValue.value = 2.0;
      state.nullableIntValue.value = 1;
      state.nullableStringValue.value = 'hullo';
      state.nullableBoolValue.value = false;
      state.nullableDateTimeValue.value = DateTime(2020, 4, 4);
67
      state.nullableEnumValue.value = TestEnum.three;
68
      state.controllerValue.value.text = 'blabla';
69 70 71 72 73 74 75 76 77
      state.objectValue.value = 53;
    });
    await tester.pump();

    expect(state.numValue.value, 42.2);
    expect(state.doubleValue.value, 441.3);
    expect(state.intValue.value, 10);
    expect(state.stringValue.value, 'guten tag');
    expect(state.boolValue.value, true);
78
    expect(state.dateTimeValue.value, DateTime(2020, 7, 4));
79
    expect(state.enumValue.value, TestEnum.two);
80 81 82 83 84 85
    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.nullableDateTimeValue.value, DateTime(2020, 4, 4));
86
    expect(state.nullableEnumValue.value, TestEnum.three);
87
    expect(state.controllerValue.value.text, 'blabla');
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
    expect(state.objectValue.value, 53);
    expect(find.text('guten tag'), findsOneWidget);
  });

  testWidgets('restart and restore', (WidgetTester tester) async {
    await tester.pumpWidget(const RootRestorationScope(
      restorationId: 'root-child',
      child: _RestorableWidget(),
    ));

    expect(find.text('hello world'), findsOneWidget);
    _RestorableWidgetState state = tester.state(find.byType(_RestorableWidget));

    // Initialized to default values.
    expect(state.numValue.value, 99);
    expect(state.doubleValue.value, 123.2);
    expect(state.intValue.value, 42);
    expect(state.stringValue.value, 'hello world');
    expect(state.boolValue.value, false);
107
    expect(state.dateTimeValue.value, DateTime(2021, 3, 16));
108
    expect(state.enumValue.value, TestEnum.one);
109 110 111 112 113 114
    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.nullableDateTimeValue.value, null);
115
    expect(state.nullableEnumValue.value, null);
116
    expect(state.controllerValue.value.text, 'FooBar');
117 118 119 120 121 122 123 124 125
    expect(state.objectValue.value, 55);

    // Modify values.
    state.setProperties(() {
      state.numValue.value = 42.2;
      state.doubleValue.value = 441.3;
      state.intValue.value = 10;
      state.stringValue.value = 'guten tag';
      state.boolValue.value = true;
126
      state.dateTimeValue.value = DateTime(2020, 7, 4);
127
      state.enumValue.value = TestEnum.two;
128 129 130 131 132 133
      state.nullableNumValue.value = 5.0;
      state.nullableDoubleValue.value = 2.0;
      state.nullableIntValue.value = 1;
      state.nullableStringValue.value = 'hullo';
      state.nullableBoolValue.value = false;
      state.nullableDateTimeValue.value = DateTime(2020, 4, 4);
134
      state.nullableEnumValue.value = TestEnum.three;
135
      state.controllerValue.value.text = 'blabla';
136 137 138 139 140 141 142 143 144
      state.objectValue.value = 53;
    });
    await tester.pump();

    expect(state.numValue.value, 42.2);
    expect(state.doubleValue.value, 441.3);
    expect(state.intValue.value, 10);
    expect(state.stringValue.value, 'guten tag');
    expect(state.boolValue.value, true);
145
    expect(state.dateTimeValue.value, DateTime(2020, 7, 4));
146
    expect(state.enumValue.value, TestEnum.two);
147 148 149 150 151 152
    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.nullableDateTimeValue.value, DateTime(2020, 4, 4));
153
    expect(state.nullableEnumValue.value, TestEnum.three);
154
    expect(state.controllerValue.value.text, 'blabla');
155 156 157 158 159 160 161 162 163 164 165 166 167 168
    expect(state.objectValue.value, 53);
    expect(find.text('guten tag'), findsOneWidget);

    // Restores to previous values.
    await tester.restartAndRestore();
    final _RestorableWidgetState oldState = state;
    state = tester.state(find.byType(_RestorableWidget));
    expect(state, isNot(same(oldState)));

    expect(state.numValue.value, 42.2);
    expect(state.doubleValue.value, 441.3);
    expect(state.intValue.value, 10);
    expect(state.stringValue.value, 'guten tag');
    expect(state.boolValue.value, true);
169
    expect(state.dateTimeValue.value, DateTime(2020, 7, 4));
170
    expect(state.enumValue.value, TestEnum.two);
171 172 173 174 175 176
    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.nullableDateTimeValue.value, DateTime(2020, 4, 4));
177
    expect(state.nullableEnumValue.value, TestEnum.three);
178
    expect(state.controllerValue.value.text, 'blabla');
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
    expect(state.objectValue.value, 53);
    expect(find.text('guten tag'), findsOneWidget);
  });

  testWidgets('restore to older state', (WidgetTester tester) async {
    await tester.pumpWidget(const RootRestorationScope(
      restorationId: 'root-child',
      child: _RestorableWidget(),
    ));

    expect(find.text('hello world'), findsOneWidget);
    final _RestorableWidgetState state = tester.state(find.byType(_RestorableWidget));

    // Modify values.
    state.setProperties(() {
      state.numValue.value = 42.2;
      state.doubleValue.value = 441.3;
      state.intValue.value = 10;
      state.stringValue.value = 'guten tag';
      state.boolValue.value = true;
199
      state.dateTimeValue.value = DateTime(2020, 7, 4);
200
      state.enumValue.value = TestEnum.two;
201 202 203 204
      state.nullableNumValue.value = 5.0;
      state.nullableDoubleValue.value = 2.0;
      state.nullableIntValue.value = 1;
      state.nullableStringValue.value = 'hullo';
205
      state.nullableBoolValue.value = false;
206
      state.nullableDateTimeValue.value = DateTime(2020, 4, 4);
207
      state.nullableEnumValue.value = TestEnum.three;
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
      state.controllerValue.value.text = 'blabla';
      state.objectValue.value = 53;
    });
    await tester.pump();
    expect(find.text('guten tag'), findsOneWidget);

    final TestRestorationData restorationData = await tester.getRestorationData();

    // Modify values.
    state.setProperties(() {
      state.numValue.value = 20;
      state.doubleValue.value = 20.0;
      state.intValue.value = 20;
      state.stringValue.value = 'ciao';
      state.boolValue.value = false;
223
      state.dateTimeValue.value = DateTime(2020, 3, 2);
224
      state.enumValue.value = TestEnum.four;
225 226 227 228
      state.nullableNumValue.value = 20.0;
      state.nullableDoubleValue.value = 20.0;
      state.nullableIntValue.value = 20;
      state.nullableStringValue.value = 'ni hao';
229
      state.nullableBoolValue.value = null;
230
      state.nullableDateTimeValue.value = DateTime(2020, 5, 5);
231
      state.nullableEnumValue.value = TestEnum.two;
232 233 234 235 236 237 238 239 240 241 242 243 244 245
      state.controllerValue.value.text = 'blub';
      state.objectValue.value = 20;
    });
    await tester.pump();
    expect(find.text('ciao'), findsOneWidget);
    final TextEditingController controller = state.controllerValue.value;

    // Restore to previous.
    await tester.restoreFrom(restorationData);
    expect(state.numValue.value, 42.2);
    expect(state.doubleValue.value, 441.3);
    expect(state.intValue.value, 10);
    expect(state.stringValue.value, 'guten tag');
    expect(state.boolValue.value, true);
246
    expect(state.dateTimeValue.value, DateTime(2020, 7, 4));
247
    expect(state.enumValue.value, TestEnum.two);
248 249 250 251
    expect(state.nullableNumValue.value, 5.0);
    expect(state.nullableDoubleValue.value, 2.0);
    expect(state.nullableIntValue.value, 1);
    expect(state.nullableStringValue.value, 'hullo');
252
    expect(state.nullableBoolValue.value, false);
253
    expect(state.nullableDateTimeValue.value, DateTime(2020, 4, 4));
254
    expect(state.nullableEnumValue.value, TestEnum.three);
255 256 257 258 259 260 261 262 263 264 265 266
    expect(state.controllerValue.value.text, 'blabla');
    expect(state.objectValue.value, 53);
    expect(find.text('guten tag'), findsOneWidget);
    expect(state.controllerValue.value, isNot(same(controller)));

    // Restore to empty data will re-initialize to default values.
    await tester.restoreFrom(TestRestorationData.empty);
    expect(state.numValue.value, 99);
    expect(state.doubleValue.value, 123.2);
    expect(state.intValue.value, 42);
    expect(state.stringValue.value, 'hello world');
    expect(state.boolValue.value, false);
267
    expect(state.dateTimeValue.value, DateTime(2021, 3, 16));
268
    expect(state.enumValue.value, TestEnum.one);
269 270 271 272
    expect(state.nullableNumValue.value, null);
    expect(state.nullableDoubleValue.value, null);
    expect(state.nullableIntValue.value, null);
    expect(state.nullableStringValue.value, null);
273
    expect(state.nullableBoolValue.value, null);
274
    expect(state.nullableDateTimeValue.value, null);
275
    expect(state.nullableEnumValue.value, null);
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
    expect(state.controllerValue.value.text, 'FooBar');
    expect(state.objectValue.value, 55);
    expect(find.text('hello world'), findsOneWidget);
  });

  testWidgets('call notifiers when value changes', (WidgetTester tester) async {
    await tester.pumpWidget(const RootRestorationScope(
      restorationId: 'root-child',
      child: _RestorableWidget(),
    ));

    expect(find.text('hello world'), findsOneWidget);
    final _RestorableWidgetState state = tester.state(find.byType(_RestorableWidget));

    final List<String> notifyLog = <String>[];
291

292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
    state.numValue.addListener(() {
      notifyLog.add('num');
    });
    state.doubleValue.addListener(() {
      notifyLog.add('double');
    });
    state.intValue.addListener(() {
      notifyLog.add('int');
    });
    state.stringValue.addListener(() {
      notifyLog.add('string');
    });
    state.boolValue.addListener(() {
      notifyLog.add('bool');
    });
307 308 309
    state.dateTimeValue.addListener(() {
      notifyLog.add('date-time');
    });
310 311 312
    state.enumValue.addListener(() {
      notifyLog.add('enum');
    });
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
    state.nullableNumValue.addListener(() {
      notifyLog.add('nullable-num');
    });
    state.nullableDoubleValue.addListener(() {
      notifyLog.add('nullable-double');
    });
    state.nullableIntValue.addListener(() {
      notifyLog.add('nullable-int');
    });
    state.nullableStringValue.addListener(() {
      notifyLog.add('nullable-string');
    });
    state.nullableBoolValue.addListener(() {
      notifyLog.add('nullable-bool');
    });
    state.nullableDateTimeValue.addListener(() {
      notifyLog.add('nullable-date-time');
    });
331 332 333
    state.nullableEnumValue.addListener(() {
      notifyLog.add('nullable-enum');
    });
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
    state.controllerValue.addListener(() {
      notifyLog.add('controller');
    });
    state.objectValue.addListener(() {
      notifyLog.add('object');
    });

    state.setProperties(() {
      state.numValue.value = 42.2;
    });
    expect(notifyLog.single, 'num');
    notifyLog.clear();

    state.setProperties(() {
      state.doubleValue.value = 42.2;
    });
    expect(notifyLog.single, 'double');
    notifyLog.clear();

    state.setProperties(() {
      state.intValue.value = 45;
    });
    expect(notifyLog.single, 'int');
    notifyLog.clear();

    state.setProperties(() {
      state.stringValue.value = 'bar';
    });
    expect(notifyLog.single, 'string');
    notifyLog.clear();

    state.setProperties(() {
      state.boolValue.value = true;
    });
    expect(notifyLog.single, 'bool');
    notifyLog.clear();

371 372 373 374 375 376
    state.setProperties(() {
      state.dateTimeValue.value = DateTime(2020, 7, 4);
    });
    expect(notifyLog.single, 'date-time');
    notifyLog.clear();

377 378 379 380 381 382
    state.setProperties(() {
      state.enumValue.value = TestEnum.two;
    });
    expect(notifyLog.single, 'enum');
    notifyLog.clear();

383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
    state.setProperties(() {
      state.nullableNumValue.value = 42.2;
    });
    expect(notifyLog.single, 'nullable-num');
    notifyLog.clear();

    state.setProperties(() {
      state.nullableDoubleValue.value = 42.2;
    });
    expect(notifyLog.single, 'nullable-double');
    notifyLog.clear();

    state.setProperties(() {
      state.nullableIntValue.value = 45;
    });
    expect(notifyLog.single, 'nullable-int');
    notifyLog.clear();

    state.setProperties(() {
      state.nullableStringValue.value = 'bar';
    });
    expect(notifyLog.single, 'nullable-string');
    notifyLog.clear();

    state.setProperties(() {
      state.nullableBoolValue.value = true;
    });
    expect(notifyLog.single, 'nullable-bool');
    notifyLog.clear();

    state.setProperties(() {
      state.nullableDateTimeValue.value = DateTime(2020, 4, 4);
    });
    expect(notifyLog.single, 'nullable-date-time');
    notifyLog.clear();

419 420 421 422 423 424
    state.setProperties(() {
      state.nullableEnumValue.value = TestEnum.three;
    });
    expect(notifyLog.single, 'nullable-enum');
    notifyLog.clear();

425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
    state.setProperties(() {
      state.controllerValue.value.text = 'foo';
    });
    expect(notifyLog.single, 'controller');
    notifyLog.clear();

    state.setProperties(() {
      state.objectValue.value = 42;
    });
    expect(notifyLog.single, 'object');
    notifyLog.clear();

    await tester.pump();
    expect(find.text('bar'), findsOneWidget);

    // Does not notify when set to same value.
    state.setProperties(() {
      state.numValue.value = 42.2;
      state.doubleValue.value = 42.2;
      state.intValue.value = 45;
      state.stringValue.value = 'bar';
      state.boolValue.value = true;
447
      state.dateTimeValue.value = DateTime(2020, 7, 4);
448
      state.enumValue.value = TestEnum.two;
449 450 451 452 453 454
      state.nullableNumValue.value = 42.2;
      state.nullableDoubleValue.value = 42.2;
      state.nullableIntValue.value = 45;
      state.nullableStringValue.value = 'bar';
      state.nullableBoolValue.value = true;
      state.nullableDateTimeValue.value = DateTime(2020, 4, 4);
455
      state.nullableEnumValue.value = TestEnum.three;
456
      state.controllerValue.value.text = 'foo';
457
      state.objectValue.value = 42;
458
    });
459

460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
    expect(notifyLog, isEmpty);
  });

  testWidgets('RestorableValue calls didUpdateValue', (WidgetTester tester) async {
    await tester.pumpWidget(const RootRestorationScope(
      restorationId: 'root-child',
      child: _RestorableWidget(),
    ));

    expect(find.text('hello world'), findsOneWidget);
    final _RestorableWidgetState state = tester.state(find.byType(_RestorableWidget));

    expect(state.objectValue.didUpdateValueCallCount, 0);

    state.setProperties(() {
      state.objectValue.value = 44;
    });
    expect(state.objectValue.didUpdateValueCallCount, 1);

    await tester.pump();

    state.setProperties(() {
      state.objectValue.value = 44;
    });
    expect(state.objectValue.didUpdateValueCallCount, 1);
  });
486

487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
  testWidgets('RestorableEnum and RestorableEnumN assert if default value is not in enum', (WidgetTester tester) async {
    expect(() => RestorableEnum<TestEnum>(
      TestEnum.four,
      values: TestEnum.values.toSet().difference(<TestEnum>{TestEnum.four})), throwsAssertionError);
    expect(() => RestorableEnumN<TestEnum>(
      TestEnum.four,
      values: TestEnum.values.toSet().difference(<TestEnum>{TestEnum.four})), throwsAssertionError);
  });

  testWidgets('RestorableEnum and RestorableEnumN assert if unknown values are set', (WidgetTester tester) async {
    final RestorableEnum<TestEnum> enumMissingValue = RestorableEnum<TestEnum>(
      TestEnum.one,
      values: TestEnum.values.toSet().difference(<TestEnum>{TestEnum.four}),
    );
    expect(() => enumMissingValue.value = TestEnum.four, throwsAssertionError);
    final RestorableEnumN<TestEnum> nullableEnumMissingValue = RestorableEnumN<TestEnum>(
      null,
      values: TestEnum.values.toSet().difference(<TestEnum>{TestEnum.four}),
    );
    expect(() => nullableEnumMissingValue.value = TestEnum.four, throwsAssertionError);
  });

  testWidgets('RestorableEnum and RestorableEnumN assert if unknown values are restored', (WidgetTester tester) async {
    final RestorableEnum<TestEnum> enumMissingValue = RestorableEnum<TestEnum>(
      TestEnum.one,
      values: TestEnum.values.toSet().difference(<TestEnum>{TestEnum.four}),
    );
    expect(() => enumMissingValue.fromPrimitives('four'), throwsAssertionError);
    final RestorableEnumN<TestEnum> nullableEnumMissingValue = RestorableEnumN<TestEnum>(
      null,
      values: TestEnum.values.toSet().difference(<TestEnum>{TestEnum.four}),
    );
    expect(() => nullableEnumMissingValue.fromPrimitives('four'), throwsAssertionError);
  });
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556

  testWidgets('RestorableN types are properly defined', (WidgetTester tester) async {
    await tester.pumpWidget(const RootRestorationScope(
      restorationId: 'root-child',
      child: _RestorableWidget(),
    ));

    expect(find.text('hello world'), findsOneWidget);
    final _RestorableWidgetState state = tester.state(find.byType(_RestorableWidget));
    state.setProperties(() {
      state.nullableIntValue.value = 24;
      state.nullableDoubleValue.value = 1.5;
    });

    // The following types of asserts do not work. They pass even when the
    // type of `value` is a `num` and not an `int` because `num` is a
    // superclass of `int`. This test is intended to prevent a regression
    // where RestorableIntN's value is of type `num?`, but it is passed into
    // a function which requires an `int?` value. This resulted in Dart
    // compile-time errors.
    //
    // expect(state.nullableIntValue.value, isA<int>());
    // expect(state.nullableIntValue.value.runtimeType, int);

    // A function that takes a nullable int value.
    void takesInt(int? value) {}
    // The following would result in a Dart compile-time error if `value` is
    // a `num?` instead of an `int?`.
    takesInt(state.nullableIntValue.value);

    // A function that takes a nullable double value.
    void takesDouble(double? value) {}
    // The following would result in a Dart compile-time error if `value` is
    // a `num?` instead of a `double?`.
    takesDouble(state.nullableDoubleValue.value);
  });
557 558
}

559
class _TestRestorableValue extends RestorableValue<Object?> {
560 561 562 563 564 565 566 567
  @override
  Object createDefaultValue() {
    return 55;
  }

  int didUpdateValueCallCount = 0;

  @override
568
  void didUpdateValue(Object? oldValue) {
569 570 571 572 573
    didUpdateValueCallCount++;
    notifyListeners();
  }

  @override
574
  Object? fromPrimitives(Object? data) {
575 576 577 578
    return data;
  }

  @override
579
  Object? toPrimitives() {
580 581 582 583 584
    return value;
  }
}

class _RestorableWidget extends StatefulWidget {
585
  const _RestorableWidget();
586 587 588 589 590 591 592 593 594 595 596

  @override
  State<_RestorableWidget> createState() => _RestorableWidgetState();
}

class _RestorableWidgetState extends State<_RestorableWidget> with RestorationMixin {
  final RestorableNum<num> numValue = RestorableNum<num>(99);
  final RestorableDouble doubleValue = RestorableDouble(123.2);
  final RestorableInt intValue = RestorableInt(42);
  final RestorableString stringValue = RestorableString('hello world');
  final RestorableBool boolValue = RestorableBool(false);
597
  final RestorableDateTime dateTimeValue = RestorableDateTime(DateTime(2021, 3, 16));
598
  final RestorableEnum<TestEnum> enumValue = RestorableEnum<TestEnum>(TestEnum.one, values: TestEnum.values);
599
  final RestorableNumN<num?> nullableNumValue = RestorableNumN<num?>(null);
600 601 602
  final RestorableDoubleN nullableDoubleValue = RestorableDoubleN(null);
  final RestorableIntN nullableIntValue = RestorableIntN(null);
  final RestorableStringN nullableStringValue = RestorableStringN(null);
603
  final RestorableBoolN nullableBoolValue = RestorableBoolN(null);
604
  final RestorableDateTimeN nullableDateTimeValue = RestorableDateTimeN(null);
605
  final RestorableEnumN<TestEnum> nullableEnumValue = RestorableEnumN<TestEnum>(null, values: TestEnum.values);
606 607 608 609
  final RestorableTextEditingController controllerValue = RestorableTextEditingController(text: 'FooBar');
  final _TestRestorableValue objectValue = _TestRestorableValue();

  @override
610
  void restoreState(RestorationBucket? oldBucket, bool initialRestore) {
611 612 613 614
    registerForRestoration(numValue, 'num');
    registerForRestoration(doubleValue, 'double');
    registerForRestoration(intValue, 'int');
    registerForRestoration(stringValue, 'string');
615
    registerForRestoration(boolValue, 'bool');
616
    registerForRestoration(dateTimeValue, 'dateTime');
617
    registerForRestoration(enumValue, 'enum');
618 619 620 621
    registerForRestoration(nullableNumValue, 'nullableNum');
    registerForRestoration(nullableDoubleValue, 'nullableDouble');
    registerForRestoration(nullableIntValue, 'nullableInt');
    registerForRestoration(nullableStringValue, 'nullableString');
622
    registerForRestoration(nullableBoolValue, 'nullableBool');
623
    registerForRestoration(nullableDateTimeValue, 'nullableDateTime');
624
    registerForRestoration(nullableEnumValue, 'nullableEnum');
625 626 627 628 629 630 631 632 633 634
    registerForRestoration(controllerValue, 'controller');
    registerForRestoration(objectValue, 'object');
  }

  void setProperties(VoidCallback callback) {
    setState(callback);
  }

  @override
  Widget build(BuildContext context) {
635
    return Text(stringValue.value, textDirection: TextDirection.ltr);
636 637 638 639 640
  }

  @override
  String get restorationId => 'widget';
}
641 642 643 644 645 646 647

enum TestEnum {
  one,
  two,
  three,
  four,
}