Flutter: singlechildscrollview scroll to position

Flutter: SingleChildScrollView Scroll to Position

The SingleChildScrollView widget in Flutter allows you to create a scrollable content area with a single child widget. To scroll to a specific position within the child widget, you can use a ScrollController.

      import 'package:flutter/material.dart';
      class MyScrollableWidget extends StatefulWidget {
        _MyScrollableWidgetState createState() => _MyScrollableWidgetState();
      class _MyScrollableWidgetState extends State {
        final ScrollController _controller = ScrollController();
        void dispose() {
        void scrollToPosition() {
            // The position you want to scroll to
            duration: Duration(milliseconds: 500),
            curve: Curves.easeInOut,
        Widget build(BuildContext context) {
          return Scaffold(
            appBar: AppBar(title: Text('Scrollable Widget Example')),
            body: SingleChildScrollView(
              controller: _controller,
              child: Container(
                height: 1000,
                child: Center(
                  child: RaisedButton(
                    child: Text('Scroll to Position'),
                    onPressed: scrollToPosition,
      void main() {
          home: MyScrollableWidget(),

In the above example, a custom scrollable widget is created using SingleChildScrollView. It has a ScrollController attached to it. The scrollToPosition method is responsible for scrolling to the desired position, which in this case is 500 pixels. It uses the animateTo method of the ScrollController to smoothly scroll to the position over a duration of 500 milliseconds with the specified curve.

The ScrollController should be disposed of when the widget is no longer used, so it is done in the dispose method to avoid memory leaks.

The SingleChildScrollView widget is wrapped inside a Scaffold to provide the necessary material design structure. The RaisedButton within the container triggers the scrollToPosition method when pressed and initiates the scroll operation.

Leave a comment