1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright 2015 The Chromium 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_test/flutter_test.dart';
import 'package:flutter/material.dart';
void main() {
testWidgets('PageStorage read and write', (WidgetTester tester) async {
const Key builderKey = PageStorageKey<String>('builderKey');
StateSetter setState;
int storedValue = 0;
await tester.pumpWidget(
MaterialApp(
home: StatefulBuilder(
key: builderKey,
builder: (BuildContext context, StateSetter setter) {
PageStorage.of(context).writeState(context, storedValue);
setState = setter;
return Center(
child: Text('storedValue: $storedValue')
);
}
)
)
);
final Element builderElement = tester.element(find.byKey(builderKey));
expect(PageStorage.of(builderElement), isNotNull);
expect(PageStorage.of(builderElement).readState(builderElement), equals(storedValue));
setState(() {
storedValue = 1;
});
await tester.pump();
expect(PageStorage.of(builderElement).readState(builderElement), equals(storedValue));
});
testWidgets('PageStorage read and write by identifier', (WidgetTester tester) async {
StateSetter setState;
int storedValue = 0;
Widget buildWidthKey(Key key) {
return MaterialApp(
home: StatefulBuilder(
key: key,
builder: (BuildContext context, StateSetter setter) {
PageStorage.of(context).writeState(context, storedValue, identifier: 123);
setState = setter;
return Center(
child: Text('storedValue: $storedValue')
);
}
)
);
}
Key key = const Key('Key one');
await tester.pumpWidget(buildWidthKey(key));
Element builderElement = tester.element(find.byKey(key));
expect(PageStorage.of(builderElement), isNotNull);
expect(PageStorage.of(builderElement).readState(builderElement), isNull);
expect(PageStorage.of(builderElement).readState(builderElement, identifier: 123), equals(storedValue));
// New StatefulBuilder widget - different key - but the same PageStorage identifier.
key = const Key('Key two');
await tester.pumpWidget(buildWidthKey(key));
builderElement = tester.element(find.byKey(key));
expect(PageStorage.of(builderElement), isNotNull);
expect(PageStorage.of(builderElement).readState(builderElement), isNull);
expect(PageStorage.of(builderElement).readState(builderElement, identifier: 123), equals(storedValue));
setState(() {
storedValue = 1;
});
await tester.pump();
expect(PageStorage.of(builderElement).readState(builderElement, identifier: 123), equals(storedValue));
});
}