10

在我的个人资料页面中有编辑选项,当使用单击编辑图标时,我使用布尔条件将文本小部件更改为 TextField 小部件,就像
widget.isUpdate ? new Flexible(child: new TextField()) : Text("Text Widget")它工作一样,但是当 TextField 聚焦时,键盘打开时 StatefulWidget 重新创建,所以布尔值再次变为 false,然后 Textfield 移动到文本小部件。这种情况仅在页面具有导航器推送页面(第二页)时发生

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

如果此页面作为默认主页,则可以正常工作。我不知道我犯了什么错误。

代码 :

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

问题 :

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

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

3 回答 3

7

您应该将变量移动isUpdate到您的状态中,记住小部件是不可变的。

        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 ))
于 2018-12-25T07:18:07.507 回答
0

另外,如果您对 didChangeDependencies 方法执行某些操作,那也会导致

于 2019-09-28T12:36:28.793 回答
0

这是因为您将默认设置bool isUpdate = false;更改为 bool isUpdate;

并使用initState您的State类的方法并将其设置isUpdate为 false。我会被叫一次。

@override
void initState() { 
  widget.isUpdate = false;   
  super.initState();
}
于 2018-12-25T07:18:16.170 回答