该问题可能由于在AlertDialog中同时使用TextField和DropdownButton导致冲突。为了解决这个问题,我们可以使用StatefulBuilder来重新构建AlertDialog的内容。
代码示例:
void _showMyDialog(BuildContext context) {
showDialog(
context: context,
builder: (BuildContext context) {
String dropdownValue = 'One';
return StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return AlertDialog(
title: Text('Title'),
content: SingleChildScrollView(
child: ListBody(
children: [
TextField(
onChanged: (value) {
setState(() {});
},
),
DropdownButton(
value: dropdownValue,
onChanged: (String newValue) {
setState(() {
dropdownValue = newValue;
});
},
items: ['One', 'Two', 'Three', 'Four']
.map>((String value) {
return DropdownMenuItem(
value: value,
child: Text(value),
);
}).toList(),
),
],
),
),
actions: [
FlatButton(
child: Text('Cancel'),
onPressed: () {
Navigator.of(context).pop();
},
),
FlatButton(
child: Text('OK'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
},
);
}
在StatefulBuilder中构建AlertDialog会重新构建AlertDialog的内容,使得TextField和DropdownButton可以同时使用而不会出现Jank问题。如果我们不使用StatefulBuilder,TextField和DropdownButton的状态变化导致AlertDialog的内容被不必要地重建或者绘制,从而出现Jank问题。 因此,通过使用StatefulBuilder,我们可以解决AlertDialog中使用TextField和DropdownButton导致的Jank问题。