Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
2d9e1718
Unverified
Commit
2d9e1718
authored
Apr 12, 2022
by
Daniel Agbemava
Committed by
GitHub
Apr 12, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update WidgetsBindingsObserver example (#101512)
parent
2a658299
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
118 additions
and
36 deletions
+118
-36
widget_binding_observer.0.dart
...es/api/lib/widgets/binding/widget_binding_observer.0.dart
+69
-0
widget_binding_observer.0_test.dart
.../test/widgets/binding/widget_binding_observer.0_test.dart
+46
-0
binding.dart
packages/flutter/lib/src/widgets/binding.dart
+3
-36
No files found.
examples/api/lib/widgets/binding/widget_binding_observer.0.dart
0 → 100644
View file @
2d9e1718
// 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.'
));
}
}
examples/api/test/widgets/binding/widget_binding_observer.0_test.dart
0 → 100644
View file @
2d9e1718
// 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
);
});
}
packages/flutter/lib/src/widgets/binding.dart
View file @
2d9e1718
...
...
@@ -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}
///
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment