当键盘出现时,如何调整页面大小而不在颤动中重建



我有一个聊天室页面,它有列表视图,当关注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,
),
);
}
}

并且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,
);
}
}

试着将运行聊天重新蚀刻的代码移动到初始状态

@override
void initState() {
context.read<ChatHistoryCubit>().getChatHistory(widget.code);
super.initState();
}

然后从pageBuild()中删除context.read<ChatHistoryCubit>().getChatHistory(widget.code);

最新更新