Update WidgetsBindingsObserver example (#101512)
This commit is contained in:
@@ -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<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.'));
|
||||
}
|
||||
}
|
||||
@@ -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<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);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user