在flutter中调用setState时如何禁用SingleChildScrollView上的滚动回顶部 [英] How to disable scrolling back to top on a SingleChildScrollView when calling setState in flutter

查看:600
本文介绍了在flutter中调用setState时如何禁用SingleChildScrollView上的滚动回顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SingleChildScrollView小部件,其子级是Flutter中的Form小部件.每当我对表单中的单选按钮或开关进行一些更改并调用setState()方法时,视图就会滚动到顶部. 如何避免这种行为?

I have a SingleChildScrollView widget whose child is a Form widget in Flutter. Whenever I made some changes to the radio buttons or switches inside the form and calling setState() method, the view scrolls to the top. How can I avoid this behavior?

推荐答案

我建议在构造SingleChildScrollView小部件时通过自定义ScrollController.

I would recommend passing through a custom ScrollController when constructing your SingleChildScrollView widget.

ScrollController具有一个名为"keepScrollOffset"的属性,您需要将其设置为true.

ScrollController has a property called 'keepScrollOffset' which you need to set to true.

摘自文档,网址为 https://docs.flutter.io/flutter /widgets/ScrollController-class.html

keepScrollOffset→布尔 每次滚动完成时,请使用PageStorage保存当前滚动偏移量,如果重新创建了此控制器的可滚动内容,则将其恢复. [...] 最终

keepScrollOffset → bool Each time a scroll completes, save the current scroll offset with PageStorage and restore it if this controller's scrollable is recreated. [...] final

这篇关于在flutter中调用setState时如何禁用SingleChildScrollView上的滚动回顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆