如何在 Flutter DropdownButtonFormField 中重置值
How to reset value in Flutter DropdownButtonFormField
我想重置 DropdownButtonFormField 的值,值更改为 null,但 DropdownButtonFormField 没有改变。问题出在哪里?如果我使用了 DropdownButton 它正在正确更改值,值已被清除。我需要使用 DropdownButtonFormField。
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
MyAppState createState() => MyAppState();
}
class MyAppState extends State<MyApp> {
String abc;
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center (
child: Column(
children: <Widget>[DropdownButtonFormField(
hint: Text('select value'),
value: abc,
items: <String>['A', 'B', 'C', 'D'].map((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
onChanged: (String newValue) {
setState(() {
abc = newValue;
});
},
),
Text("value is $abc"),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: (){
setState((){
abc = null;
});
},
tooltip: 'Reset',
child: Icon(Icons.clear),
)
),
);
}
}
您可以使用 GlobalKey<FormFieldState>
.
class SomeWidget extends StatelessWidget {
final GlobalKey<FormFieldState> _key;
@override
Widget build() {
return DropdownButtonFormField(
key: _key,
//...
)
}
reset() {
_key.currentState.reset();
}
}
class SomeWidget extends StatelessWidget {
final GlobalKey<FormFieldState> _key = GlobalKey<FormFieldState>();
@override
Widget build() {
return DropdownButtonFormField(
key: _key,
//...
)
}
reset() {
_key.currentState.reset();
}
}
我想重置 DropdownButtonFormField 的值,值更改为 null,但 DropdownButtonFormField 没有改变。问题出在哪里?如果我使用了 DropdownButton 它正在正确更改值,值已被清除。我需要使用 DropdownButtonFormField。
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
MyAppState createState() => MyAppState();
}
class MyAppState extends State<MyApp> {
String abc;
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center (
child: Column(
children: <Widget>[DropdownButtonFormField(
hint: Text('select value'),
value: abc,
items: <String>['A', 'B', 'C', 'D'].map((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
onChanged: (String newValue) {
setState(() {
abc = newValue;
});
},
),
Text("value is $abc"),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: (){
setState((){
abc = null;
});
},
tooltip: 'Reset',
child: Icon(Icons.clear),
)
),
);
}
}
您可以使用 GlobalKey<FormFieldState>
.
class SomeWidget extends StatelessWidget {
final GlobalKey<FormFieldState> _key;
@override
Widget build() {
return DropdownButtonFormField(
key: _key,
//...
)
}
reset() {
_key.currentState.reset();
}
}
class SomeWidget extends StatelessWidget {
final GlobalKey<FormFieldState> _key = GlobalKey<FormFieldState>();
@override
Widget build() {
return DropdownButtonFormField(
key: _key,
//...
)
}
reset() {
_key.currentState.reset();
}
}