Scroll To End Of The Listview Builder

[Solved] Scroll To End Of The Listview Builder | Swift - Code Explorer | yomemimo.com
Question : scroll to end of the ListView builder

Answered by : cooperative-caribou-okib3apu1vkj

import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
class MyList extends StatefulWidget { MyList({Key key}) : super(key: key); @override _MyListState createState() => _MyListState();
}
class _MyListState extends State<MyList> { final ScrollController _scrollController = ScrollController(); final lastKey = GlobalKey(); List<String> items; @override void initState() { super.initState(); items = List<String>.generate(8, (i) => "Item $i"); } void add() { setState(() { items.add("new Item ${items.length}"); }); SchedulerBinding.instance.addPostFrameCallback((_) => scrollToEnd()); } void scrollToEnd() async { await _scrollController.animateTo( _scrollController.position.maxScrollExtent, duration: const Duration(milliseconds: 350), curve: Curves.easeOut); Scrollable.ensureVisible(lastKey.currentContext); } @override Widget build(BuildContext context) { return MaterialApp( title: "List", home: Scaffold( body: ListView.builder( controller: _scrollController, itemCount: items.length, shrinkWrap: true, itemBuilder: (context, index) { return ListTile( title: Text('${items[index]}'), key: index == items.length - 1 ? lastKey : null, ); }, ), floatingActionButton: FloatingActionButton( onPressed: () { add(); }, child: Icon(Icons.add), ), )); }
}

Source : https://stackoverflow.com/questions/57645089/flutter-listview-get-full-size-of-scrollcontroller-after-adding-item-to-list/57645944#57645944 | Last Update : Mon, 18 Oct 21

Answers related to scroll to end of the listview builder

Code Explorer Popular Question For Swift