Unverified Commit 2d9e1718 authored by Daniel Agbemava's avatar Daniel Agbemava Committed by GitHub

Update WidgetsBindingsObserver example (#101512)

parent 2a658299
// 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.
// Flutter code sample for WidgetBindingsObserver
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('WidgetBindingsObserver Sample')),
body: const WidgetBindingsObserverSample(),
),
);
}
}
class WidgetBindingsObserverSample extends StatefulWidget {
const WidgetBindingsObserverSample({Key? key}) : super(key: key);
@override
State<WidgetBindingsObserverSample> createState() => _WidgetBindingsObserverSampleState();
}
class _WidgetBindingsObserverSampleState extends State<WidgetBindingsObserverSample> with WidgetsBindingObserver {
final List<AppLifecycleState> _stateHistoryList = <AppLifecycleState>[];
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
if(WidgetsBinding.instance.lifecycleState != null) {
_stateHistoryList.add(WidgetsBinding.instance.lifecycleState!);
}
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
setState(() { _stateHistoryList.add(state); });
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
Widget build(BuildContext context) {
if (_stateHistoryList.isNotEmpty) {
return ListView.builder(
key: const ValueKey<String>('stateHistoryList'),
itemCount: _stateHistoryList.length,
itemBuilder: (BuildContext context, int index) {
return Text('state is: ${_stateHistoryList[index]}');
},
);
}
return const Center(child: Text('There are no AppLifecycleStates to show.'));
}
}
// 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/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_api_samples/widgets/binding/widget_binding_observer.0.dart'
as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('App tracks lifecycle states', (WidgetTester tester) async {
Future<void> setAppLifeCycleState(AppLifecycleState state) async {
final ByteData? message =
const StringCodec().encodeMessage(state.toString());
await ServicesBinding.instance.defaultBinaryMessenger
.handlePlatformMessage('flutter/lifecycle', message, (_) {});
}
await tester.pumpWidget(
const example.MyApp(),
);
expect(find.text('There are no AppLifecycleStates to show.'), findsOneWidget);
await setAppLifeCycleState(AppLifecycleState.resumed);
await tester.pumpAndSettle();
expect(find.text('state is: AppLifecycleState.resumed'), findsOneWidget);
await setAppLifeCycleState(AppLifecycleState.inactive);
await tester.pumpAndSettle();
expect(find.text('state is: AppLifecycleState.inactive'), findsOneWidget);
await setAppLifeCycleState(AppLifecycleState.paused);
await tester.pumpAndSettle();
await setAppLifeCycleState(AppLifecycleState.resumed);
await tester.pumpAndSettle();
expect(find.text('state is: AppLifecycleState.paused'), findsOneWidget);
await setAppLifeCycleState(AppLifecycleState.detached);
await tester.pumpAndSettle();
await setAppLifeCycleState(AppLifecycleState.resumed);
await tester.pumpAndSettle();
expect(find.text('state is: AppLifecycleState.detached'), findsOneWidget);
});
}
......@@ -33,45 +33,12 @@ export 'dart:ui' show AppLifecycleState, Locale;
/// handlers must be implemented (and the analyzer will list those that have
/// been omitted).
///
/// {@tool snippet}
///
/// This [StatefulWidget] implements the parts of the [State] and
/// {@tool dartpad}
/// This sample shows how to implement parts of the [State] and
/// [WidgetsBindingObserver] protocols necessary to react to application
/// lifecycle messages. See [didChangeAppLifecycleState].
///
/// ```dart
/// class AppLifecycleReactor extends StatefulWidget {
/// const AppLifecycleReactor({ Key? key }) : super(key: key);
///
/// @override
/// State<AppLifecycleReactor> createState() => _AppLifecycleReactorState();
/// }
///
/// class _AppLifecycleReactorState extends State<AppLifecycleReactor> with WidgetsBindingObserver {
/// @override
/// void initState() {
/// super.initState();
/// WidgetsBinding.instance.addObserver(this);
/// }
///
/// @override
/// void dispose() {
/// WidgetsBinding.instance.removeObserver(this);
/// super.dispose();
/// }
///
/// late AppLifecycleState _notification;
///
/// @override
/// void didChangeAppLifecycleState(AppLifecycleState state) {
/// setState(() { _notification = state; });
/// }
///
/// @override
/// Widget build(BuildContext context) {
/// return Text('Last notification: $_notification');
/// }
/// }
/// ** See code in examples/api/lib/widgets/binding/widget_binding_observer.0.dart **
/// ```
/// {@end-tool}
///
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment