'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?
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
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,
);
}
}
Solution 1:[1]
Try moving your code that runs the refetching of the chats into init state
@override
void initState() {
context.read<ChatHistoryCubit>().getChatHistory(widget.code);
super.initState();
}
Then remove context.read<ChatHistoryCubit>().getChatHistory(widget.code); from pageBuild()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mozes Ong |
