xuelongqy / flutter_easy_refresh

A flutter widget that provides pull-down refresh and pull-up load.
https://xuelongqy.github.io/flutter_easy_refresh/
MIT License
3.9k stars 635 forks source link

fix: Avoid an endless loop caused by multiple ListViews nested. #681

Closed LinXunFeng closed 1 year ago

LinXunFeng commented 1 year ago

修复前的复现代码如下:

  Simulation? createBallisticSimulation(
      ScrollMetrics position, double velocity) {
    // 该方法在点击FloatingActionButton进行页面刷新后会不断地被调用
    print("position.maxScrollExtent -- ${position.maxScrollExtent}");
    ...
}
import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';

class Test1Page extends StatefulWidget {
  const Test1Page({super.key});

  @override
  State<Test1Page> createState() => _Test1PageState();
}

class _Test1PageState extends State<Test1Page> {
  List<int> sectionItemCountList = [3, 4, 3, 4];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('test'),
      ),
      body: _buildBody(),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.refresh),
        onPressed: () {
          sectionItemCountList[2] = sectionItemCountList[2] + 1;
          setState(() {});
        },
      ),
    );
  }

  Widget _buildBody() {
    Widget resultWidget = CustomScrollView(slivers: [
      SliverList(
        delegate: SliverChildBuilderDelegate(
          (context, index) {
            return Column(
              children: [
                ListView.builder(
                  physics: NeverScrollableScrollPhysics(),
                  shrinkWrap: true,
                  itemBuilder: (context, index) {
                    return Container(
                      margin: EdgeInsets.all(8),
                      height: 80,
                      color: Colors.yellow,
                      child: Text("$index"),
                    );
                  },
                  itemCount: sectionItemCountList[index],
                ),
              ],
            );
          },
          childCount: sectionItemCountList.length,
        ),
      ),
    ]);

    resultWidget = EasyRefresh(
      onRefresh: () async {
        return Future.delayed(Duration(seconds: 1));
      },
      onLoad: () async {
        return Future.delayed(Duration(seconds: 1));
      },
      child: resultWidget,
    );
    return resultWidget;
  }
}

点击右下角的 FloatingActionButton 后,控制台会不停的输出:

...循环输出一样的内容
flutter: position.maxScrollExtent -- 0.0
flutter: position.maxScrollExtent -- 727.0
flutter: position.maxScrollExtent -- 0.0
flutter: position.maxScrollExtent -- 727.0
flutter: position.maxScrollExtent -- 0.0
flutter: position.maxScrollExtent -- 727.0
flutter: position.maxScrollExtent -- 0.0
flutter: position.maxScrollExtent -- 727.0
...循环输出一样的内容
xuelongqy commented 1 year ago

非常感谢!我会合并这个请求,但我觉得应该把判断条件放在更合适的地方,我会对其进行调整,将会在下一个版本发布