flutter - 在颤动中刷新后将单子滚动视图移回顶部位置
问题描述
每当我点击给定 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)
],),
],
)
),
),
),
),
),
解决方案
由于我不确定是什么optionButton
样子,因此您可能首先需要在构建方法上方使用它:
ScrollController scrollController = new ScrollController();
然后,如果您optionButton
在同一类中,请在您的按钮功能中调用它onTap(){} or onPressed(){}
:
scrollController.jumpTo(0.0);
确保将控制器添加到SingleChildScrollView
如果optionButton
来自不同的班级,那么我将不得不改变我的答案。
让我知道这是否有帮助!
推荐阅读
- python - python的optparse
- angular - 未捕获(承诺):TypeError:无法读取未定义的属性“替换”
- python-3.x - 出现错误 Not Found 在服务器上找不到请求的 URL。如果您手动输入了 URL,请检查您的拼写并重试
- c++ - 如何为已定义的案例创建子类?
- php - 关于使用您的作曲家库建议的软件包
- firebase - Flutter - 尝试构建未来的上下文以检查用户是否已登录,但我的构建方法有错误
- java - 如何访问数组列表并使用 AND 条件验证值
- css - 拖动时从其先前位置隐藏拖动的元素
- c - 求和的递归函数
- laravel - 可以在工厂访问 $amount 吗?