diff --git a/examples/api/lib/widgets/binding/widget_binding_observer.0.dart b/examples/api/lib/widgets/binding/widget_binding_observer.0.dart new file mode 100644 index 0000000000..304eaa61f9 --- /dev/null +++ b/examples/api/lib/widgets/binding/widget_binding_observer.0.dart @@ -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. + +// 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 createState() => _WidgetBindingsObserverSampleState(); +} + +class _WidgetBindingsObserverSampleState extends State with WidgetsBindingObserver { + final List _stateHistoryList = []; + + @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('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.')); + } +} diff --git a/examples/api/test/widgets/binding/widget_binding_observer.0_test.dart b/examples/api/test/widgets/binding/widget_binding_observer.0_test.dart new file mode 100644 index 0000000000..759463f348 --- /dev/null +++ b/examples/api/test/widgets/binding/widget_binding_observer.0_test.dart @@ -0,0 +1,46 @@ +// 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 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); + }); +} diff --git a/packages/flutter/lib/src/widgets/binding.dart b/packages/flutter/lib/src/widgets/binding.dart index 8f0eedcc38..31e2fa4589 100644 --- a/packages/flutter/lib/src/widgets/binding.dart +++ b/packages/flutter/lib/src/widgets/binding.dart @@ -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 createState() => _AppLifecycleReactorState(); -/// } -/// -/// class _AppLifecycleReactorState extends State 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} ///