[英]Flutter - TextField in StatefulBuilder in modalBottomSheet not working
I have a problem.我有个问题。 In my code, I call
showModalBottomSheet
and inside of it, I have a feedback form.在我的代码中,我调用了
showModalBottomSheet
并在其中调用了一个反馈表。 The user can select a reaction and then leave a comment to submit.用户可以选择一个反应,然后留下评论以提交。
Therefore, I had to use a StatefulBuilder
to use setState
inside the modal.因此,我不得不使用
StatefulBuilder
在模态中使用setState
。 The TextField
has a weird behavior though—when I click on it, the keyboard appears for an instant and then the modal is reset to the original state. TextField
有一个奇怪的行为——当我点击它时,键盘会出现一瞬间,然后模态被重置为原始状态。 If I remove the StatefulBuilder
and I leave the GestureDetector
as main Widget, everything works as expected (the keyboard appears, the modal moves to show it, and so on).如果我删除
StatefulBuilder
并将GestureDetector
作为主要小部件,则一切都按预期工作(键盘出现,模式移动以显示它,等等)。
Is there any way to make the StatefulBuilder
and the TextField
coexist?有没有办法让
StatefulBuilder
和TextField
共存? Below there is a snippet of code, appropriately cut to focus on this problem.下面是一段代码,经过适当的剪裁以关注这个问题。
TextEditingController feedbackTextFieldController = TextEditingController();
...
showModalBottomSheet<void>(
context: context,
isScrollControlled: true, // makes content maxHeight = full device height
builder: (BuildContext context) {
bool _isFeedbackLoading = false;
return StatefulBuilder(
builder: (context, setState) => GestureDetector(
onTap: () {
// the following is needed to dismiss the keyboard on tap outside of it
FocusScopeNode currentFocus = FocusScope.of(context);
if (!currentFocus.hasPrimaryFocus) {
currentFocus.unfocus();
}
},
child: AnimatedPadding(
// to make the modal move when the keyboard is shown
padding: MediaQuery.of(context).viewInsets,
duration: const Duration(milliseconds: 100),
curve: Curves.decelerate,
child: Container(
child: _isFeedbackLoading
? Center(
child: CircularProgressIndicator(),
)
: TextField(
controller: feedbackTextFieldController,
textInputAction: TextInputAction.done,
textCapitalization: TextCapitalization.sentences,
maxLines: 2,
style: ...,
decoration: ...,
),
),
),
),
);
},
);
Thank you!谢谢!
you can give try to following code您可以尝试以下代码
showModalBottomSheet<void>(
context: context,
isScrollControlled: true, // makes content maxHeight = full device height
builder: (BuildContext context) {
bool _isFeedbackLoading = false;
return StatefulBuilder(
// it could also because of you may reaching the wrong context
// builder: (context, setState) => GestureDetector(
builder: (_, setState) => GestureDetector(
// may be you are not able to tap
behavior: HitTestBehavior.opaque,
onTap: () {
// the following is needed to dismiss the keyboard on tap outside of it
FocusScopeNode currentFocus = FocusScope.of(context);
if (!currentFocus.hasPrimaryFocus) {
currentFocus.unfocus();
}
},
child: AnimatedPadding(
// to make the modal move when the keyboard is shown
padding: MediaQuery.of(context).viewInsets,
duration: const Duration(milliseconds: 100),
curve: Curves.decelerate,
child: Container(
child: _isFeedbackLoading
? Center(
child: CircularProgressIndicator(),
)
: TextField(
controller: feedbackTextFieldController,
textInputAction: TextInputAction.done,
textCapitalization: TextCapitalization.sentences,
maxLines: 2,
style: ...,
decoration: ...,
),
),
),
),
);
},
);
or you can try to make it another StatefulWidget
for this, like following或者您可以尝试为此设置另一个
StatefulWidget
,如下所示
showModalBottomSheet<void>(
context: context,
isScrollControlled: true, // makes content maxHeight = full device height
builder: (BuildContext context) {
return YourRefactoredStatefulWidget();
}
.....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.