【问题标题】:Flutter : Getting Values From TextField and Used Them In a FunctionFlutter:从 TextField 获取值并在函数中使用它们
【发布时间】:2021-03-09 12:05:25
【问题描述】:

我开发了一个应用程序,用户可以将他们的文档上传到 Firebase 存储。我可以轻松应对。但是对于文件夹,我需要两个来自 TextField 的参数。

用户将参数写入TextFields,然后参数将用于文件夹。

我的代码是:

            class Uploader extends StatefulWidget {
            final File file;

            TextEditingController _parameter1 = TextEditingController(); // definition of controller1

            TextEditingController _parameter1 = TextEditingController(); definition of controller1

            Uploader({Key key, this.file}) : super(key: key);

            createState() => _UploaderState();
              }

            class _UploaderState extends State<Uploader> {
            final FirebaseStorage _storage =
            FirebaseStorage(storageBucket: 'gs://emo- 
            is0.appspot.com');

            StorageUploadTask _uploadTask;

            _startUpload() async {

            String filePath = 
            'test/${parameter1}/${parameter2}/${DateTime.now()}.png';

            StorageUploadTask _uploadTask = 
            _storage.ref().child(filePath).putFile(widget.file);

             StorageTaskSnapshot taskSnapshot = await 
             _uploadTask.onComplete;
              Navigator.push(
              context,
              MaterialPageRoute(
              builder: (context) =>
             (InvoiceUploadScreen())));
               }

              @override
              Widget build(BuildContext context) {

                if (_uploadTask != null) {
                return StreamBuilder<StorageTaskEvent>(
                 stream: _uploadTask.events,
                 builder: (context, snapshot) {
                 var event = snapshot?.data?.snapshot;

                 double progressPercent = event != null
            ? event.bytesTransferred / event.totalByteCount
            : 0;

              return Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              if (_uploadTask.isComplete)
                Text('Yükleme Tamamlandı',
                    style: TextStyle(
                        color: Colors.greenAccent,
                        height: 2,
                        fontWeight: FontWeight.w500,
                        fontSize: 30)
                ),

              if (_uploadTask.isPaused)
                FlatButton(
                  child: Icon(Icons.play_arrow, size: 50),
                  onPressed: _uploadTask.resume,
                ),
              if (_uploadTask.isInProgress)
                FlatButton(
                  child: Icon(Icons.pause, size: 50),
                  onPressed: _uploadTask.pause,
                ),
              LinearProgressIndicator(value: progressPercent),
              Text(
                '${(progressPercent * 100).toStringAsFixed(2)} % ',
                style: TextStyle(fontSize: 50),
              ),
            ]
        );
          }
            );
           }

               else {

             return FlatButton.icon(
      color: Colors.blue,
      textColor: Colors.white,
      label: Text('Sunucuya Yükle'),
      icon: Icon(Icons.cloud_upload),
      onPressed: _startUpload);
    }
   }
  }

我无法定义 TextField。我不确定我需要把 TextField 放在哪里,因为我已经定义了控制器

   // HERE Parameter 1 and 2 will come from textfields

当按下发送按钮时,在存储中会有文件夹 test/parameter1/parameter2/a.png 结构

参数 1 和 2 都应该从文本字段中获取

我被困在这里,因为我如何在这里制作算法以及如何成功开发这种结构

谢谢

【问题讨论】:

  • “我无法定义 TextField。我不确定需要将 TextField 放在哪里,因为我已经定义了控制器” - TextField 是一个小部件,因此您将它放在 Widget build() 函数中。在您的代码中,有一个 Text 小部件,尝试将其替换为 TextField 并查看它在您的 UI 中的显示方式。然后根据您的要求进行调整。

标签: firebase flutter dart firebase-storage textfield


【解决方案1】:

使用步骤here 为您的每个 TextField 设置 TextEditingController。然后,您可以监听来自控制器的值并根据用户键入的内容更新 parameter1 和 parameter2。

【讨论】:

  • Joe 我刚刚更新了我的代码块,因为我不确定需要将 TextField 放在哪里,因为我已经定义了用于从 Textfields 获取值的控制器
猜你喜欢
  • 2021-03-08
  • 1970-01-01
  • 2021-07-22
  • 2020-10-01
  • 2019-01-18
  • 2019-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多