Ami*_*ati 39
这个函数不是生命周期的一部分,因为这个时候widget属性的State为空,如果想在构造函数中访问widget属性是不行的。但是构造函数必须要第一次调用。
创建状态
当 Flutter 被指示构建一个 时StatefulWidget,它会立即调用createState()
初始状态
当此对象插入到树中时调用。
在调用时插入渲染树时,此函数在生命周期中仅调用一次。这里可以做一些初始化,比如初始化State变量。
设置状态
该setState()方法通常由 Flutter 框架本身和开发人员调用。
didChangeDependencies
当此 [State] 对象的依赖项更改时调用。
didUpdateWidget
每当小部件配置更改时调用。
停用
当这个对象从树中移除时调用。在dispose之前,我们会调用这个函数。
处置
当此对象从树中永久移除时调用。
didChangeAppLifecycleState
当系统将应用程序置于后台或将应用程序返回到前台时调用。
这里有一个很好的详细文档:https : //www.bookstack.cn/read/flutterbyexample/aebe8dda4df3319f.md
import 'package:flutter/material.dart';
class ScreenLifecyle extends StatefulWidget {
ScreenLifecyleState state;
//createState(): When the Framework is instructed to build a StatefulWidget, it immediately calls createState()
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return ScreenLifecyleState();
}
}
class ScreenLifecyleState extends State<ScreenLifecyle> {
/*
mounted is true: When createState creates your state class, a buildContext is assigned to that state.
BuildContext is, overly simplified, the place in the widget tree in which this widget is placed.
Here's a longer explanation. All widgets have a bool this.mounted property.
It is turned true when the buildContext is assigned. It is an error to call setState when a widget is unmounted.
mounted is false: The state object can never remount, and an error is thrown is setState is called.
*/
/*
This is the first method called when the widget is created (after the class constructor, of course.)
initState is called once and only once. It must called super.initState().
*/
@override
void initState() {
// TODO: implement initState
super.initState();
print("initState");
}
/*
This method is called immediately after initState on the first time the widget is built.
*/
@override
void didChangeDependencies() {
// TODO: implement didChangeDependencies
super.didChangeDependencies();
print("didChangeDependencies");
}
/*
build(): This method is called often. It is required, and it must return a Widget.
*/
@override
Widget build(BuildContext context) {
print("build");
// TODO: implement build
return Container();
}
/*
If the parent widget changes and has to rebuild this widget (because it needs to give it different data),
but it's being rebuilt with the same runtimeType, then this method is called.
This is because Flutter is re-using the state, which is long lived.
In this case, you may want to initialize some data again, as you would in initState.
*/
@override
void didUpdateWidget(ScreenLifecyle oldWidget) {
print("didUpdateWidget");
// TODO: implement didUpdateWidget
super.didUpdateWidget(oldWidget);
}
@override
void setState(fn) {
print("setState");
// TODO: implement setState
super.setState(fn);
}
/*
Deactivate is called when State is removed from the tree,
but it might be reinserted before the current frame change is finished.
This method exists basically because State objects can be moved from one point in a tree to another.
*/
@override
void deactivate() {
// TODO: implement deactivate
print("deactivate");
super.deactivate();
}
/*
Dispose is called when the State object is removed, which is permanent.
This method is where you should unsubscribe and cancel all animations, streams, etc.
*/
@override
void dispose() {
// TODO: implement dispose
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
switch (state) {
case AppLifecycleState.inactive:
print('appLifeCycleState inactive');
break;
case AppLifecycleState.resumed:
print('appLifeCycleState resumed');
break;
case AppLifecycleState.paused:
print('appLifeCycleState paused');
break;
case AppLifecycleState.suspending:
print('appLifeCycleState suspending');
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
She*_*ard 35
createState(): 当指示Framework构建StatefulWidget时,它立即调用createState()
installed 为true:当createState创建您的状态类时,将为该状态分配buildContext.BuildContext过于简化了小部件树中放置此小部件的位置.这是一个更长的解释.所有小部件都有一个bool this.mounted属性.分配buildContext时,它变为true.卸载窗口小部件时调用setState是错误的.
initState(): 这是创建窗口小部件时调用的第一个方法(当然是在类构造函数之后).111只调用一次initState.它必须调用super.initState().
didChangeDependencies(): 在第一次构建窗口小部件时,在initState之后立即调用此方法.
build(): 经常调用此方法.它是必需的,它必须返回一个Widget.
didUpdateWidget(Widget oldWidget): 如果父窗口小部件发生更改并且必须重建此窗口小部件(因为它需要为其提供不同的数据),但是使用相同的runtimeType重建它,则会调用此方法.这是因为Flutter正在重新使用状态,这是长期存在的.在这种情况下,您可能希望再次初始化一些数据,就像在initState中一样.
setState(): 经常从框架本身和开发人员调用此方法.它用于通知框架数据已更改
deactivate(): 从树中删除State时调用Deactivate,但在当前帧更改完成之前可能会重新插入.此方法的存在主要是因为State对象可以从树中的一个点移动到另一个点.
dispose(): 删除State对象时调用Dispose,这是永久的.您可以在此方法取消订阅并取消所有动画,流等.
mount为false: 状态对象永远不能重新装入,并且抛出错误是调用setState.
aks*_*ane 18
只有StatefulWidget持有 state 。它的生命周期如下
AppLifecycleState 如下
非活动 - 应用程序处于非活动状态且未接收用户输入。仅限 iOS
暂停 - 应用程序当前对用户不可见,不响应用户输入,并在后台运行。
恢复 - 应用程序可见并响应用户输入。
暂停 - 应用程序将暂时暂停。仅限安卓
Ian*_*son 16
这里有一个例子:https://github.com/flutter/flutter/blob/master/examples/layers/services/lifecycle.dart
你需要使用 WidgetsBindingObserver
Far*_*ana 13
应用生命周期
对于 LifeCycle,您需要WidgetsBindingObserver 在应用程序在前台和后台运行时使用 它。
import 'package:flutter/widgets.dart';
class YourWidgetState extends State<YourWidget> with WidgetsBindingObserver {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
//do your stuff
}
}
}
Run Code Online (Sandbox Code Playgroud)
但就我而言,OnResume当我从一个屏幕移动到另一个屏幕时,我无法捕捉到这种情况。所以下面的代码与startActivityForResult.
当您进行其他活动时使用此代码
Navigator.push(context,
MaterialPageRoute(builder: (context) => ProductDetails(pid: productList[index]["pid"],),
settings: RouteSettings(name: '/productdetail')),).then((value){
setState(() {
length=value;
});
debugPrint('CHECK BACK FROM DETAIL $length');
});
Run Code Online (Sandbox Code Playgroud)
当你按回
onPressed: (){Navigator.pop(context,length);}
Run Code Online (Sandbox Code Playgroud)
我认为 Flutter 应用程序生命周期回调不会在这里帮助你。你可以试试这个逻辑。
在第一页(导航到第二页时)
Navigator.push(context, MaterialPageRoute(builder: (context) => Page2())).then((value) {
print("Value returned form Page 2 = $value");
};
Run Code Online (Sandbox Code Playgroud)
在第二页(导航回第一页时)
Navigator.pop(context, returnedValue);
Run Code Online (Sandbox Code Playgroud)
生命周期回调
void main() => runApp(HomePage());
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
print("Current state = $state");
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text("Lifecycle")),
body: Center(child: Text("Center"),),
),
);
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17169 次 |
| 最近记录: |