颤音:如何避免列表视图以动态滚动(或更改其物理)



我有一个listView窗口小部件,我想允许它滚动或基于某些逻辑。

NeverScrollables Crollphysics可防止滚动,但是由于物理参数是最终参数,所以我之后无法更改。

我想使用状态使用不同的物理来重建listView,但我想是一个非常繁重的操作,可以重建完全的listView。

有人知道或如何处理这种情况,在其他用户操作完成之前,用户不应滚动listView?

您可以在listView中有条件地应用物理:

shrinkWrap: true,
physics: !isScrolable? const NeverScrollableScrollPhysics(): 
         const AlwaysScrollableScrollPhysics(),

然后,当您需要时,您可以更改修改变量值的状态。


setState(() {
    isScrolable = !isScrolable;
}); 

更改physics并使用setState应该可以解决问题,如果您不想使用它,则可以使用Stack小部件,然后将Container放在ListView上方,以避免交互,请检查此问题我制作的样本:

  class _MySampleWidgetState extends State<MySampleWidget> {
    bool scrollEnabled = true;
    @override
    Widget build(BuildContext context) {
      return Column(
        children: [
          Expanded(
            child: Center(
              child: RaisedButton(
                onPressed: () {
                  setState(() {
                    scrollEnabled = !scrollEnabled;
                  });
                },
                child: Text("Update"),
              ),
            ),
          ),
          Expanded(
            child: Stack(
              children: [
                ListView.builder(
                  shrinkWrap: true,
                  itemBuilder: (_, index) => ListTile(
                        title: Text("index: $index"),
                      ),
                ),
                if (!scrollEnabled)
                  Container(
                    color: Colors.transparent,
                  ),
              ],
            ),
          ),
        ],
      );
    }
  }

最新更新