mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
WIP Commits separated as follows: - Update lints in analysis_options files - Run `dart fix --apply` - Clean up leftover analysis issues - Run `dart format .` in the right places. Local analysis and testing passes. Checking CI now. Part of https://github.com/flutter/flutter/issues/178827 - Adoption of flutter_lints in examples/api coming in a separate change (cc @loic-sharma) ## Pre-launch Checklist - [ ] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [ ] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [ ] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [ ] I signed the [CLA]. - [ ] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [ ] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [ ] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
594 lines
23 KiB
Dart
594 lines
23 KiB
Dart
// 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('Sliver with keep alive without key - should dispose after reordering', (
|
|
WidgetTester tester,
|
|
) async {
|
|
var childList = <Widget>[
|
|
const WidgetTest0(text: 'child 0', keepAlive: true),
|
|
const WidgetTest1(text: 'child 1', keepAlive: true),
|
|
const WidgetTest2(text: 'child 2', keepAlive: true),
|
|
];
|
|
await tester.pumpWidget(SwitchingChildListTest(children: childList));
|
|
final _WidgetTest0State state0 = tester.state(find.byType(WidgetTest0));
|
|
expect(find.text('child 0'), findsOneWidget);
|
|
expect(find.text('child 1', skipOffstage: false), findsNothing);
|
|
expect(find.text('child 2', skipOffstage: false), findsNothing);
|
|
|
|
childList = createSwitchedChildList(childList, 0, 2);
|
|
await tester.pumpWidget(SwitchingChildListTest(children: childList));
|
|
final _WidgetTest2State state2 = tester.state(find.byType(WidgetTest2));
|
|
expect(find.text('child 2'), findsOneWidget);
|
|
expect(find.text('child 1', skipOffstage: false), findsNothing);
|
|
expect(find.text('child 0', skipOffstage: false), findsNothing);
|
|
|
|
expect(state0.hasBeenDisposed, true);
|
|
expect(state2.hasBeenDisposed, false);
|
|
});
|
|
|
|
testWidgets('Sliver without keep alive without key - should dispose after reordering', (
|
|
WidgetTester tester,
|
|
) async {
|
|
var childList = <Widget>[
|
|
const WidgetTest0(text: 'child 0'),
|
|
const WidgetTest1(text: 'child 1'),
|
|
const WidgetTest2(text: 'child 2'),
|
|
];
|
|
await tester.pumpWidget(SwitchingChildListTest(children: childList));
|
|
final _WidgetTest0State state0 = tester.state(find.byType(WidgetTest0));
|
|
expect(find.text('child 0'), findsOneWidget);
|
|
expect(find.text('child 1', skipOffstage: false), findsNothing);
|
|
expect(find.text('child 2', skipOffstage: false), findsNothing);
|
|
|
|
childList = createSwitchedChildList(childList, 0, 2);
|
|
await tester.pumpWidget(SwitchingChildListTest(children: childList));
|
|
final _WidgetTest2State state2 = tester.state(find.byType(WidgetTest2));
|
|
expect(find.text('child 2'), findsOneWidget);
|
|
expect(find.text('child 1', skipOffstage: false), findsNothing);
|
|
expect(find.text('child 0', skipOffstage: false), findsNothing);
|
|
|
|
expect(state0.hasBeenDisposed, true);
|
|
expect(state2.hasBeenDisposed, false);
|
|
});
|
|
|
|
testWidgets('Sliver without keep alive with key - should dispose after reordering', (
|
|
WidgetTester tester,
|
|
) async {
|
|
var childList = <Widget>[
|
|
WidgetTest0(text: 'child 0', key: GlobalKey()),
|
|
WidgetTest1(text: 'child 1', key: GlobalKey()),
|
|
WidgetTest2(text: 'child 2', key: GlobalKey()),
|
|
];
|
|
await tester.pumpWidget(SwitchingChildListTest(children: childList));
|
|
final _WidgetTest0State state0 = tester.state(find.byType(WidgetTest0));
|
|
expect(find.text('child 0'), findsOneWidget);
|
|
expect(find.text('child 1', skipOffstage: false), findsNothing);
|
|
expect(find.text('child 2', skipOffstage: false), findsNothing);
|
|
|
|
childList = createSwitchedChildList(childList, 0, 2);
|
|
await tester.pumpWidget(SwitchingChildListTest(children: childList));
|
|
final _WidgetTest2State state2 = tester.state(find.byType(WidgetTest2));
|
|
expect(find.text('child 2'), findsOneWidget);
|
|
expect(find.text('child 1', skipOffstage: false), findsNothing);
|
|
expect(find.text('child 0', skipOffstage: false), findsNothing);
|
|
|
|
expect(state0.hasBeenDisposed, true);
|
|
expect(state2.hasBeenDisposed, false);
|
|
});
|
|
|
|
testWidgets('Sliver with keep alive with key - should not dispose after reordering', (
|
|
WidgetTester tester,
|
|
) async {
|
|
var childList = <Widget>[
|
|
WidgetTest0(text: 'child 0', key: GlobalKey(), keepAlive: true),
|
|
WidgetTest1(text: 'child 1', key: GlobalKey(), keepAlive: true),
|
|
WidgetTest2(text: 'child 2', key: GlobalKey(), keepAlive: true),
|
|
];
|
|
await tester.pumpWidget(SwitchingChildListTest(children: childList));
|
|
final _WidgetTest0State state0 = tester.state(find.byType(WidgetTest0));
|
|
expect(find.text('child 0'), findsOneWidget);
|
|
expect(find.text('child 1', skipOffstage: false), findsNothing);
|
|
expect(find.text('child 2', skipOffstage: false), findsNothing);
|
|
|
|
childList = createSwitchedChildList(childList, 0, 2);
|
|
await tester.pumpWidget(SwitchingChildListTest(children: childList));
|
|
final _WidgetTest2State state2 = tester.state(find.byType(WidgetTest2));
|
|
expect(find.text('child 2'), findsOneWidget);
|
|
expect(find.text('child 1', skipOffstage: false), findsNothing);
|
|
expect(find.text('child 0', skipOffstage: false), findsOneWidget);
|
|
expect(state0.hasBeenDisposed, false);
|
|
expect(state2.hasBeenDisposed, false);
|
|
});
|
|
|
|
testWidgets('Sliver with keep alive with Unique key - should not dispose after reordering', (
|
|
WidgetTester tester,
|
|
) async {
|
|
var childList = <Widget>[
|
|
WidgetTest0(text: 'child 0', key: UniqueKey(), keepAlive: true),
|
|
WidgetTest1(text: 'child 1', key: UniqueKey(), keepAlive: true),
|
|
WidgetTest2(text: 'child 2', key: UniqueKey(), keepAlive: true),
|
|
];
|
|
await tester.pumpWidget(SwitchingChildListTest(children: childList));
|
|
final _WidgetTest0State state0 = tester.state(find.byType(WidgetTest0));
|
|
expect(find.text('child 0'), findsOneWidget);
|
|
expect(find.text('child 1', skipOffstage: false), findsNothing);
|
|
expect(find.text('child 2', skipOffstage: false), findsNothing);
|
|
|
|
childList = createSwitchedChildList(childList, 0, 2);
|
|
await tester.pumpWidget(SwitchingChildListTest(children: childList));
|
|
final _WidgetTest2State state2 = tester.state(find.byType(WidgetTest2));
|
|
expect(find.text('child 2'), findsOneWidget);
|
|
expect(find.text('child 1', skipOffstage: false), findsNothing);
|
|
expect(find.text('child 0', skipOffstage: false), findsOneWidget);
|
|
expect(state0.hasBeenDisposed, false);
|
|
expect(state2.hasBeenDisposed, false);
|
|
});
|
|
|
|
testWidgets('Sliver with keep alive with Value key - should not dispose after reordering', (
|
|
WidgetTester tester,
|
|
) async {
|
|
var childList = <Widget>[
|
|
const WidgetTest0(text: 'child 0', key: ValueKey<int>(0), keepAlive: true),
|
|
const WidgetTest1(text: 'child 1', key: ValueKey<int>(1), keepAlive: true),
|
|
const WidgetTest2(text: 'child 2', key: ValueKey<int>(2), keepAlive: true),
|
|
];
|
|
await tester.pumpWidget(SwitchingChildListTest(children: childList));
|
|
final _WidgetTest0State state0 = tester.state(find.byType(WidgetTest0));
|
|
expect(find.text('child 0'), findsOneWidget);
|
|
expect(find.text('child 1', skipOffstage: false), findsNothing);
|
|
expect(find.text('child 2', skipOffstage: false), findsNothing);
|
|
|
|
childList = createSwitchedChildList(childList, 0, 2);
|
|
await tester.pumpWidget(SwitchingChildListTest(children: childList));
|
|
final _WidgetTest2State state2 = tester.state(find.byType(WidgetTest2));
|
|
expect(find.text('child 2'), findsOneWidget);
|
|
expect(find.text('child 1', skipOffstage: false), findsNothing);
|
|
expect(find.text('child 0', skipOffstage: false), findsOneWidget);
|
|
expect(state0.hasBeenDisposed, false);
|
|
expect(state2.hasBeenDisposed, false);
|
|
});
|
|
|
|
testWidgets('Sliver complex case 1', (WidgetTester tester) async {
|
|
var childList = <Widget>[
|
|
WidgetTest0(text: 'child 0', key: GlobalKey(), keepAlive: true),
|
|
WidgetTest1(text: 'child 1', key: GlobalKey(), keepAlive: true),
|
|
const WidgetTest2(text: 'child 2', keepAlive: true),
|
|
];
|
|
await tester.pumpWidget(SwitchingChildListTest(children: childList));
|
|
final _WidgetTest0State state0 = tester.state(find.byType(WidgetTest0));
|
|
expect(find.text('child 0'), findsOneWidget);
|
|
expect(find.text('child 1', skipOffstage: false), findsNothing);
|
|
expect(find.text('child 2', skipOffstage: false), findsNothing);
|
|
|
|
childList = createSwitchedChildList(childList, 0, 2);
|
|
await tester.pumpWidget(SwitchingChildListTest(children: childList));
|
|
final _WidgetTest2State state2 = tester.state(find.byType(WidgetTest2));
|
|
expect(find.text('child 2'), findsOneWidget);
|
|
expect(find.text('child 1', skipOffstage: false), findsNothing);
|
|
expect(find.text('child 0', skipOffstage: false), findsOneWidget);
|
|
|
|
childList = createSwitchedChildList(childList, 0, 1);
|
|
await tester.pumpWidget(SwitchingChildListTest(children: childList));
|
|
final _WidgetTest1State state1 = tester.state(find.byType(WidgetTest1));
|
|
expect(find.text('child 1'), findsOneWidget);
|
|
expect(find.text('child 2', skipOffstage: false), findsNothing);
|
|
expect(find.text('child 0', skipOffstage: false), findsOneWidget);
|
|
|
|
childList = createSwitchedChildList(childList, 1, 2);
|
|
await tester.pumpWidget(SwitchingChildListTest(children: childList));
|
|
expect(find.text('child 1'), findsOneWidget);
|
|
expect(find.text('child 0', skipOffstage: false), findsOneWidget);
|
|
expect(find.text('child 2', skipOffstage: false), findsNothing);
|
|
|
|
childList = createSwitchedChildList(childList, 0, 1);
|
|
await tester.pumpWidget(SwitchingChildListTest(children: childList));
|
|
expect(find.text('child 0'), findsOneWidget);
|
|
expect(find.text('child 1', skipOffstage: false), findsOneWidget);
|
|
expect(find.text('child 2', skipOffstage: false), findsNothing);
|
|
|
|
expect(state0.hasBeenDisposed, false);
|
|
expect(state1.hasBeenDisposed, false);
|
|
// Child 2 does not have a key.
|
|
expect(state2.hasBeenDisposed, true);
|
|
});
|
|
|
|
testWidgets('Sliver complex case 2', (WidgetTester tester) async {
|
|
var childList = <Widget>[
|
|
WidgetTest0(text: 'child 0', key: GlobalKey(), keepAlive: true),
|
|
WidgetTest1(text: 'child 1', key: UniqueKey()),
|
|
const WidgetTest2(text: 'child 2', keepAlive: true),
|
|
];
|
|
await tester.pumpWidget(SwitchingChildListTest(children: childList));
|
|
final _WidgetTest0State state0 = tester.state(find.byType(WidgetTest0));
|
|
expect(find.text('child 0'), findsOneWidget);
|
|
expect(find.text('child 1', skipOffstage: false), findsNothing);
|
|
expect(find.text('child 2', skipOffstage: false), findsNothing);
|
|
|
|
childList = createSwitchedChildList(childList, 0, 2);
|
|
await tester.pumpWidget(SwitchingChildListTest(children: childList));
|
|
final _WidgetTest2State state2 = tester.state(find.byType(WidgetTest2));
|
|
expect(find.text('child 2'), findsOneWidget);
|
|
expect(find.text('child 1', skipOffstage: false), findsNothing);
|
|
expect(find.text('child 0', skipOffstage: false), findsOneWidget);
|
|
|
|
childList = createSwitchedChildList(childList, 0, 1);
|
|
await tester.pumpWidget(SwitchingChildListTest(children: childList));
|
|
final _WidgetTest1State state1 = tester.state(find.byType(WidgetTest1));
|
|
expect(find.text('child 1'), findsOneWidget);
|
|
expect(find.text('child 2', skipOffstage: false), findsNothing);
|
|
expect(find.text('child 0', skipOffstage: false), findsOneWidget);
|
|
|
|
childList = createSwitchedChildList(childList, 1, 2);
|
|
await tester.pumpWidget(SwitchingChildListTest(children: childList));
|
|
expect(find.text('child 1'), findsOneWidget);
|
|
expect(find.text('child 0', skipOffstage: false), findsOneWidget);
|
|
expect(find.text('child 2', skipOffstage: false), findsNothing);
|
|
|
|
childList = createSwitchedChildList(childList, 0, 1);
|
|
await tester.pumpWidget(SwitchingChildListTest(children: childList));
|
|
expect(find.text('child 0'), findsOneWidget);
|
|
expect(find.text('child 1', skipOffstage: false), findsNothing);
|
|
expect(find.text('child 2', skipOffstage: false), findsNothing);
|
|
|
|
expect(state0.hasBeenDisposed, false);
|
|
expect(state1.hasBeenDisposed, true);
|
|
expect(state2.hasBeenDisposed, true);
|
|
});
|
|
|
|
testWidgets('Sliver with SliverChildBuilderDelegate', (WidgetTester tester) async {
|
|
var childList = <Widget>[
|
|
WidgetTest0(text: 'child 0', key: UniqueKey(), keepAlive: true),
|
|
WidgetTest1(text: 'child 1', key: GlobalKey()),
|
|
const WidgetTest2(text: 'child 2', keepAlive: true),
|
|
];
|
|
await tester.pumpWidget(SwitchingChildBuilderTest(children: childList));
|
|
final _WidgetTest0State state0 = tester.state(find.byType(WidgetTest0));
|
|
expect(find.text('child 0'), findsOneWidget);
|
|
expect(find.text('child 1', skipOffstage: false), findsNothing);
|
|
expect(find.text('child 2', skipOffstage: false), findsNothing);
|
|
|
|
childList = createSwitchedChildList(childList, 0, 2);
|
|
await tester.pumpWidget(SwitchingChildBuilderTest(children: childList));
|
|
final _WidgetTest2State state2 = tester.state(find.byType(WidgetTest2));
|
|
expect(find.text('child 2'), findsOneWidget);
|
|
expect(find.text('child 1', skipOffstage: false), findsNothing);
|
|
expect(find.text('child 0', skipOffstage: false), findsOneWidget);
|
|
|
|
childList = createSwitchedChildList(childList, 0, 1);
|
|
await tester.pumpWidget(SwitchingChildBuilderTest(children: childList));
|
|
final _WidgetTest1State state1 = tester.state(find.byType(WidgetTest1));
|
|
expect(find.text('child 1'), findsOneWidget);
|
|
expect(find.text('child 2', skipOffstage: false), findsNothing);
|
|
expect(find.text('child 0', skipOffstage: false), findsOneWidget);
|
|
|
|
childList = createSwitchedChildList(childList, 1, 2);
|
|
await tester.pumpWidget(SwitchingChildBuilderTest(children: childList));
|
|
expect(find.text('child 1'), findsOneWidget);
|
|
expect(find.text('child 0', skipOffstage: false), findsOneWidget);
|
|
expect(find.text('child 2', skipOffstage: false), findsNothing);
|
|
|
|
childList = createSwitchedChildList(childList, 0, 1);
|
|
await tester.pumpWidget(SwitchingChildBuilderTest(children: childList));
|
|
expect(find.text('child 0'), findsOneWidget);
|
|
expect(find.text('child 1', skipOffstage: false), findsNothing);
|
|
expect(find.text('child 2', skipOffstage: false), findsNothing);
|
|
|
|
expect(state0.hasBeenDisposed, false);
|
|
expect(state1.hasBeenDisposed, true);
|
|
expect(state2.hasBeenDisposed, true);
|
|
});
|
|
|
|
testWidgets('SliverFillViewport should not dispose widget with key during in screen reordering', (
|
|
WidgetTester tester,
|
|
) async {
|
|
var childList = <Widget>[
|
|
WidgetTest0(text: 'child 0', key: UniqueKey(), keepAlive: true),
|
|
WidgetTest1(text: 'child 1', key: UniqueKey()),
|
|
const WidgetTest2(text: 'child 2', keepAlive: true),
|
|
];
|
|
await tester.pumpWidget(SwitchingChildListTest(viewportFraction: 0.1, children: childList));
|
|
final _WidgetTest0State state0 = tester.state(find.byType(WidgetTest0));
|
|
final _WidgetTest1State state1 = tester.state(find.byType(WidgetTest1));
|
|
final _WidgetTest2State state2 = tester.state(find.byType(WidgetTest2));
|
|
expect(find.text('child 0'), findsOneWidget);
|
|
expect(find.text('child 1'), findsOneWidget);
|
|
expect(find.text('child 2'), findsOneWidget);
|
|
|
|
childList = createSwitchedChildList(childList, 0, 2);
|
|
await tester.pumpWidget(SwitchingChildListTest(viewportFraction: 0.1, children: childList));
|
|
|
|
childList = createSwitchedChildList(childList, 0, 1);
|
|
await tester.pumpWidget(SwitchingChildListTest(viewportFraction: 0.1, children: childList));
|
|
|
|
childList = createSwitchedChildList(childList, 1, 2);
|
|
await tester.pumpWidget(SwitchingChildListTest(viewportFraction: 0.1, children: childList));
|
|
|
|
childList = createSwitchedChildList(childList, 0, 1);
|
|
await tester.pumpWidget(SwitchingChildListTest(viewportFraction: 0.1, children: childList));
|
|
|
|
expect(state0.hasBeenDisposed, false);
|
|
expect(state1.hasBeenDisposed, false);
|
|
expect(state2.hasBeenDisposed, true);
|
|
});
|
|
|
|
testWidgets('SliverList should not dispose widget with key during in screen reordering', (
|
|
WidgetTester tester,
|
|
) async {
|
|
var childList = <Widget>[
|
|
WidgetTest0(text: 'child 0', key: UniqueKey(), keepAlive: true),
|
|
const WidgetTest1(text: 'child 1', keepAlive: true),
|
|
WidgetTest2(text: 'child 2', key: UniqueKey()),
|
|
];
|
|
await tester.pumpWidget(SwitchingSliverListTest(children: childList));
|
|
final _WidgetTest0State state0 = tester.state(find.byType(WidgetTest0));
|
|
final _WidgetTest1State state1 = tester.state(find.byType(WidgetTest1));
|
|
final _WidgetTest2State state2 = tester.state(find.byType(WidgetTest2));
|
|
expect(find.text('child 0'), findsOneWidget);
|
|
expect(find.text('child 1'), findsOneWidget);
|
|
expect(find.text('child 2'), findsOneWidget);
|
|
|
|
childList = createSwitchedChildList(childList, 0, 2);
|
|
await tester.pumpWidget(SwitchingSliverListTest(children: childList));
|
|
|
|
childList = createSwitchedChildList(childList, 1, 2);
|
|
await tester.pumpWidget(SwitchingSliverListTest(children: childList));
|
|
|
|
childList = createSwitchedChildList(childList, 1, 2);
|
|
await tester.pumpWidget(SwitchingSliverListTest(children: childList));
|
|
|
|
childList = createSwitchedChildList(childList, 0, 1);
|
|
await tester.pumpWidget(SwitchingSliverListTest(children: childList));
|
|
|
|
childList = createSwitchedChildList(childList, 0, 2);
|
|
await tester.pumpWidget(SwitchingSliverListTest(children: childList));
|
|
|
|
childList = createSwitchedChildList(childList, 0, 1);
|
|
await tester.pumpWidget(SwitchingSliverListTest(children: childList));
|
|
expect(state0.hasBeenDisposed, false);
|
|
expect(state1.hasBeenDisposed, true);
|
|
expect(state2.hasBeenDisposed, false);
|
|
});
|
|
|
|
testWidgets('SliverList remove child from child list', (WidgetTester tester) async {
|
|
var childList = <Widget>[
|
|
WidgetTest0(text: 'child 0', key: UniqueKey(), keepAlive: true),
|
|
const WidgetTest1(text: 'child 1', keepAlive: true),
|
|
WidgetTest2(text: 'child 2', key: UniqueKey()),
|
|
];
|
|
await tester.pumpWidget(SwitchingSliverListTest(children: childList));
|
|
final _WidgetTest0State state0 = tester.state(find.byType(WidgetTest0));
|
|
final _WidgetTest1State state1 = tester.state(find.byType(WidgetTest1));
|
|
final _WidgetTest2State state2 = tester.state(find.byType(WidgetTest2));
|
|
expect(find.text('child 0'), findsOneWidget);
|
|
expect(find.text('child 1'), findsOneWidget);
|
|
expect(find.text('child 2'), findsOneWidget);
|
|
|
|
childList = createSwitchedChildList(childList, 0, 1);
|
|
childList.removeAt(2);
|
|
await tester.pumpWidget(SwitchingSliverListTest(children: childList));
|
|
expect(find.text('child 0'), findsOneWidget);
|
|
expect(find.text('child 1'), findsOneWidget);
|
|
expect(find.text('child 2'), findsNothing);
|
|
expect(state0.hasBeenDisposed, false);
|
|
expect(state1.hasBeenDisposed, true);
|
|
expect(state2.hasBeenDisposed, true);
|
|
});
|
|
}
|
|
|
|
List<Widget> createSwitchedChildList(List<Widget> childList, int i, int j) {
|
|
final Widget w = childList[i];
|
|
childList[i] = childList[j];
|
|
childList[j] = w;
|
|
return List<Widget>.of(childList);
|
|
}
|
|
|
|
class SwitchingChildBuilderTest extends StatefulWidget {
|
|
const SwitchingChildBuilderTest({required this.children, super.key});
|
|
|
|
final List<Widget> children;
|
|
|
|
@override
|
|
State<SwitchingChildBuilderTest> createState() => _SwitchingChildBuilderTest();
|
|
}
|
|
|
|
class _SwitchingChildBuilderTest extends State<SwitchingChildBuilderTest> {
|
|
late List<Widget> children;
|
|
late Map<Key, int> _mapKeyToIndex;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
children = widget.children;
|
|
_mapKeyToIndex = <Key, int>{};
|
|
for (var index = 0; index < children.length; index += 1) {
|
|
final Key? key = children[index].key;
|
|
if (key != null) {
|
|
_mapKeyToIndex[key] = index;
|
|
}
|
|
}
|
|
}
|
|
|
|
@override
|
|
void didUpdateWidget(SwitchingChildBuilderTest oldWidget) {
|
|
super.didUpdateWidget(oldWidget);
|
|
if (oldWidget.children != widget.children) {
|
|
children = widget.children;
|
|
_mapKeyToIndex = <Key, int>{};
|
|
for (var index = 0; index < children.length; index += 1) {
|
|
final Key? key = children[index].key;
|
|
if (key != null) {
|
|
_mapKeyToIndex[key] = index;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Directionality(
|
|
textDirection: TextDirection.ltr,
|
|
child: Center(
|
|
child: SizedBox(
|
|
height: 100,
|
|
child: CustomScrollView(
|
|
cacheExtent: 0,
|
|
slivers: <Widget>[
|
|
SliverFillViewport(
|
|
delegate: SliverChildBuilderDelegate(
|
|
(BuildContext context, int index) {
|
|
return children[index];
|
|
},
|
|
childCount: children.length,
|
|
findChildIndexCallback: (Key key) => _mapKeyToIndex[key] ?? -1,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class SwitchingChildListTest extends StatelessWidget {
|
|
const SwitchingChildListTest({required this.children, this.viewportFraction = 1.0, super.key});
|
|
|
|
final List<Widget> children;
|
|
final double viewportFraction;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Directionality(
|
|
textDirection: TextDirection.ltr,
|
|
child: Center(
|
|
child: SizedBox(
|
|
height: 100,
|
|
child: CustomScrollView(
|
|
cacheExtent: 0,
|
|
slivers: <Widget>[
|
|
SliverFillViewport(
|
|
viewportFraction: viewportFraction,
|
|
delegate: SliverChildListDelegate(children),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class SwitchingSliverListTest extends StatelessWidget {
|
|
const SwitchingSliverListTest({required this.children, super.key});
|
|
|
|
final List<Widget> children;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Directionality(
|
|
textDirection: TextDirection.ltr,
|
|
child: Center(
|
|
child: SizedBox(
|
|
height: 100,
|
|
child: CustomScrollView(
|
|
cacheExtent: 0,
|
|
slivers: <Widget>[SliverList.list(children: children)],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class WidgetTest0 extends StatefulWidget {
|
|
const WidgetTest0({required this.text, this.keepAlive = false, super.key});
|
|
|
|
final String text;
|
|
final bool keepAlive;
|
|
|
|
@override
|
|
State<WidgetTest0> createState() => _WidgetTest0State();
|
|
}
|
|
|
|
class _WidgetTest0State extends State<WidgetTest0> with AutomaticKeepAliveClientMixin {
|
|
bool hasBeenDisposed = false;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
super.build(context);
|
|
return Text(widget.text);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
hasBeenDisposed = true;
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
bool get wantKeepAlive => widget.keepAlive;
|
|
}
|
|
|
|
class WidgetTest1 extends StatefulWidget {
|
|
const WidgetTest1({required this.text, this.keepAlive = false, super.key});
|
|
|
|
final String text;
|
|
final bool keepAlive;
|
|
|
|
@override
|
|
State<WidgetTest1> createState() => _WidgetTest1State();
|
|
}
|
|
|
|
class _WidgetTest1State extends State<WidgetTest1> with AutomaticKeepAliveClientMixin {
|
|
bool hasBeenDisposed = false;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
super.build(context);
|
|
return Text(widget.text);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
hasBeenDisposed = true;
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
bool get wantKeepAlive => widget.keepAlive;
|
|
}
|
|
|
|
class WidgetTest2 extends StatefulWidget {
|
|
const WidgetTest2({required this.text, this.keepAlive = false, super.key});
|
|
|
|
final String text;
|
|
final bool keepAlive;
|
|
|
|
@override
|
|
State<WidgetTest2> createState() => _WidgetTest2State();
|
|
}
|
|
|
|
class _WidgetTest2State extends State<WidgetTest2> with AutomaticKeepAliveClientMixin {
|
|
bool hasBeenDisposed = false;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
super.build(context);
|
|
return Text(widget.text);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
hasBeenDisposed = true;
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
bool get wantKeepAlive => widget.keepAlive;
|
|
}
|