![](/img/trans.png)
[英]Can I disable the automatic keyboard pop-up of the SearchDelegate Class?
[英]I can not save the date after editing in the pop-up box
代碼如下。 所有包都已導入,沒有錯誤。 它添加並保存文本,但在彈出框中編輯后不保存日期。 在“保存”部分,我收到此錯誤“未處理的異常:‘bool’類型不是‘String’類型的子類型”。 我也試過
Navigator.of(context).pop(textField?.controller?.text ?? "");
但這次它在編輯和保存后刪除了文本。
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'dart:io';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Notepad',
home: NoteList(),
);
}
}
class NoteList extends StatefulWidget {
@override
_NoteListState createState() => _NoteListState();
}
class _NoteListState extends State<NoteList> {
final List<String> _notes = [];
final TextEditingController _textController = TextEditingController();
void _loadNotes() async {
var directory = await getApplicationDocumentsDirectory();
var file = File("${directory.path}/notes.txt");
if (!await file.exists()) {
file.createSync();
file.writeAsStringSync("Hello, World!\nThis is your first note.\n");
}
var contents = await file.readAsString();
setState(() {
_notes.addAll(contents.split("\n"));
});
}
void _saveNotes() async {
var directory = await getApplicationDocumentsDirectory();
var file = File("${directory.path}/notes.txt");
var contents = _notes.join("\n");
await file.writeAsString(contents);
}
void _deleteNote(int index) {
setState(() {
_notes.removeAt(index);
});
_saveNotes();
}
@override
void initState() {
super.initState();
_loadNotes();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Notepad'),
),
body: Column(
children: [
Expanded(
child: ListView.builder(
itemCount: _notes.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(_notes[index]),
onTap: () async {
var result = await showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text("Edit Note"),
content: TextField(
controller: TextEditingController(text: _notes[index]),
autofocus: true,
),
actions: [
ElevatedButton(
child: Text("Cancel"),
onPressed: () => Navigator.of(context).pop(),
),
ElevatedButton(
child: Text("Save"),
onPressed: () {
final TextField? textField = context.findAncestorWidgetOfExactType<TextField>();
Navigator.of(context).pop(textField != null?.controller?.text);
},
),
],
);
},
);
// update the note with the new text if the user saved their changes
if (result != null) {
setState(() {
_notes[index] = result;
});
_saveNotes();
}
},
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: [
IconButton(
icon: Icon(Icons.delete),
onPressed: () => _deleteNote(index),
),
],
),
);
},
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
controller: _textController,
decoration: InputDecoration(
hintText: "Enter a new note",
),
),
),
ElevatedButton(
child: Text("Add Note"),
onPressed: () {
setState(() {
_notes.add(_textController.text);
});
_textController.clear();
_saveNotes();
},
),
],
),
);
}
}
我以前從未在我的代碼中使用過findAncestorWidgetOfExactType ,但它用於查找祖先。 但是當您使用 AlertDialog 時,它的內部小部件(如標題、內容和操作)被認為是兄弟。 這就是為什么您無法使用此方法找到 TextField 的原因。
嘗試在 showDialog 的構建器方法中創建一個臨時的 TextEditingController 並在 TextField & Save 按鈕中使用它。 像這樣
var result = await showDialog(
context: context,
builder: (context) {
TextEditingController temp = TextEditingController(text: _notes[index]);
return AlertDialog(
title: Text("Edit Note"),
content: TextField(
controller: temp,
autofocus: true,
),
actions: [
ElevatedButton(
child: Text("Cancel"),
onPressed: () => Navigator.of(context).pop(),
),
ElevatedButton(
child: Text("Save"),
onPressed: () {
Navigator.of(context).pop(temp.text);
},
),
],
);
},
);
你可以在這里測試代碼https://dartpad.dev/?id=e75b493dae1287757c5e1d77a0dc73f1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.