Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get full size of a ScrollController

Tags:

flutter

I've attached a ScrollController to a CustomScrollView of [SliverAppBar, SliverList]

In a default case I would use reverse:true and animateTo (0.0) to move the scroll to the last element added but in this case using reverse would also reverse the SliverAppBar/SliverList order !

So I'd like to use animateTo ( sizeOfScrollableAfterElementAdded ) but I can't find this value.

Thank you !

like image 970
Robert Felker Avatar asked May 23 '17 17:05

Robert Felker


People also ask

What is ScrollController?

A ScrollController is a Listenable. It notifies its listeners whenever any of the attached ScrollPositions notify their listeners (i.e. whenever any of them scroll). It does not notify its listeners when the list of attached ScrollPositions changes. Typically used with ListView, GridView, CustomScrollView.

What is scroll padding in flutter?

A sliver that applies padding on each side of another sliver. Slivers are special-purpose widgets that can be combined using a CustomScrollView to create custom scroll effects. A SliverPadding is a basic sliver that insets another sliver by applying padding on each side.


1 Answers

You can use _scrollController.position.maxScrollExtent to scroll to the end. Make sure to do this in a post-frame callback so that it will include the new item you just added.

Here is an example of a sliver list that scrolls to the end as more items are added.

before after

import 'package:flutter/scheduler.dart'; import 'package:flutter/material.dart';  void main() {   runApp(new MyApp()); }  class MyApp extends StatelessWidget {   @override   Widget build(BuildContext context) {     return new MaterialApp(       title: 'Flutter Demo',       home: new MyHomePage(),     );   } }  class MyHomePage extends StatefulWidget {   State createState() => new MyHomePageState(); }   class MyHomePageState extends State<MyHomePage> {   ScrollController _scrollController = new ScrollController();    List<Widget> _items = new List.generate(40, (index) {     return new Text("item $index");   });    @override   Widget build(BuildContext context) {     return new Scaffold(       floatingActionButton: new FloatingActionButton(         child: new Icon(Icons.arrow_downward),         onPressed: () {           setState(() {             _items.add(new Text("item ${_items.length}"));           });           SchedulerBinding.instance.addPostFrameCallback((_) {             _scrollController.animateTo(               _scrollController.position.maxScrollExtent,               duration: const Duration(milliseconds: 300),               curve: Curves.easeOut,             );           });         },       ),       body: new CustomScrollView(         controller: _scrollController,         slivers: [           new SliverAppBar(             title: new Text('Sliver App Bar'),           ),           new SliverList(             delegate: new SliverChildBuilderDelegate(               (context, index) => _items[index],               childCount: _items.length,             ),           ),         ],       ),     );   } } 
like image 119
Collin Jackson Avatar answered Sep 18 '22 03:09

Collin Jackson