Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to scroll a SingleChildScrollView to a certain position?

I have an app where when I press a TypeAheadFormField, I want the page to scroll down a little bit, for the user to see the suggestions as long as the keyboard. I watched stuff on internet and I can not figure it out. I get the error "ScrollController not attached to any scroll views".

Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: _onWillPop,
      child: Scaffold(
        bottomNavigationBar: BottomAppBar(
        color: Colors.teal[200],
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              const Spacer(),
              IconButton(
                tooltip: 'Home',
                splashColor: Colors.lightBlueAccent,
                icon: const Icon(Icons.home),
                iconSize: 25,
                onPressed: () {
                  Navigator.pop(context);
                },
              ),
              const Spacer(),
            ],
          ),
        ),
        body: buildScroll(),
        backgroundColor: Colors.white,
      ),
    );

  }

Here above I am using the buildScroll widget.

Widget buildGame() {

    var myController = TextEditingController();

    @override
    void dispose() {
      myController.dispose();
      super.dispose();
    }

    int nextNumber({required int min, required int max}) =>
        min + Random().nextInt(max - min + 1);

    int rowNumber = Random().nextInt(5);
    int columnNumber = nextNumber(min: 1, max: 10);
    int columnNumberHints = nextNumber(min: 1, max: 10);
    final String language = languageMatrix[rowNumber][0];
    final sentence = languageMatrix[rowNumber][columnNumber];
    final String languageLowerCases = language.toLowerCase();

    scrollController.animateTo(
        scrollController.position.pixels,
        duration: const Duration(milliseconds: 200),
        curve: Curves.easeInOut
    );

    return Column(
         ---stuff here
    )
Widget buildScroll () {
    return SingleChildScrollView(
      controller: scrollController,
      child: buildGame(),
    );
  }

And here I am putting all together. But as I said, I am getting that error. What should I change?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try the following code:

if (scrollController.hasClients) {
  scrollController.animateTo(
    scrollController.position.pixels,
    duration: const Duration(milliseconds: 200),
    curve: Curves.easeInOut
  );
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading