English 中文(简体)
文本处理中的问题 主计长
原标题:Problem with TextEditingController on back pressed

我在“彩虹”应用中拥有以下<代码>StatefulWidget:

class EditProfilePage extends StatefulWidget {
  const EditProfilePage({super.key});

  @override
  State<EditProfilePage> createState() => _EditProfilePageState();
}

class _EditProfilePageState extends State<EditProfilePage> {
  ProfileClient client = ProfileClient();
  TextEditingController usernameController = TextEditingController();
  Future<ProfileResponse>? futureProfileResponse;

  @override
  void initState() {
    super.initState();
    futureProfileResponse = fetch();
  }

  Future<ProfileResponse> fetch() async {
    String key = await Utils.getKey();
    return client.get(key);
  }

  @override
  void dispose() {
    usernameController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text( Edit profile ),
        ),
        body: SingleChildScrollView(
            child: ConstrainedBox(
                constraints: BoxConstraints(
                    maxHeight: MediaQuery.of(context).size.height),
                child: buildPage())));
  }

  FutureBuilder<ProfileResponse> buildPage() {
    return FutureBuilder<ProfileResponse>(
        future: futureProfileResponse,
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            ProfileResponse profileResponse = snapshot.data!;
            if (profileResponse.code == 200) {
              usernameController.text = profileResponse.profile.username;
              return Padding(
                padding: const EdgeInsets.all(24),
                child: Column(
                  children: [
                    TextField(
                      controller: usernameController,
                      decoration: const InputDecoration(
                        labelText:  Username ,
                      ),
                    ),
                  ],
                ),
              );
            }
          }
          return const Center(child: CircularProgressIndicator());
        });
  }
}

我有意理解为什么在用户名称<代码>Text Field内写字,然后将背纽扣,<代码>Text< Field/code>的内容又回到原来的价值。

This is a demo to make better understand my problem: Demo GIF

确切地说,这个问题在我用<代码>Constraining Box widget对”方法进行总结后即开始出现。 我之所以这样做,是因为在我的原始网页上,我有更多的<代码>Text Field widgets,我需要他们在打开钥匙板时“搬上”。 为了简单起见,我删除了与这一具体问题无关的部分法典。

问题回答

当用户开放/披露钥匙板时,现有的屏幕规模将发生变化,从而重建植被。 整条<代码>FutureBuilder将重新设计,builder将在您将案文分配给控制人时执行。 您可以通过增加一份书面声明来核实:

print( Widget rebuild at ${DateTime.now()} );
usernameController.text = profileResponse.profile.username;

您使用<代码>FutureBuilder的理由是,只有在以后填写并填写<代码>后才能使<>text Field。 确定控制者的价值应与这一植被建设机制分开。 页: 1 <代码>fetch中的控制器数值 方法:

Future<ProfileResponse> fetch() async {
  String key = await Utils.getKey();
  final profileResponse = client.get(key);
  usernameController.text = profileResponse.profile.username;
  return profileResponse;
}

在<代码>FutureBuilder中,删除这一行文:

usernameController.text = profileResponse.profile.username;

这样,当植被重建时,<代码>用户名总经理。





相关问题
format text_field helper?

Is it possible to apply strftime formatting to the value of a text input field using a Rails text_field helper? In the form I use for creating or editing a record, I have a text input field which is ...

django admin TinyMCE integration

This is weird: I have installed and configured django-tinymce, but it doesn t seem to work with django admin. this works fine with Safari: class ArticleAdmin(admin.ModelAdmin): ...

TextField in UITableView

I m new to iphone world .. help me out of this. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *MYIdentifier =@"MyIdentifier"; ...

Static text within text field?

How can i keep static text within a <input type="text" name="site"> text field, just like the tumblr account ***.tumblr.com here > http://www.tumblr.com/

Select only first four lines, from Sql text field

Sql Server 2008 Express >> Visual Web Developer >> C# I m pulling records out of the table like this: SELECT Name, Category, Review FROM ReviewTable This works fine but the Review field Type in SQL ...

Flash Dynamic Text Width

Hey! I try to find out the text size of a textfield in the following example field.text = aaaaaaa ; trace(field.textWidth); setTimeput(function(){ field.text = aa ; trace(field.textWidth); },...

Textfield and Go Button

I would like to place search textfield and the Go button side by side. It looks aligned equally in Moz, Opera, IE8, Safari but not in IE7. IE7 shows 1px gap in height. How it can be done for IE7?

热门标签