Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add documentation examples to KeepAlive AutomaticKeepAlive and AutomaticKeepAliveClientMixin #168137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions examples/api/lib/widgets/keep_alive/automatic_keep_alive.0.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// 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/material.dart';

/// Flutter code sample for [AutomaticKeepAlive].
///
/// This example demonstrates how to use the [AutomaticKeepAlive] to preserve the state
/// of individual list items in a `ListView` when they are scrolled out of view.
/// Each item has a counter that maintains its state.
void main() {
runApp(const AutomaticKeepAliveExampleApp());
}

class AutomaticKeepAliveExampleApp extends StatelessWidget {
const AutomaticKeepAliveExampleApp({super.key});

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('AutomaticKeepAlive Example')),
body: ListView.builder(
addAutomaticKeepAlives: false,
addRepaintBoundaries: false,
addSemanticIndexes: false,
itemCount: 100,
itemBuilder: (BuildContext context, int index) {
return AutomaticKeepAlive(child: _KeepAliveItem(index: index));
},
),
),
);
}
}

class _KeepAliveItem extends StatefulWidget {
const _KeepAliveItem({required this.index});

final int index;

@override
State<_KeepAliveItem> createState() => _KeepAliveItemState();
}

class _KeepAliveItemState extends State<_KeepAliveItem>
with AutomaticKeepAliveClientMixin<_KeepAliveItem> {
int _counter = 0;

@override
bool get wantKeepAlive => widget.index.isEven;

@override
Widget build(BuildContext context) {
super.build(context);
return ListTile(
title: Text('Item ${widget.index}: $_counter'),
trailing: IconButton(
icon: const Icon(Icons.add),
onPressed: () {
setState(() {
_counter++;
});
},
),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// 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/material.dart';

/// Flutter code sample for [AutomaticKeepAliveClientMixin].
///
/// This example demonstrates how to use the [AutomaticKeepAliveClientMixin] to
/// preserve the state of individual list items in a `ListView` when they are
/// scrolled out of view. Each item has a counter that maintains its state.
void main() {
runApp(const AutomaticKeepAliveClientMixinExampleApp());
}

class AutomaticKeepAliveClientMixinExampleApp extends StatefulWidget {
const AutomaticKeepAliveClientMixinExampleApp({super.key});

@override
State<AutomaticKeepAliveClientMixinExampleApp> createState() =>
_AutomaticKeepAliveClientMixinExampleAppState();
}

class _AutomaticKeepAliveClientMixinExampleAppState
extends State<AutomaticKeepAliveClientMixinExampleApp> {
bool _keepAlive = true;

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('AutomaticKeepAliveClientMixin Example'),
actions: <Widget>[
Row(
children: <Widget>[
const Text('Keep Alive'),
Switch(
value: _keepAlive,
onChanged: (bool value) {
setState(() {
_keepAlive = value;
});
},
),
],
),
],
),
body: ListView.builder(
itemCount: 100,
itemBuilder: (BuildContext context, int index) {
return _KeepAliveItem(index: index, keepAlive: _keepAlive);
},
),
),
);
}
}

class _KeepAliveItem extends StatefulWidget {
const _KeepAliveItem({required this.index, required this.keepAlive});

final int index;
final bool keepAlive;

@override
State<_KeepAliveItem> createState() => _KeepAliveItemState();
}

class _KeepAliveItemState extends State<_KeepAliveItem>
with AutomaticKeepAliveClientMixin<_KeepAliveItem> {
int _counter = 0;

@override
void didUpdateWidget(_KeepAliveItem oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.keepAlive != widget.keepAlive) {
updateKeepAlive();
}
}

@override
bool get wantKeepAlive => widget.keepAlive;

@override
Widget build(BuildContext context) {
super.build(context);

return ListTile(
title: Text('Item ${widget.index}: $_counter'),
trailing: IconButton(
icon: const Icon(Icons.add),
onPressed: () {
setState(() {
_counter++;
});
},
),
);
}
}
64 changes: 64 additions & 0 deletions examples/api/lib/widgets/keep_alive/keep_alive.0.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// 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/material.dart';

/// Flutter code sample for [KeepAlive].
///
/// This example demonstrates how to use the [KeepAlive] to preserve the state
/// of individual list items in a `ListView` when they are scrolled out of view.
/// Each item has a counter that maintains its state.
void main() {
runApp(const KeepAliveExampleApp());
}

class KeepAliveExampleApp extends StatelessWidget {
const KeepAliveExampleApp({super.key});

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('KeepAlive Example')),
body: ListView.builder(
addAutomaticKeepAlives: false,
addRepaintBoundaries: false,
addSemanticIndexes: false,
itemCount: 100,
itemBuilder: (BuildContext context, int index) {
return KeepAlive(keepAlive: index.isEven, child: _KeepAliveItem(index: index));
},
),
),
);
}
}

class _KeepAliveItem extends StatefulWidget {
const _KeepAliveItem({required this.index});

final int index;

@override
State<_KeepAliveItem> createState() => _KeepAliveItemState();
}

class _KeepAliveItemState extends State<_KeepAliveItem> {
int _counter = 0;

@override
Widget build(BuildContext context) {
return ListTile(
title: Text('Item ${widget.index}: $_counter'),
trailing: IconButton(
icon: const Icon(Icons.add),
onPressed: () {
setState(() {
_counter++;
});
},
),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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/material.dart';
import 'package:flutter_api_samples/widgets/keep_alive/automatic_keep_alive.0.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
testWidgets('The state is maintained for the even items', (WidgetTester tester) async {
await tester.pumpWidget(const AutomaticKeepAliveExampleApp());

expect(find.text('Item 0: 0'), findsOne);
expect(find.text('Item 1: 0'), findsOne);

await tester.tap(find.widgetWithIcon(IconButton, Icons.add).first);
await tester.tap(find.widgetWithIcon(IconButton, Icons.add).at(1));
await tester.pump();

expect(find.text('Item 0: 1'), findsOne);
expect(find.text('Item 1: 1'), findsOne);

// Scrolls all the way down to the bottom of the list.
await tester.fling(find.byType(ListView), const Offset(0, -6000), 1000);
await tester.pumpAndSettle();

expect(find.text('Item 99: 0'), findsOne);

// Scrolls all the way back to the top of the list.
await tester.fling(find.byType(ListView), const Offset(0, 6000), 1000);
await tester.pumpAndSettle();

expect(find.text('Item 0: 1'), findsOne, reason: 'The state of item 0 should be maintained');
expect(
find.text('Item 1: 0'),
findsOne,
reason: 'The state of item 1 should not be maintained',
);
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// 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/material.dart';
import 'package:flutter_api_samples/widgets/keep_alive/automatic_keep_alive_client_mixin.0.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
testWidgets('The state is maintained when the item is scrolled out of view', (
WidgetTester tester,
) async {
await tester.pumpWidget(const AutomaticKeepAliveClientMixinExampleApp());

expect(find.text('Item 0: 0'), findsOne);

await tester.tap(find.widgetWithIcon(IconButton, Icons.add).first);
await tester.pump();

expect(find.text('Item 0: 1'), findsOne);

// Scrolls all the way down to the bottom of the list.
await tester.fling(find.byType(ListView), const Offset(0, -6000), 1000);
await tester.pumpAndSettle();

expect(find.text('Item 99: 0'), findsOne);

// Scrolls all the way back to the top of the list.
await tester.fling(find.byType(ListView), const Offset(0, 6000), 1000);
await tester.pumpAndSettle();

expect(find.text('Item 0: 1'), findsOne, reason: 'The state of item 0 should be maintained');
});

testWidgets('The state is not maintained when the item is scrolled out of view', (
WidgetTester tester,
) async {
await tester.pumpWidget(const AutomaticKeepAliveClientMixinExampleApp());

await tester.tap(find.byType(Switch));
await tester.pumpAndSettle();

expect(find.text('Item 0: 0'), findsOne);

await tester.tap(find.widgetWithIcon(IconButton, Icons.add).first);
await tester.pump();

expect(find.text('Item 0: 1'), findsOne);

// Scrolls all the way down to the bottom of the list.
await tester.fling(find.byType(ListView), const Offset(0, -6000), 1000);
await tester.pumpAndSettle();

expect(find.text('Item 99: 0'), findsOne);

// Scrolls all the way back to the top of the list.
await tester.fling(find.byType(ListView), const Offset(0, 6000), 1000);
await tester.pumpAndSettle();

expect(
find.text('Item 0: 0'),
findsOne,
reason: 'The state of item 0 should not be maintained',
);
});
}
40 changes: 40 additions & 0 deletions examples/api/test/widgets/keep_alive/keep_alive.0_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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/material.dart';
import 'package:flutter_api_samples/widgets/keep_alive/keep_alive.0.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
testWidgets('The state is maintained for the even items', (WidgetTester tester) async {
await tester.pumpWidget(const KeepAliveExampleApp());

expect(find.text('Item 0: 0'), findsOne);
expect(find.text('Item 1: 0'), findsOne);

await tester.tap(find.widgetWithIcon(IconButton, Icons.add).first);
await tester.tap(find.widgetWithIcon(IconButton, Icons.add).at(1));
await tester.pump();

expect(find.text('Item 0: 1'), findsOne);
expect(find.text('Item 1: 1'), findsOne);

// Scrolls all the way down to the bottom of the list.
await tester.fling(find.byType(ListView), const Offset(0, -6000), 1000);
await tester.pumpAndSettle();

expect(find.text('Item 99: 0'), findsOne);

// Scrolls all the way back to the top of the list.
await tester.fling(find.byType(ListView), const Offset(0, 6000), 1000);
await tester.pumpAndSettle();

expect(find.text('Item 0: 1'), findsOne, reason: 'The state of item 0 should be maintained');
expect(
find.text('Item 1: 0'),
findsOne,
reason: 'The state of item 1 should not be maintained',
);
});
}
Loading