【问题标题】:How to create a Loop to see how much you scrolled in Flutter?如何创建一个循环来查看你在 Flutter 中滚动了多少?
【发布时间】:2021-02-28 16:28:38
【问题描述】:

如何创建一个循环,使用ScrollPosition 显示以像素为单位的滚动量 假设用户在执行以下操作的屏幕上上下滚动,

假设视图为 10,000 像素

在屏幕的一半左右,回到顶部并进入大约 3/4 的视图。您可以使用ScrollPosition 创建一个循环以获取以像素为单位的总滚动距离,因此一个数字将显示为 5000+5000+7500 = 17500 像素。任何想法,将不胜感激。 代码:

body: TabBarView(
                children: [
                  
                  new ListView.separated(
                    separatorBuilder: (context, index) => Divider(),
                    itemCount: 10,
                    itemBuilder: (context, index) => ListTile(
                      leading: CircleAvatar(
                        backgroundImage: AssetImage("assets/images/avatar.jpg"),
                      ),
                      title: Text("List Index is $index"),
                    ),

                  ),
])

【问题讨论】:

    标签: android android-studio flutter dart counter


    【解决方案1】:

    你可以创建一个状态

    double totalScrolled = 0;
    

    在您的小部件定义中并将您的 ListView 包装在 NotificationListener<ScrollUpdateNotification>

    NotificationListener<ScrollUpdateNotification>(
      child: ListView(
      ),
      onNotification: (notif) { 
        totalScrolled +=notif.scrollDelta.abs();
     
      },
    ),
    

    然后只需将scrollDelta 的绝对值添加到您的totalScrolled

    【讨论】:

    • 我添加了我正在编写的代码,但我无法添加 NotificationListener,因为当我添加它时,它会作为一个新选项卡出现。如果我在 ListView 中创建一个容器,它会给我太多位置参数错误
    猜你喜欢
    • 2022-07-08
    • 2016-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-23
    • 1970-01-01
    • 2017-01-17
    相关资源
    最近更新 更多