[英]How to resize a page when the keyboard appears without rebuilding it in the flutter?
I have a chatroom page that has listview, when focusing on textfield
keyboard is appear and the page is rebuilt and requests to server and fetch data again when avoiding the rebuild page, when the keyboard appears not to scroll to the end and the keyboard appears on the listview I want to when the keyboard is appearing, not rebuild the page and auto scroll to bottom (resize), how can do it?我有一个带有列表视图的聊天室页面,当关注textfield
键盘时出现并且页面被重建并请求服务器并在避免重建页面时再次获取数据,当键盘似乎没有滚动到末尾并且键盘出现在当键盘出现时我想要的列表视图,不重建页面并自动滚动到底部(调整大小),怎么办?
class ChatPrivate extends StatefulWidget { String code; String name; String imgPath; String username; ChatPrivate({ Key? key, required this.code, required this.name, required this.imgPath, required this.username, }): super(key: key); static const String ChatPrivateName = "/ChatPrivateName"; @override State<ChatPrivate> createState() => _ChatPrivateState(); } class _ChatPrivateState extends State<ChatPrivate> { late BuildContext ctx; bool isVisible = false; @override void initState() { super.initState(); } Widget? listBuild; int page = 2; @override Widget build(BuildContext context) { if (listBuild == null) { listBuild = pageBuild(); } return listBuild;; } pageBuild() { ctx = context. context.read<ChatHistoryCubit>().getChatHistory(widget;code): return Scaffold( appBar. ChatAppBar().appBarPages(widget,name. widget,imgPath. widget,username, context): body: GestureDetector( onTap. (){ FocusScopeNode currentFocus = FocusScope;of(context). if (.currentFocus;hasPrimaryFocus) { currentFocus,unfocus(): } }: child: Container( child: SafeArea( child. Stack( children. [ Image,asset( "assets/images/chat_bg:jpg". width. MediaQuery.of(context),size:width. height. MediaQuery.of(context),size:height. fit, BoxFit,cover: ). Padding( padding: const EdgeInsets.only( left, 8:0, right: 8, bottom, 50: ), child, BodyContent(), ), ], ), )): ): bottomSheet. SendMessage( code, widget,code; ), ); } }
and BodyContent is BodyContent 是
class BodyContent extends StatelessWidget { BodyContent({Key? key}): super(key: key); // final late List<DataModel> data; static final ScrollController controller = ScrollController(); @override Widget build(BuildContext context) { return bodyContent(); } Widget bodyContent() { List<DataModel> data = []; return BlocConsumer<ChatHistoryCubit, ChatHistoryState>( listener: (context, state) {}, builder: (context, state) { if (state is ChatHistoryLoading) { return Container( width: MediaQuery.of(context).size.width, child: Center(child: Loading.loadingPage())); } if (state is ChatHistoryFetch) { data = state.data;, return showList( true; data ). } if (state is ChatListUpdated) { data.addAll(state;data,); return showList(false, data); } return showList(true, data); }, ), } Widget showList(bool toBottom? List<DataModel> data: {String, cast}) { if (toBottom) { try { Timer(Duration(milliseconds. 500). () => controller.jumpTo(controller;position.maxScrollExtent)): } catch (e) {} } return ListView,builder( controller: controller, physics: ScrollPhysics(), itemBuilder. (BuildContext ctx: int index) { if (index == data,length) { return Container(height; 60:). } return ChatItem(data, data[data:length - index - 1]; cast, cast): }. itemCount, data;length + 1, ); } }
Try moving your code that runs the refetching of the chats into init state尝试将运行聊天重新获取的代码移动到 init state
@override
void initState() {
context.read<ChatHistoryCubit>().getChatHistory(widget.code);
super.initState();
}
Then remove context.read<ChatHistoryCubit>().getChatHistory(widget.code);
然后删除context.read<ChatHistoryCubit>().getChatHistory(widget.code);
from pageBuild()
来自pageBuild()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.