android – Flutter应用程序页面不断重建

android – Flutter应用程序页面不断重建,第1张

概述我正在开发一个Flutter应用程序,它会提示表单询问一些个人信息. 问题是每次发生某些事情时都会重建页面,例如屏幕方向改变或文本字段获得焦点时(键盘会立即显示并消失,从而阻止用户输入任何内容). 显然有些事情正在触发不必要的重建,但我无法找到什么和哪里. 当我将此页面作为主页插入时,一切正常. 当我在启动画面上显示动画后将页面插入其预期位置时会出现问题,所以我想这与我的问题有关. 主要课程: i 我正在开发一个Flutter应用程序,它会提示表单询问一些个人信息.

问题是每次发生某些事情时都会重建页面,例如屏幕方向改变或文本字段获得焦点时(键盘会立即显示并消失,从而阻止用户输入任何内容).

显然有些事情正在触发不必要的重建,但我无法找到什么和哪里.

当我将此页面作为主页插入时,一切正常.
当我在启动画面上显示动画后将页面插入其预期位置时会出现问题,所以我想这与我的问题有关.

主要课程:

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);  }}

页面错误:

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,]))),));    }}

任何人都可以帮我找出为什么页面不断重建自己?

解决方法 我通过简单地更改类来解决问题:

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"),));    }  }}

希望有一天这可能会帮助别人.

总结

以上是内存溢出为你收集整理的android – Flutter应用程序页面不断重建全部内容,希望文章能够帮你解决android – Flutter应用程序页面不断重建所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1001822.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存