我正在設計一個 flutter 表單,它使用 TextField 小部件為兩個文本欄位輸入,一個使用 DateTimeField 的時間欄位和一個使用 WeekDay 選擇器小部件的日期選擇器。我使用了 reset 方法來重置表單,但這只是重置 BasicTimeField 小部件,無法重置所有其他欄位。
class MyCustomForm extends StatefulWidget {
const MyCustomForm({Key? key}) : super(key: key);
@override
State<MyCustomForm> createState() => _MyCustomFormState();
}
class _MyCustomFormState extends State<MyCustomForm> {
final formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
var ht = MediaQuery.of(context).size.height;
var wd = MediaQuery.of(context).size.width;
return Form(
key: formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
SizedBox(
height: ht / 20,
),
Padding(
padding: EdgeInsets.symmetric(horizontal: wd / 40, vertical: 1),
child: Text(
"Subject",
style: TextStyle(fontSize: ht / 30, fontWeight: FontWeight.w500),
),
),
Padding(
padding:
EdgeInsets.symmetric(horizontal: wd / 40, vertical: ht / 60),
child: TextField(
decoration: InputDecoration(
border: OutlineInputBorder(), // UnderlineInputBorder(),
hintText: 'Type Subject name here',
),
),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: wd / 40, vertical: 1),
child: Text(
"Class Link",
style: TextStyle(fontSize: ht / 30, fontWeight: FontWeight.w500),
),
),
Padding(
padding:
EdgeInsets.symmetric(horizontal: wd / 40, vertical: ht / 60),
child: const TextField(
decoration: InputDecoration(
border: OutlineInputBorder(), // UnderlineInputBorder(),
hintText: 'Paste class link here',
),
),
),
Center(
child: Text(
"Timing",
style: TextStyle(fontSize: ht / 30, fontWeight: FontWeight.w500),
)),
Row(mainAxisAlignment: MainAxisAlignment.center, children: [
BasicTimeField(),
SizedBox(
width: wd / 9,
child: Center(
child: Text(
"to",
style:
TextStyle(fontSize: ht / 40, fontWeight: FontWeight.w400),
),
),
),
BasicTimeField()
]),
SizedBox(
height: ht / 40,
),
Center(
child: Text(
"Select days of week",
style: TextStyle(fontSize: ht / 30, fontWeight: FontWeight.w500),
)),
DayPicker(),
SizedBox(
height: ht / 20,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
child: Text('Add'),
onPressed: () => formKey.currentState?.save(),
),
const SizedBox(
width: 50,
),
ElevatedButton(
child: Text('Reset'),
onPressed: () => formKey.currentState?.reset(),
),
],
)
],
),
);
}
}
uj5u.com熱心網友回復:
在您的表單中重置不起作用,因為您使用的文本欄位在使用時不會重置
formKey.currentState?.reset()
嘗試將 Textform 更改為 TextFormField
現在,它將起作用。
完整更新的代碼(來自您的代碼)
import 'package:flutter/material.dart';
class MyCustomForm extends StatefulWidget {
const MyCustomForm({Key? key}) : super(key: key);
@override
State<MyCustomForm> createState() => _MyCustomFormState();
}
class _MyCustomFormState extends State<MyCustomForm> {
final formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
var ht = MediaQuery.of(context).size.height;
var wd = MediaQuery.of(context).size.width;
return Scaffold(
body: Form(
key: formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
SizedBox(
height: ht / 20,
),
Padding(
padding: EdgeInsets.symmetric(horizontal: wd / 40, vertical: 1),
child: Text(
"Subject",
style:
TextStyle(fontSize: ht / 30, fontWeight: FontWeight.w500),
),
),
Padding(
padding:
EdgeInsets.symmetric(horizontal: wd / 40, vertical: ht / 60),
child: TextFormField(
decoration: const InputDecoration(
border: OutlineInputBorder(), // UnderlineInputBorder(),
hintText: 'Type Subject name here',
),
),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: wd / 40, vertical: 1),
child: Text(
"Class Link",
style:
TextStyle(fontSize: ht / 30, fontWeight: FontWeight.w500),
),
),
Padding(
padding:
EdgeInsets.symmetric(horizontal: wd / 40, vertical: ht / 60),
child: TextFormField(
decoration: const InputDecoration(
border: OutlineInputBorder(), // UnderlineInputBorder(),
hintText: 'Paste class link here',
),
),
),
Center(
child: Text(
"Timing",
style: TextStyle(fontSize: ht / 30, fontWeight: FontWeight.w500),
)),
Row(mainAxisAlignment: MainAxisAlignment.center, children: [
// BasicTimeField(),
SizedBox(
width: wd / 9,
child: Center(
child: Text(
"to",
style: TextStyle(
fontSize: ht / 40, fontWeight: FontWeight.w400),
),
),
),
// BasicTimeField()
]),
SizedBox(
height: ht / 40,
),
Center(
child: Text(
"Select days of week",
style: TextStyle(fontSize: ht / 30, fontWeight: FontWeight.w500),
)),
// DayPicker(),
SizedBox(
height: ht / 20,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
child: Text('Add'),
onPressed: () => formKey.currentState?.save(),
),
const SizedBox(
width: 50,
),
ElevatedButton(
child: Text('Reset'),
onPressed: () => formKey.currentState?.reset(),
),
],
)
],
),
),
);
}
}
輸出
輸入資料

按下復位后清除

uj5u.com熱心網友回復:
formKey.currentState?.reset()
這行代碼重置了所有Form() 后代的表單欄位。它僅適用于 TextFormField 小部件。
您可以將所有TextField小部件更改為TextFormField,以便重置對它們起作用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/363686.html
上一篇:在反應中設定自定義錯誤驗證
