Flutter 导航器堆栈上的页面在推送新页面时重建
我正在开发一个Flitter应用程序,它会提示一个表单询问一些个人信息Flutter 导航器堆栈上的页面在推送新页面时重建,flutter,dart,navigation,popup,push,Flutter,Dart,Navigation,Popup,Push,我正在开发一个Flitter应用程序,它会提示一个表单询问一些个人信息 问题在于,每次发生某些事情时,页面都会被重建,例如屏幕方向改变或文本字段获得焦点时(键盘立即出现和消失,阻止用户键入任何内容) 很明显,有什么东西触发了不必要的重建,但我无法找出是什么和在哪里 当我将此页面插入主页时,一切正常。 在启动屏幕上显示动画后,当我将页面插入其预定位置时,就会出现问题,因此我猜这与我的问题有关。 主要类别: import 'package:flutter/material.dart'; import
问题在于,每次发生某些事情时,页面都会被重建,例如屏幕方向改变或文本字段获得焦点时(键盘立即出现和消失,阻止用户键入任何内容) 很明显,有什么东西触发了不必要的重建,但我无法找出是什么和在哪里 当我将此页面插入主页时,一切正常。 在启动屏幕上显示动画后,当我将页面插入其预定位置时,就会出现问题,因此我猜这与我的问题有关。 主要类别:
import 'package:flutter/material.dart';
import './view/SplashScreen.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new SplashScreen(),
);
}
}
启动屏幕:
import 'package:flutter/material.dart';
import 'dart:async';
import './UserLoader.dart';
class SplashScreen extends StatefulWidget {
@override
_SplashScreenState createState() => new _SplashScreenState();
}
class _SplashScreenState extends State<SplashScreen>
with SingleTickerProviderStateMixin {
AnimationController _iconAnimationController;
CurvedAnimation _iconAnimation;
@override
void initState() {
super.initState();
_iconAnimationController = new AnimationController(
vsync: this, duration: new Duration(milliseconds: 2000));
_iconAnimation = new CurvedAnimation(
parent: _iconAnimationController, curve: Curves.easeIn);
_iconAnimation.addListener(() => this.setState(() {}));
_iconAnimationController.forward();
startTimeout();
}
@override
Widget build(BuildContext context) {
return new Material(
color: Colors.white,
child: new InkWell(
child: new Center(
child: new Container(
width: 275.0,
height: 275.0,
decoration: new BoxDecoration(
image: new DecorationImage(
colorFilter: new ColorFilter.mode(
Colors.white.withOpacity(_iconAnimation.value),
BlendMode.dstATop),
image: new AssetImage("images/logo.png")),
),
),
),
),
);
}
void handleTimeout() {
Navigator.of(context).pushReplacement(new MaterialPageRoute(
builder: (BuildContext context) => new UserLoader()));
}
startTimeout() async {
var duration = const Duration(seconds: 3);
return new Timer(duration, handleTimeout);
}
}
导入“包装:颤振/材料.省道”;
导入“dart:async”;
导入“/UserLoader.dart”;
类SplashScreen扩展StatefulWidget{
@凌驾
_SplashScreenState createState()=>new_SplashScreenState();
}
类的状态扩展了状态
使用SingleTickerProviderStateMixin{
动画控制器_iconAnimationController;
曲线动画化;
@凌驾
void initState(){
super.initState();
_iconAnimationController=新的AnimationController(
vsync:this,duration:newduration(毫秒:2000));
_iconAnimation=新曲线动画(
父对象:_iconAnimationController,曲线:Curves.easeIn);
_iconAnimation.addListener(()=>this.setState((){}));
_iconAnimationController.forward();
startTimeout();
}
@凌驾
小部件构建(构建上下文){
退回新材料(
颜色:颜色,白色,
孩子:新墨水井(
孩子:新中心(
子容器:新容器(
宽度:275.0,
身高:275.0,
装饰:新盒子装饰(
图片:新装饰图片(
colorFilter:新的colorFilter.mode(
颜色。白色。不透明度(_iconAnimation.value),
BlendMode.dstoop),
图像:新资产图像(“images/logo.png”),
),
),
),
),
);
}
void handleTimeout(){
导航器.of(上下文).pushReplacement(新材料路线)(
生成器:(BuildContext上下文)=>newuserloader());
}
startTimeout()异步{
变量持续时间=常数持续时间(秒:3);
返回新计时器(持续时间、handleTimeout);
}
}
错误页面:
import 'package:flutter/material.dart';
class UserLoader extends StatefulWidget {
@override
_UserLoaderState createState() => new _UserLoaderState();
}
class _UserLoaderState extends State<UserLoader> {
@override
Widget build(BuildContext context) {
final _formKey = new GlobalKey<FormState>();
final _emailController = new TextEditingController();
return new Scaffold(
appBar: new AppBar(
title: new Text("Informations"),
actions: <Widget>[
new IconButton(
icon: const Icon(Icons.save),
onPressed: () {
// unrelated stuff happens here
})
],
),
body: new Center(
child: new SingleChildScrollView(
child: new Form(
key: _formKey,
child: new Column(children: <Widget>[
new ListTile(
leading: const Icon(Icons.email),
title: new TextFormField(
decoration: new InputDecoration(
hintText: "Email",
),
keyboardType: TextInputType.emailAddress,
controller: _emailController,
validator: _validateEmail,
),
),
]))),
));
}}
导入“包装:颤振/材料.省道”;
类UserLoader扩展了StatefulWidget{
@凌驾
_UserLoaderState createState()=>new_UserLoaderState();
}
类_UserLoaderState扩展状态{
@凌驾
小部件构建(构建上下文){
final _formKey=新的GlobalKey();
final _emailController=新文本编辑控制器();
归还新脚手架(
appBar:新的appBar(
标题:新文本(“信息”),
行动:[
新图标按钮(
图标:常量图标(Icons.save),
已按下:(){
//不相关的事情在这里发生
})
],
),
正文:新中心(
子:新的SingleChildScrollView(
儿童:新表格(
键:_formKey,
子项:新列(子项:[
新ListTile(
引导:常量图标(Icons.email),
标题:新TextFormField(
装饰:新的输入装饰(
hintText:“电子邮件”,
),
键盘类型:TextInputType.emailAddress,
控制器:\u电子邮件控制器,
验证程序:_validateEmail,
),
),
]))),
));
}}
有人能帮我找出为什么页面会不断地自我重建吗?我只需像这样更改类就解决了这个问题:
import 'package:flutter/material.dart';
class UserLoader extends StatefulWidget {
@override
_UserLoaderState createState() => new _UserLoaderState();
}
class _UserLoaderState extends State<UserLoader> {
Widget _form; // Save the form
@override
Widget build(BuildContext context) {
if (_form == null) { // Create the form if it does not exist
_form = _createForm(context); // Build the form
}
return _form; // Show the form in the application
}
Widget _createForm(BuildContext context) {
// This is the exact content of the build method in the question
final _formKey = new GlobalKey<FormState>();
final _emailController = new TextEditingController();
return new Scaffold(
appBar: new AppBar(
title: new Text("Informations"),
actions: <Widget>[
new IconButton(
icon: const Icon(Icons.save),
onPressed: () {
// unrelated stuff happens here
})
],
),
body: new Center(
child: new SingleChildScrollView(
child: new Form(
key: _formKey,
child: new Column(children: <Widget>[
new ListTile(
leading: const Icon(Icons.email),
title: new TextFormField(
decoration: new InputDecoration(
hintText: "Email",
),
keyboardType: TextInputType.emailAddress,
controller: _emailController,
validator: _validateEmail,
),
),
]))),
));
}
}
}
导入“包装:颤振/材料.省道”;
类UserLoader扩展了StatefulWidget{
@凌驾
_UserLoaderState createState()=>new_UserLoaderState();
}
类_UserLoaderState扩展状态{
Widget _form;//保存表单
@凌驾
小部件构建(构建上下文){
如果(_form==null){//如果表单不存在,请创建该表单
_表单=_createForm(context);//构建表单
}
return _form;//在应用程序中显示表单
}
小部件_createForm(构建上下文){
//这就是问题中构建方法的确切内容
final _formKey=新的GlobalKey();
final _emailController=新文本编辑控制器();
归还新脚手架(
appBar:新的appBar(
标题:新文本(“信息”),
行动:[
新图标按钮(
图标:常量图标(Icons.save),
已按下:(){
//不相关的事情在这里发生
})
],
),
正文:新中心(
子:新的SingleChildScrollView(
儿童:新表格(
键:_formKey,
子项:新列(子项:[
新ListTile(
引导:常量图标(Icons.email),
标题:新TextFormField(
装饰:新的输入装饰(
hintText:“电子邮件”,
),
键盘类型:TextInputType.emailAddress,
控制器:\u电子邮件控制器,
验证程序:_validateEmail,
),
),
]))),
));
}
}
}
希望有一天这会对其他人有所帮助。您所需要做的就是移动这一行
final _formKey = new GlobalKey<FormState>();
final\u formKey=new GlobalKey();
从build
方法到状态类声明(即outsid