I have created a simple demo to explain what i want..
I have a Listview builder which shows names,and a textfield for adding name to names list.
I have used following code for moving listview builder to last item while adding new name with IconButton,
_scrollController.jumpTo(_scrollController.position.maxScrollExtent);
but how to scroll to last item when Screen loading…I placed above code in InitState() but its not working…
so how to do it without changing order of names..
here is my code
class _HomeScreenState extends State<HomeScreen> {
TextEditingController txtname = TextEditingController();
final _scrollController = ScrollController();
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
body: Column(
children: [
Expanded(
child: ListView.builder(
controller: _scrollController,
itemCount: names.length,
itemBuilder: (context, index) => ListTile(
title: Text(names[index].toString()),
))),
Container(
child: TextField(
controller: txtname,
decoration: InputDecoration(
suffix: IconButton(
icon: Icon(Icons.send),
onPressed: () {
setState(() {
names.add(txtname.text);
_scrollController.jumpTo(
_scrollController.position.maxScrollExtent);
txtname.clear();
});
},
),
hintText: 'Enter Name'),
),
)
],
),
));
}
}
2
Answers
You can wrap the Listview with a
SingleChildScrollView
and set the reading direction to reverse:This worked for me,
So basically This function
WidgetsBinding.instance.addPostFrameCallback((_) {...
ensures that a callback after the frame has been painted. and usingTimer
waits for theListView.builder
to be completed first. You can also addFocusScope.of(context).unfocus();
to dismiss the keyboard