首页 > 解决方案 > 在颤动中刷新后将单子滚动视图移回顶部位置

问题描述

每当我点击给定 4 个按钮的任何选项时,我的新数据都会加载到文本和 4 个选项字段中。但是滚动视图并没有回到它的正常位置,而是保持向下,在我之前离开它的位置。那么如何在每次刷新后让 singlechildscrollview 恢复到原来的形式呢?

 Expanded(
          child: Container(
            width: MediaQuery.of(context).size.width,
            margin:EdgeInsets.only(top: 50.0,bottom: 50) ,
            decoration: BoxDecoration(
                color: Color(0xFFf2f2f2),
                borderRadius: BorderRadius.only(topRight:  Radius.circular(40),topLeft:Radius.circular(40) ),

            ),
            child: SingleChildScrollView(
              
              scrollDirection: Axis.vertical,
              child: Container(
                height: 800,
                child: (
                Column(

                    crossAxisAlignment: CrossAxisAlignment.center,

                children: [
                  Padding(
                    padding: const EdgeInsets.only(top: 50,left: 20,right: 20),
                    child: Text(question.replaceAll("\\n", "\n"),style: TextStyle(fontWeight: FontWeight.bold,fontSize: 18),),
                  ),

                  SizedBox(height: 20),
                  optionButton(text: option1),
                  SizedBox(height: 30),
                  optionButton(text: option2),
                  SizedBox(height: 30),
                  optionButton(text: option1),
                  SizedBox(height: 30),
                  optionButton(text: option2),
                  SizedBox(height: 30),
                  Spacer(),




                Column(
                  children: [
                  Text("TOTAL"),
                    Container(
                        width: MediaQuery. of(context). size. width,
                        color: Colors.blue,
                        child:
                        Center(child: Text(score.toString(),)),),
SizedBox(height: 20)
                ],),
                ],
                )

                ),
              ),
            ),
          ),
        ),

标签: flutterdartflutter-animation

解决方案


由于我不确定是什么optionButton样子,因此您可能首先需要在构建方法上方使用它:

ScrollController scrollController = new ScrollController();

然后,如果您optionButton在同一类中,请在您的按钮功能中调用它onTap(){} or onPressed(){}

scrollController.jumpTo(0.0);

确保将控制器添加到SingleChildScrollView

如果optionButton来自不同的班级,那么我将不得不改变我的答案。

让我知道这是否有帮助!


推荐阅读