在 flutter 中刷新后将 singlechildscroll view 移回顶部位置
move singlechildscroll view back to top position after refresh in 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
在同一个 class 中,请在您的按钮函数中调用此 onTap(){} or onPressed(){}
:
scrollController.jumpTo(0.0);
确保将控制器添加到 SingleChildScrollView
如果 optionButton
来自不同的 class,那么我将不得不更改我的答案。
如果有帮助请告诉我!
每当我点击给定 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
在同一个 class 中,请在您的按钮函数中调用此 onTap(){} or onPressed(){}
:
scrollController.jumpTo(0.0);
确保将控制器添加到 SingleChildScrollView
如果 optionButton
来自不同的 class,那么我将不得不更改我的答案。
如果有帮助请告诉我!