Skip to content

Commit 2d9e171

Browse files
Update WidgetsBindingsObserver example (#101512)
1 parent 2a65829 commit 2d9e171

File tree

3 files changed

+118
-36
lines changed

3 files changed

+118
-36
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
// Flutter code sample for WidgetBindingsObserver
6+
7+
import 'package:flutter/material.dart';
8+
9+
void main() => runApp(const MyApp());
10+
11+
class MyApp extends StatelessWidget {
12+
const MyApp({Key? key}) : super(key: key);
13+
14+
@override
15+
Widget build(BuildContext context) {
16+
return MaterialApp(
17+
home: Scaffold(
18+
appBar: AppBar(title: const Text('WidgetBindingsObserver Sample')),
19+
body: const WidgetBindingsObserverSample(),
20+
),
21+
);
22+
}
23+
}
24+
25+
class WidgetBindingsObserverSample extends StatefulWidget {
26+
const WidgetBindingsObserverSample({Key? key}) : super(key: key);
27+
28+
@override
29+
State<WidgetBindingsObserverSample> createState() => _WidgetBindingsObserverSampleState();
30+
}
31+
32+
class _WidgetBindingsObserverSampleState extends State<WidgetBindingsObserverSample> with WidgetsBindingObserver {
33+
final List<AppLifecycleState> _stateHistoryList = <AppLifecycleState>[];
34+
35+
@override
36+
void initState() {
37+
super.initState();
38+
WidgetsBinding.instance.addObserver(this);
39+
if(WidgetsBinding.instance.lifecycleState != null) {
40+
_stateHistoryList.add(WidgetsBinding.instance.lifecycleState!);
41+
}
42+
}
43+
44+
@override
45+
void didChangeAppLifecycleState(AppLifecycleState state) {
46+
setState(() { _stateHistoryList.add(state); });
47+
}
48+
49+
@override
50+
void dispose() {
51+
WidgetsBinding.instance.removeObserver(this);
52+
super.dispose();
53+
}
54+
55+
@override
56+
Widget build(BuildContext context) {
57+
if (_stateHistoryList.isNotEmpty) {
58+
return ListView.builder(
59+
key: const ValueKey<String>('stateHistoryList'),
60+
itemCount: _stateHistoryList.length,
61+
itemBuilder: (BuildContext context, int index) {
62+
return Text('state is: ${_stateHistoryList[index]}');
63+
},
64+
);
65+
}
66+
67+
return const Center(child: Text('There are no AppLifecycleStates to show.'));
68+
}
69+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/services.dart';
6+
import 'package:flutter/widgets.dart';
7+
import 'package:flutter_api_samples/widgets/binding/widget_binding_observer.0.dart'
8+
as example;
9+
import 'package:flutter_test/flutter_test.dart';
10+
11+
void main() {
12+
testWidgets('App tracks lifecycle states', (WidgetTester tester) async {
13+
Future<void> setAppLifeCycleState(AppLifecycleState state) async {
14+
final ByteData? message =
15+
const StringCodec().encodeMessage(state.toString());
16+
await ServicesBinding.instance.defaultBinaryMessenger
17+
.handlePlatformMessage('flutter/lifecycle', message, (_) {});
18+
}
19+
20+
await tester.pumpWidget(
21+
const example.MyApp(),
22+
);
23+
24+
expect(find.text('There are no AppLifecycleStates to show.'), findsOneWidget);
25+
26+
await setAppLifeCycleState(AppLifecycleState.resumed);
27+
await tester.pumpAndSettle();
28+
expect(find.text('state is: AppLifecycleState.resumed'), findsOneWidget);
29+
30+
await setAppLifeCycleState(AppLifecycleState.inactive);
31+
await tester.pumpAndSettle();
32+
expect(find.text('state is: AppLifecycleState.inactive'), findsOneWidget);
33+
34+
await setAppLifeCycleState(AppLifecycleState.paused);
35+
await tester.pumpAndSettle();
36+
await setAppLifeCycleState(AppLifecycleState.resumed);
37+
await tester.pumpAndSettle();
38+
expect(find.text('state is: AppLifecycleState.paused'), findsOneWidget);
39+
40+
await setAppLifeCycleState(AppLifecycleState.detached);
41+
await tester.pumpAndSettle();
42+
await setAppLifeCycleState(AppLifecycleState.resumed);
43+
await tester.pumpAndSettle();
44+
expect(find.text('state is: AppLifecycleState.detached'), findsOneWidget);
45+
});
46+
}

packages/flutter/lib/src/widgets/binding.dart

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -33,45 +33,12 @@ export 'dart:ui' show AppLifecycleState, Locale;
3333
/// handlers must be implemented (and the analyzer will list those that have
3434
/// been omitted).
3535
///
36-
/// {@tool snippet}
37-
///
38-
/// This [StatefulWidget] implements the parts of the [State] and
36+
/// {@tool dartpad}
37+
/// This sample shows how to implement parts of the [State] and
3938
/// [WidgetsBindingObserver] protocols necessary to react to application
4039
/// lifecycle messages. See [didChangeAppLifecycleState].
4140
///
42-
/// ```dart
43-
/// class AppLifecycleReactor extends StatefulWidget {
44-
/// const AppLifecycleReactor({ Key? key }) : super(key: key);
45-
///
46-
/// @override
47-
/// State<AppLifecycleReactor> createState() => _AppLifecycleReactorState();
48-
/// }
49-
///
50-
/// class _AppLifecycleReactorState extends State<AppLifecycleReactor> with WidgetsBindingObserver {
51-
/// @override
52-
/// void initState() {
53-
/// super.initState();
54-
/// WidgetsBinding.instance.addObserver(this);
55-
/// }
56-
///
57-
/// @override
58-
/// void dispose() {
59-
/// WidgetsBinding.instance.removeObserver(this);
60-
/// super.dispose();
61-
/// }
62-
///
63-
/// late AppLifecycleState _notification;
64-
///
65-
/// @override
66-
/// void didChangeAppLifecycleState(AppLifecycleState state) {
67-
/// setState(() { _notification = state; });
68-
/// }
69-
///
70-
/// @override
71-
/// Widget build(BuildContext context) {
72-
/// return Text('Last notification: $_notification');
73-
/// }
74-
/// }
41+
/// ** See code in examples/api/lib/widgets/binding/widget_binding_observer.0.dart **
7542
/// ```
7643
/// {@end-tool}
7744
///

0 commit comments

Comments
 (0)