我正在嘗試在我的 AddNoteScreen 中使用自定義 TextField 小部件。我不知道為什么它不列印 TextField 輸入或如何讓它作業。我認為擁有 TextEditingController 就足夠了。我是新手。很抱歉發布這么多代碼,我不知道該怎么解釋。
這是我的自定義小部件:
class MyWidget extends StatefulWidget {
final Widget textFields;
MyWidget(
{required this.textFields});
@override
State<MyWidget> createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
TextEditingController notesController = TextEditingController();
List<TextField> textFields = [];
@override
void initState() {
super.initState();
textFields.add(_buildTextField());
}
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.only(left: 10, right: 10),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(
Radius.circular(10),
),
color: Colors.white,
),
height: 300,
alignment: Alignment.centerLeft,
child: ListView(
children: textFields,
),
);
}
TextField _buildTextField() {
return TextField(
style: TextStyle(
color: Colors.black,
fontSize: 18,
),
decoration: InputDecoration(
prefix: Icon(
Icons.circle,
size: 10,
color: Colors.black,
),
prefixIconConstraints: BoxConstraints(
minWidth: 20,
minHeight: 10,
maxHeight: 10,
maxWidth: 20,
),
),
autofocus: true,
onSubmitted: (_) {
setState(() {
textFields.add(_buildTextField());
notesController.text ' ';
});
}
);
}
}
我的 AddNoteScreen:
class AddNoteScreen extends StatefulWidget {
User user;
AddNoteScreen({
required this.user,
});
@override
State<AddNoteScreen> createState() => _AddNoteScreenState();
}
class _AddNoteScreenState extends State<AddNoteScreen> {
TextEditingController notesController = TextEditingController();
FirebaseFirestore firestore = FirebaseFirestore.instance;
bool loading = false;
@override
void initState(){
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor:Color (0xFF162242),
elevation: 0,
),
body: GestureDetector(
onTap: () {
FocusScope.of(context).unfocus();
notesController.text = '';
},
child: SingleChildScrollView(
child: Padding(
padding: EdgeInsets.all(20),
child: Column(children: [
Container(
padding: EdgeInsets.only(left: 10, right: 10),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(10),),
color: Colors.white,
),
child: MyWidget(
textFields: TextField(
style: TextStyle (color: Color(0xFF192A4F),fontSize: 18,),
textCapitalization: TextCapitalization.sentences,
controller: notesController,
decoration: InputDecoration(
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.transparent),
),
),
),
),
),
SizedBox(height: 50,),
loading ? Center (child: CircularProgressIndicator(),) : Container(
height: 50,
width: MediaQuery.of(context).size.width,
child: ElevatedButton(
onPressed: ()async{
if (
notesController.text.isEmpty)
{
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text("All feilds are required")));
} else {
setState(() {
loading = true;
});
await FirestoreService().insertNote(notesController.text,
widget.user.uid);
CollectionReference notes = firestore.collection('notes');
QuerySnapshot allResults = await notes.get();
allResults.docs.forEach((DocumentSnapshot result) {
print(result.data());
});
setState(() {
loading = false;
});
Navigator.pop(context);
}
}, child: Text("Add Note", style: TextStyle(
fontSize: 25,
fontWeight: FontWeight.bold,
),
),style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Color (0xFF162242)),
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
),
),
//color:Color (0xFF162242)
//ElevatedButton.styleFrom(primary: Color (0xFF162242),),
),
),
]),),
),
),
);
我的 FirestoreService 頁面:
class FirestoreService{
FirebaseFirestore firestore = FirebaseFirestore.instance;
Future insertNote(String notes, String userId)async{
try{
await firestore.collection('notes').add({
"notes":notes,
"userId":userId
});
} catch(e){
}
}
}
還有我的 NoteModel 螢屏:
class NoteModelEdit {
String id;
String notes;
String userId;
NoteModelEdit({
required this.id,
required this.notes,
required this.userId
});
factory NoteModelEdit.fromJson(DocumentSnapshot snapshot){
return NoteModelEdit(
id: snapshot.id,
notes: snapshot['notes'],
userId: snapshot['userId']
);
}
}


任何幫助表示贊賞!
uj5u.com熱心網友回復:
我相信您正在存盤一個空便箋,這就是資料庫結果為空的原因。
這一行:
if (notesController.text.isNotEmpty) // show error message
應該變成這樣:
if (notesController.text.isEmpty) // show error message
此外,這也應該改變:
// This does nothing:
new TextEditingController().clear();
// To clear the controller:
notesController.text = '';
更新
如下更改代碼以測驗是否將某些內容寫入資料庫。一旦這個作業,弄清楚如何正確使用 MyWidget,因為那里也有問題。
// OLD CODE
child: MyWidget(
textFields: TextField(
style: TextStyle (color: Color(0xFF192A4F),fontSize: 18,),
textCapitalization: TextCapitalization.sentences,
controller: notesController,
decoration: InputDecoration(
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.transparent),
),
),
),
),
// NEW CODE
child: TextField(
style: TextStyle (color: Color(0xFF192A4F),fontSize: 18,),
textCapitalization: TextCapitalization.sentences,
controller: notesController,
decoration: InputDecoration(
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.transparent),
),
),
),
我建議始終只更改一小部分代碼,并確保您的代碼在每個小步驟中都能正常作業。不要一次進行太多更改。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/479895.html
上一篇:在firebase云函式中使用async/await
下一篇:如何訪問Firebase專案
