Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 颤振将文本onchange值指定给变量_Android_Flutter - Fatal编程技术网

Android 颤振将文本onchange值指定给变量

Android 颤振将文本onchange值指定给变量,android,flutter,Android,Flutter,我正在学习颤振教程,遇到了一个问题。它应该从textfield onChanged函数中获取一个值,并将其分配给一个变量。但是它不起作用。因为它是在iphone上显示的,所以我想它在android上的工作原理可能有点不同 @override Widget build(BuildContext context) { String newTaskTitle; return Container( color: Color(0xff757575), ...

我正在学习颤振教程,遇到了一个问题。它应该从textfield onChanged函数中获取一个值,并将其分配给一个变量。但是它不起作用。因为它是在iphone上显示的,所以我想它在android上的工作原理可能有点不同

@override
  Widget build(BuildContext context) {
    String newTaskTitle;
    return Container(
       color: Color(0xff757575),
       ....

TextField(
  autofocus: true,
  textAlign: TextAlign.center,
  onChanged: (newText) {
     newTaskTitle = newText;
  },
),
FlatButton(
   child: Text(
       'Add',
       style: TextStyle(
           color: Colors.white,
           ),
        ),
        color: Colors.lightBlueAccent,
        onPressed: () {
           print(newTaskTitle);
        },

Kalev,尝试使用
StatefulWidget
并在需要新文本时刷新状态,如下所示

class MyApp extends StatefulWidget {
  @override
  MyAppState createState() => MyAppState();
}

class MyAppState extends State<MyApp> {
  String text = 'Original text';
  String newTaskTitle;
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(text),
            TextField(
              autofocus: true,
              textAlign: TextAlign.center,
              onChanged: (newText) {
                newTaskTitle = newText;
              },
            ),
            FlatButton(
                child: Text(
                  'Add',
                  style: TextStyle(
                    color: Colors.white,
                  ),
                ),
                color: Colors.lightBlueAccent,
                onPressed: () {
                  setState(() {
                    text = newTaskTitle;
                  });
                }),
          ],
        ),
      ),
    ));
  }
}

啊,颤振中最常见的问题之一

首先,您使用的是
StatefulWidget
?有状态小部件非常适合于状态管理。它们可以保存状态,通过使用名为
setState()
的函数,您可以更改状态

setState
做什么

setState()
函数再次调用
build
方法重建小部件

我建议将
newTaskTitle
变量存储在
build
方法之外,因为它不应该包含逻辑,但也因为每次调用
build
时,它都会重置该变量的值

现在,还记得
setState
函数吗?您应该像这样在
onChanged
回调中使用它

TextField(
  autofocus: true,
  textAlign: TextAlign.center,
  onChanged: (newText) {
     setState(() {
       newTaskTitle = newText;
     });
  },
),
现在,当用户在文本字段中键入时,它将更新该值。但是等一下,我们还没做完。我不建议使用
onChanged
方法来存储字段的当前值。我建议您使用
文本编辑控制器
,并将其分配给
文本字段
控制器
属性。您可以使用
textededitingcontroller.text
值访问文本。如果您不想使用它,至少使用回调
onEditingComplete
,该回调仅在用户编辑完字段后运行,因此不会在每次字段更改时调用
setState


 static String newTaskTitle;
@override
  Widget build(BuildContext context) {
   
    return Container(
       color: Color(0xff757575),
       ....

希望这能起作用

谢谢,但我正在试图理解为什么它在iphone教程中起作用,而在android emulator上不起作用。不确定你在遵循哪个教程,但你需要设置状态以访问变量的新状态。这是一个与android相关的问题,改为有状态小部件修复了它。问题是,当键盘盖上按钮,然后在输入后,文本字段被重置,我一直得到空值。谢谢大家的快速反应!

 static String newTaskTitle;
@override
  Widget build(BuildContext context) {
   
    return Container(
       color: Color(0xff757575),
       ....