Flutter   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – Flutter应用程序页面不断重建大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在开发一个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应用程序页面不断重建所遇到的程序开发问题。

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。