Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Feb 17, 2023
Merged

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

merged 1 commit into from
Feb 17, 2023

Conversation

LinXunFeng
Copy link
Contributor

@LinXunFeng LinXunFeng commented Feb 16, 2023

修复前的复现代码如下:

  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
Copy link
Owner

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

@xuelongqy xuelongqy merged commit 1650fb8 into xuelongqy:v3 Feb 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants