Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scroll SingleChildScrollView programmatically?

How to scroll SingleChildScrollView programmatically?

On long Form, it is desirable to automatically scroll to a require input field.

like image 490
Kyaw Tun Avatar asked Feb 21 '18 08:02

Kyaw Tun


People also ask

How do I scroll automatically in Flutter?

You can create a ScrollController and pass it to the controller parameter of your scrolling widget. Then you can use the animateTo method to animate to an offset.

When should I use SingleChildScrollView?

This widget is useful when you have a single box that will normally be entirely visible, for example a clock face in a time picker, but you need to make sure it can be scrolled if the container gets too small in one axis (the scroll direction).

What is ScrollController in Flutter?

A scroll controller creates a ScrollPosition to manage the state specific to an individual Scrollable widget. To use a custom ScrollPosition, subclass ScrollController and override createScrollPosition. A ScrollController is a Listenable.


2 Answers

Use ScrollController to scroll inside Column or ListView

Examples:

Colum With SingleChildScrollView

 class ScrollContainerPage extends StatelessWidget {
      ScrollController _scrollController = ScrollController();
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('ScrollView Example'),
          ),
          body: SingleChildScrollView(
            controller: _scrollController,
            child: ListView(
              children: [
                RaisedButton(
                  onPressed: () {
                   /// Scroll maximum end, if you want you can give hardcoded values also in place of _scrollController.position.maxScrollExtent
                    _scrollController.animateTo(
                        _scrollController.position.maxScrollExtent,
                        duration: Duration(milliseconds: 500),
                        curve: Curves.ease);
                  },
                  child: Text('Scroll To Bottom'),
                ),
                SizedBox(height: 20,),
                Container(height: 200, color: Colors.red,),
                SizedBox(height: 20,),
                Container(height: 200, color: Colors.blue,),
                SizedBox(height: 20,),
                Container(height: 200, color: Colors.green,),
                SizedBox(height: 20,),
                Container(height: 200, color: Colors.yellow,),
                SizedBox(height: 20,),
              ],
            ),
          ),
        );
      }
    }

ListView

class ScrollContainerPage extends StatelessWidget {
  ScrollController _scrollController = ScrollController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('ScrollView Example'),
      ),
      body: ListView(
        controller: _scrollController,
        children: [
          RaisedButton(
            onPressed: () {
              _scrollController.animateTo(
                  _scrollController.position.maxScrollExtent,
                  duration: Duration(milliseconds: 500),
                  curve: Curves.ease);
            },
            child: Text('Scroll To Bottom'),
          ),
          SizedBox(height: 20,),
          Container(height: 200, color: Colors.red,),
          SizedBox(height: 20,),
          Container(height: 200, color: Colors.blue,),
          SizedBox(height: 20,),
          Container(height: 200, color: Colors.green,),
          SizedBox(height: 20,),
          Container(height: 200, color: Colors.yellow,),
          SizedBox(height: 20,),
        ],
      ),
    );
  }
}

Note: Only thing need to make sure that ScrollController assigned to respective widget

like image 161
Jitesh Mohite Avatar answered Sep 22 '22 06:09

Jitesh Mohite


There are multiple solutions :

  • Using ScrollController: Assign it to controller field of your SingleChildScrollView. And use myScrollController.animateTo or myScrollController.jumpTo
  • Use static methods of Scrollable. Possibly Scrollable.ensureVisible. But be aware that the context parameter is the context of the widget that needs to be visible. You can get that context by using GlobalKey.
like image 44
Rémi Rousselet Avatar answered Sep 20 '22 06:09

Rémi Rousselet