【发布时间】: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