简体   繁体   English

在 flutter 中,如何在不重建键盘的情况下调整页面大小?

[英]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.

相关问题 当键盘出现时,如何调整布局中单个视图的大小? - How to resize a single View in my Layout when the Keyboard appears? 键盘出现时如何正确调整布局大小? - How can I correctly resize the layout when the keyboard appears? 键盘出现时,android调整大小布局 - android resize layout when keyboard appears 出现键盘时调整屏幕的一部分 - Resize one part of screen when keyboard appears Flutter:出现键盘时背景图像正在挤压 - Flutter: Background image is squeezing when keyboard appears Flutter 键盘出现时溢出底部 - Flutter overflow bottom when keyboard appears 键盘出现时没有adjustPan推送列表视图 - Push Listview when keyboard appears without adjustPan 当软键盘出现在 android 上时,正确调整主 kivy 窗口的大小 - Properly resize main kivy window when soft keyboard appears on android Flutter Riverpod,如何避免使用消费者重建整个页面 - Flutter Riverpod, how avoid rebuilding whole page using consumer Flutter - 如何在键盘出现时使用 SingleScrollView() 滚动整个屏幕,而不仅仅是自动显示文本 - Flutter - how to use SingleScrollView() to Scroll Entire Screen when Keyboard Appears not just text automatically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM