【发布时间】:2020-02-07 15:31:17
【问题描述】:
当我设置 Textfield autofocus:false 时,它不会刷新页面,但是当我点击 TextField 时,键盘会显示然后主页面会重建,这会导致延迟。
这个问题已经存在将近一周了。我可以找到与文本字段重建 UI 相关的问题,但该解决方案不适用于我的。
当按钮被点击时,主页包含此功能
void addCommentModal() {
showModalBottomSheet(
context: context,
builder: (BuildContext context) {
return Container(
padding:
EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom),
child: AddCommentModal(
onPost: (String text) {
// APIServices.commentPost(context, i.toString(), text);
Navigator.pop(context);
},
),
);
},
);
}
添加评论模式
class AddCommentModal extends StatefulWidget {
final ValueChanged<String> onPost;
AddCommentModal({@required this.onPost});
@override
_AddCommentModalState createState() => _AddCommentModalState();
}
class _AddCommentModalState extends State<AddCommentModal> {
final commentController = TextEditingController();
bool _canPost = false;
String defaultProfilePhoto = "";
@override
void initState() {
defaultProfilePhoto = Constants.userFirstName[0].toUpperCase();
super.initState();
}
@override
void dispose() {
commentController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
print("PHOTO: ${Constants.userProfilePhoto}");
return Container(
padding: EdgeInsets.all(10),
child: Row(
children: <Widget>[
Container(
width: 50,
height: 50,
child: ClipRRect(
borderRadius: new BorderRadius.circular(50),
child: Constants.userProfilePhoto == null
? Container(
color: Color(colorPrimary),
alignment: Alignment.center,
child: Text(
defaultProfilePhoto,
style: TextStyle(
color: Color(colorText), fontSize: 20),
),
)
: Image.network(
APIServices.httpDomain + Constants.userProfilePhoto,
fit: BoxFit.cover,
)),
),
Expanded(
child: Container(
margin: EdgeInsets.only(
left: 10,
),
child: TextField(
controller: commentController,
autofocus: true,
decoration: new InputDecoration(
suffixIcon: IconButton(
onPressed: () => widget.onPost(commentController.text),
icon: Icon(
FontAwesomeIcons.paperPlane,
size: 15,
color: Theme.of(context).primaryColor,
)),
contentPadding: EdgeInsets.all(10),
hintText: "Add a comment ...",
fillColor: Colors.white,
border: new OutlineInputBorder(
borderRadius: new BorderRadius.circular(20.0),
),
),
keyboardType: TextInputType.text,
style: new TextStyle(fontFamily: "Poppins", fontSize: 15),
),
))
],
));
}
}
【问题讨论】:
标签: flutter dart flutter-layout flutter-test