【问题标题】:move singlechildscroll view back to top position after refresh in flutter在颤动中刷新后将singlechildscrollview移回顶部位置
【发布时间】:2020-11-05 19:45:36
【问题描述】:

每当我点击给定 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)
                ],),
                ],
                )

                ),
              ),
            ),
          ),
        ),

【问题讨论】:

    标签: flutter dart flutter-animation


    【解决方案1】:

    由于我不确定optionButton 的外观,您可能首先需要在构建方法上方使用它:

    ScrollController scrollController = new ScrollController();
    

    然后,如果您的 optionButton 属于同一类,请在您的按钮函数 onTap(){} or onPressed(){} 中调用它:

    scrollController.jumpTo(0.0);
    

    确保将控制器添加到SingleChildScrollView

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

    如果这有帮助,请告诉我!

    【讨论】:

    • 我认为这就是方式,我投票赞成,还有另一种方式可以将其设置为动画到顶部,您应该将其放入 ontap 或 onChanged 函数_scrollController.animateTo(0, duration: Duration(milliseconds: 500), curve: Curves.fastOutSlowIn);
    • 非常感谢,这两种方法都有效。非常感谢我也投了赞成票,但由于我的声望少于 15 个,所以我的投票现在不算数。
    猜你喜欢
    • 1970-01-01
    • 2018-12-27
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-21
    • 2021-12-18
    • 2021-05-30
    相关资源
    最近更新 更多