【问题标题】:Space is not adding after word in textfield (Flutter)文本字段中的单词后未添加空格(颤振)
【发布时间】:2021-10-09 06:52:18
【问题描述】:

我是 Flutter 的新手,我的应用程序中有一个文本字段,当我输入空格时,它不会添加到文本字段中。我已经用 ListTile 敲击了我的文本字段和按钮...这个问题是因为 ListTile 吗?如果是,那么 ListTile 的替代方案是什么,或者如果不是这种情况,那么究竟是什么问题? 我的代码如下:

class _HomeState extends State<Home> {

  class _HomeState extends State<Home> {

  TextEditingController _enterDataField = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: AppBar(
          title: new Text("Read/Write"),
          backgroundColor: Colors.blueAccent,
          centerTitle: true,
        ),
        body: new Container(
            padding: EdgeInsets.all(13.4),
            alignment: Alignment.center,

            child: new ListTile(
              title: new TextField(
                keyboardType: TextInputType.multiline,
                controller: _enterDataField,

                decoration: new InputDecoration(
                  prefixIcon: Icon(Icons.message),
                  border: OutlineInputBorder(),
                  labelText: 'Write Something',
                ),
              ),

                subtitle: TextButton(
                  style: ButtonStyle(
                    foregroundColor: MaterialStateProperty.all<Color>(Colors.blue),
                  ),
                  onPressed: () {
                    writeData(_enterDataField.text);
                  },

                  child: new Column(
                    children: [
                      new Text('Save Data'),
                      new Padding(padding: EdgeInsets.all(14.0)),
                      new FutureBuilder(
                      future: readData(),
                      builder: (BuildContext context, AsyncSnapshot<String?> data) {
                            if(data.hasData!=null)
                              {
                               return new Text(
                                   data.data.toString(),
                               style: new TextStyle(color: Colors.blueAccent),
                               );
                              }
                            else
                              {
                                return new Text('No Data Saved');
                              }
                      }

                      )],
                  ),
                )


            )
        ));
  }
}




Future<String?> get _localPath async
  {
    final directory = await getApplicationDocumentsDirectory();
    return directory.path;
  }

  Future<File> get _localFile async
  {
    final path =await _localPath;
    return new File('$path/data.txt');
  }

  Future<File> writeData(String? message) async
  {
    final file =await _localFile;
     return file.writeAsString('$message');
  }

  Future<String?> readData() async
  {
    try
    {
    final file =await _localFile;
    String? data = await file.readAsString();
    return data;
    }
    catch(e)
    {
      return "Nothing Saved Yet!";
    }
}

【问题讨论】:

  • 分享更多关于 readData() 和 writeData() 的细节。
  • @RohitChaursiya 这些是未来的功能,我已经添加...请检查
  • 请检查以下代码。

标签: android ios flutter dart flutter-layout


【解决方案1】:
TextField(
  keyboardType: TextInputType.multiline,
  maxLength: null,
  maxLines: null,
)

【讨论】:

    【解决方案2】:

    这里我已经尝试解决了。检查下面的代码。

    
    class _HomeState extends State<Home> {
      TextEditingController _enterDataField = TextEditingController();
    
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
            appBar: AppBar(
              title: new Text("Read/Write"),
              backgroundColor: Colors.blueAccent,
              centerTitle: true,
            ),
            body: new Container(
                padding: EdgeInsets.all(13.4),
                alignment: Alignment.center,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    ListTile(
                        title: TextField(
                          keyboardType: TextInputType.multiline,
                          controller: _enterDataField,
                          decoration: new InputDecoration(
                            prefixIcon: Icon(Icons.message),
                            border: OutlineInputBorder(),
                            labelText: 'Write Something',
                          ),
                          minLines: 1,
                          maxLines: 10,
                        ),
                        subtitle: TextButton(
                          style: ButtonStyle(
                            foregroundColor:
                                MaterialStateProperty.all<Color>(Colors.blue),
                          ),
                          onPressed: () {
                            setState(() {
                              writeData(_enterDataField.text);
                              readData();
                            });
                          },
                          child: new Text('Save Data'),
                        )),
                    Padding(padding: EdgeInsets.all(14.0)),
                    FutureBuilder(
                        future: readData(),
                        builder:
                            (BuildContext context, AsyncSnapshot<String?> data) {
                          if (data.hasData != null) {
                            return new Text(
                              data.data.toString(),
                              style: new TextStyle(color: Colors.blueAccent),
                            );
                          } else {
                            return new Text('No Data Saved');
                          }
                        })
                  ],
                )));
      }
    }
    
    Future<String?> get _localPath async {
      final directory = await getApplicationDocumentsDirectory();
      return directory.path;
    }
    
    Future<File> get _localFile async {
      String path = await _localPath as String;
    
      return File('$path/data.txt');
    }
    
    Future<File> writeData(String? message) async {
      final file = await _localFile;
      return file.writeAsString('$message');
    }
    
    Future<String?> readData() async {
      try {
        final file = await _localFile;
        String? data = await file.readAsString();
        return data;
      } catch (e) {
        return "Nothing Saved Yet!";
      }
    }
    
    

    【讨论】:

      猜你喜欢
      • 2020-12-11
      • 2021-11-24
      • 2018-09-21
      • 2020-07-04
      • 2020-11-24
      • 2020-04-30
      • 1970-01-01
      • 1970-01-01
      • 2017-11-13
      相关资源
      最近更新 更多