0

I want to click on the button and jump to the next widget in my SingleChildScrollView but I have to pass height in the controller and I have a text widget and I don't know what is the height of text because there came from the server. so can anyone help me please how can I jump to the second container in my SingleChildScrollView?

here is my button I want to press :

 IconButton(
              highlightColor: Colors.transparent,
              splashColor: Colors.transparent,
              hoverColor: Colors.transparent,
              padding: EdgeInsets.zero,
              constraints: BoxConstraints(),
              onPressed: () {
                scrollController.animateTo(
                    500.0, // here
                    curve: Curves.easeInOut,
                    duration: Duration(milliseconds: 1800 ));
              },
              icon: SvgPicture.asset(
                MyIcons.message3,
              ),
            ),

it's my controller :

ScrollController scrollController = ScrollController();

here is my code :

 SingleChildScrollView(
 controller: scrollController,
 child: Column(
 children: [
   text(),
  Container()
 ]));

I just want to jump to text and receive to the container when I click on the button.

JideGuru
  • 7,102
  • 6
  • 26
  • 48
user17838882
  • 93
  • 2
  • 14

2 Answers2

0

mainAxisSize: MainAxisSize.max, Add this line in column

SingleChildScrollView(
controller: scrollController,
child: Column(
mainAxisSize: MainAxisSize.max,
children: [
text(),
Container()
]));
0

You can do this using keys.

Declare a unique key for your Container() widget.

final containerKey = GlobalKey();

Assign this key to your container:

Container(
   key: containerKey,
   ...
),

On the click of your button do the following:

scrollController.position.ensureVisible(
  containerKey.currentContext.findRenderObject(),
  alignment: 0.5, // How far into view (between 0 and 1).
  duration: const Duration(seconds: 1),
);

Make sure your scrollController is assigned to your SingleChildScrollView.

SingleChildScrollView(
    controller: scrollController,
    child: ...
)
Muhammad Hussain
  • 966
  • 4
  • 15