android - 为什么我在Flutter中不使用String?

标签 android flutter dart mobile

我有一件简单的事情有问题,但我不知道代码中是怎么回事。
下面是代码:

  Widget build(BuildContext context) {
    String newTaskTitle;
    return Container(
      color: Color(0xff757575),
      child: Container(
        padding: EdgeInsets.all(20.0),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(20.0),
            topRight: Radius.circular(20.0),
          ),
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: <Widget>[
            Text(
              'Add Task',
              textAlign: TextAlign.center,
              style: TextStyle(
                fontSize: 30.0,
                color: Colors.lightBlueAccent,
              ),
            ),
            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);
              },
            ),
          ],
        ),
      ),
    );
  }
我尝试获取文本字段输入的值,并将其传递到另一个屏幕,但是当我键入一些内容然后按下按钮时,它为我提供了空值。
enter image description here

最佳答案

您也可以使用 Controller 来实现。只需初始化一个并在TextField小部件中指定 Controller 即可。要访问文本,只需执行_controlller.text,如下所示:

  final _controller = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        color: Color(0xff757575),
        child: Container(
          padding: EdgeInsets.all(20.0),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.only(
              topLeft: Radius.circular(20.0),
              topRight: Radius.circular(20.0),
            ),
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: <Widget>[
              Text(
                'Add Task',
                textAlign: TextAlign.center,
                style: TextStyle(
                  fontSize: 30.0,
                  color: Colors.lightBlueAccent,
                ),
              ),
              TextField(
                controller: _controller,
                autofocus: true,
                textAlign: TextAlign.center,
              ),
              FlatButton(
                child: Text(
                  'Add',
                  style: TextStyle(
                    color: Colors.white,
                  ),
                ),
                color: Colors.lightBlueAccent,
                onPressed: () {
                  print(_controller.text);
                },
              ),
            ],
          ),
        ),
      ),
    );
  }

关于android - 为什么我在Flutter中不使用String?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63227725/

相关文章:

java - AdMob v4.4 左下重力不起作用

flutter - 未定义名称 'Colors' 。在 Flutter 中使用 TextStyle 类时

firebase - 预期为 'Map<String, dynamic>' 类型的值,但得到了 'Null' 类型的值

flutter - 如何取消在主函数中打开的 StreamSubscription

dart - Flutter bloc 和 Firebase 电话认证

java - Android 中谷歌分析事件跟踪器的简单示例

android - 如何在运行时在 React Native 中请求 Android 设备位置的权限?

button - 带有透明png的Flutter按钮

android - Multidex 抛出 java.lang.NoClassDefFoundError、RxJava、Retrolambda

flutter - 在 Dart 中显示本地化标签的问题