打开键盘时重新创建StatefulWidget页面 [英] StatefulWidget Page recreate when keyboard is open

查看:167
本文介绍了打开键盘时重新创建StatefulWidget页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的个人资料页面中,使用单击编辑图标时具有编辑选项,我使用布尔条件(如
widget.isUpdate ? new Flexible(child: new TextField()) : Text("Text Widget")它起作用,但是当TextField聚焦时,键盘又在那个时候打开了StatefulWidget,因此Boolean再次变为false,然后Textfield移至Text Widget.仅当页面具有

In my profile page have edit option when use click edit icon i change Text widget to TextField Widget using Boolean condition like
widget.isUpdate ? new Flexible(child: new TextField()) : Text("Text Widget") it working but when TextField focused the keyboard is open on that time StatefulWidget recreate so again Boolean became false then Textfield move to Text Widget. This scenario only happen when page has Navigator push page (Second Page) like

 Navigator.push(context,MaterialPageRoute(builder: (context) => UpdateProfile()))

如果此页面为默认主页,则工作正常.我没有犯什么错误.

if this page as default home page then work fine. I don't what mistake i done.

代码:

import 'package:flutter/material.dart';

class UpdateProfile extends StatefulWidget {
  bool isUpdate = false;

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return UpdateProfileState();
  }
}

class UpdateProfileState extends State<UpdateProfile> {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
          title: Text("Update"),
          elevation: 2.0),
      body: Container(
        width: double.infinity,
        height: double.infinity,
        color: Colors.red,
        margin: const EdgeInsets.all(10.0),
        child: Row(
          children: <Widget>[
            widget.isUpdate ? new Flexible(child: new TextField()) : Text("Text Widget"),
            GestureDetector(
              child: IconTheme(
                  data: IconThemeData(color: Color(0xFFffffff)),
                  child: Icon(Icons.edit)),
              onTap: () {
                setState(() {
                  widget.isUpdate = !widget.isUpdate;
                });
              },
            )
          ],
        ),
      ),
    );
  }
}

问题:

如果我设置为主页,则如下所示可以正常工作

If i set as home page then work fine like below

import 'package:expense_manager_app/page/splash_page.dart';
import 'package:expense_manager_app/page/update_profile.dart';
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        brightness: Brightness.dark,
        primaryColor: Colors.red[500],
        accentColor: Colors.green[900],
      ),
      home: UpdateProfile(),
    );
  }
}

推荐答案

您应将变量isUpdate移至您的州内,记住小部件是不可变的.

You should move your variable isUpdate inside your State, remember the widget is inmutable.

        class UpdateProfileState extends State<UpdateProfile> {

         bool isUpdate = false;

          @override
          Widget build(BuildContext context) {
            // TODO: implement build
            return Scaffold(
              appBar: AppBar(
                  title: Text("Update"),
                  elevation: 2.0),
              body: Container(
                width: double.infinity,
                height: double.infinity,
                color: Colors.red,
                margin: const EdgeInsets.all(10.0),
                child: Row(
                  children: <Widget>[
                    isUpdate ? new Flexible(child: new TextField()) : Text("Text Widget"),
                    GestureDetector(
                      child: IconTheme(
                          data: IconThemeData(color: Color(0xFFffffff)),
                          child: Icon(Icons.edit)),
                      onTap: () {
                        setState(() {
                          isUpdate = !isUpdate;
                        });
                      },
                    )
                  ],
                ),
              ),
            );
          }
        }

还要更改此内容:

    Navigator.push(context,MaterialPageRoute(builder: (context) => UpdateProfile()))

对此:

    final page = UpdateProfile();
    Navigator.push(context,MaterialPageRoute(builder: (context) => page ))

这篇关于打开键盘时重新创建StatefulWidget页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆