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

CustomScrollView 设置centerKey后第二次下拉不会刷新 #581

Closed
xiaohucode opened this issue Jul 31, 2022 · 2 comments
Closed

CustomScrollView 设置centerKey后第二次下拉不会刷新 #581

xiaohucode opened this issue Jul 31, 2022 · 2 comments

Comments

@xiaohucode
Copy link

感谢大佬的插件,我有需求需要实现下拉加载上一章数据与上滑加载下一章数据
使用CustomScrollView 嵌套由于设置了centerKey 导致刷新加载一章后无法继续刷新加载

实现代码:

import 'dart:async';

import 'package:easy_refresh/easy_refresh.dart';
import 'package:floor/floor.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

class CustomScrollDelegate extends SliverChildBuilderDelegate {
  Function(int firstIndex, int lastIndex, double leadingScrollOffset,
      double trailingScrollOffset) scrollCallBack;
  Function(int firstIndex, int lastIndex) layoutFinishCallBack;

  int Function(Key key) findChildIndexCallback;

  CustomScrollDelegate(NullableIndexedWidgetBuilder builder,
      {int itemCount,
      this.scrollCallBack,
      this.findChildIndexCallback,
      this.layoutFinishCallBack})
      : super(builder,
            childCount: itemCount,
            findChildIndexCallback: findChildIndexCallback);
  @override
  void didFinishLayout(int firstIndex, int lastIndex) {
    super.didFinishLayout(firstIndex, lastIndex);
    if (layoutFinishCallBack != null) {
      layoutFinishCallBack(firstIndex, lastIndex);
    }
  }

  @override
  double estimateMaxScrollOffset(int firstIndex, int lastIndex,
      double leadingScrollOffset, double trailingScrollOffset) {
    if (scrollCallBack != null) {
      scrollCallBack(
          firstIndex, lastIndex, leadingScrollOffset, trailingScrollOffset);
    }
    return super.estimateMaxScrollOffset(
        firstIndex, lastIndex, leadingScrollOffset, trailingScrollOffset);
  }
}

class MangaList {
  List<String> urls;
  String name;
  MangaList({List<String> this.urls, String this.name});
}

class testListView extends StatefulWidget {
  const testListView({Key key}) : super(key: key);

  @override
  State<testListView> createState() => _testListViewState();
}

class _testListViewState extends State<testListView> {
  List<MangaList> list = [
    MangaList(
        name: "章节0",
        urls: List.generate(20, (index) => "item:${index}").toList())
  ];

  List<MangaList> prev_list = [

  ];

  ScrollController _controller = ScrollController();
  StreamController<int> _streamController = StreamController.broadcast();

  final _easyRefreshController = EasyRefreshController(
      controlFinishRefresh: true, controlFinishLoad: true);

  @override
  void dispose() {
    _streamController.close();
    super.dispose();
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final centerKey = ValueKey<String>("bottom-sliver-list");

    return Scaffold(
      appBar: AppBar(
        title: Text("view"),
      ),
      body: Stack(
        children: [
          EasyRefresh(
            controller: _easyRefreshController,
            header: CupertinoHeader(),

            // onLoad: () {
            //   _easyRefreshController.finishLoad();
            // },
            onRefresh: () async {
              await Future.delayed(Duration(seconds: 2));
              _easyRefreshController.finishRefresh();
              _easyRefreshController.resetHeader();
              prev_list.add(MangaList(
                  name: "章节${prev_list.length + 1}",
                  urls:
                      List.generate(20, (index) => "item:${index}").toList()));
              setState(() {});
            },
            child: CustomScrollView(
              center: centerKey,
              controller: _controller,
              // reverse: true,
              // shrinkWrap: true,

              cacheExtent: 0.0,
              clipBehavior: Clip.none,
              slivers: [
                SliverList(
                  delegate: CustomScrollDelegate(
                    (ctx, i) {
                      return Column(
                        mainAxisSize: MainAxisSize.min,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: prev_list[i]
                            .urls
                            .map(
                              (m) => Container(
                                alignment: Alignment.center,
                                decoration: BoxDecoration(
                                  border: Border.all(
                                      color: Colors.black, width: 0.2),
                                  color: Colors.blue,
                                ),
                                width: double.infinity,
                                height: 100,
                                child: Text("${prev_list[i].name} ${m}"),
                              ),
                            )
                            .toList(),
                      );
                    },
                    itemCount: prev_list.length,
                  ),
                ),
                SliverList(
                  key: centerKey,
                  delegate: CustomScrollDelegate(
                    (ctx, i) {
                      return Column(
                        mainAxisSize: MainAxisSize.min,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: list[i]
                            .urls
                            .map(
                              (m) => Container(
                                alignment: Alignment.center,
                                decoration: BoxDecoration(
                                  border: Border.all(
                                      color: Colors.black, width: 0.2),
                                  color: Colors.blue,
                                ),
                                width: double.infinity,
                                height: 100,
                                child: Text("${list[i].name} ${m}"),
                              ),
                            )
                            .toList(),
                      );
                    },
                    itemCount: list.length,
                  ),
                ),
              ],
            ),
          ),

          Positioned(
            bottom: 200,
            right: 10,
            child: Container(
              height: 50,
              width: 50,
              child: StreamBuilder<int>(
                stream: _streamController.stream,
                initialData: 0,
                builder: (context, snapshot) {
                  return TextButton(
                    onPressed: () {},
                    child: Text("${snapshot.data}"),
                  );
                },
              ),
              decoration: BoxDecoration(
                color: Colors.black,
                borderRadius: BorderRadius.all(
                  Radius.circular(50),
                ),
              ),
            ),
          ),
          Positioned(
            bottom: 10,
            right: 10,
            child: Container(
              height: 50,
              width: 50,
              child: TextButton(
                onPressed: () {
                  final mangaList = MangaList(
                      name: "章节${list.length + 1}",
                      urls: List.generate(20, (index) => "item:${100 + index}")
                          .toList());
                  list.insert(list.length, mangaList);
                  setState(() {});
                },
                child: Text("加入"),
              ),
              decoration: BoxDecoration(
                color: Colors.black,
                borderRadius: BorderRadius.all(
                  Radius.circular(50),
                ),
              ),
            ),
          ),
          Positioned(
            bottom: 100,
            right: 10,
            child: Container(
              height: 50,
              width: 50,
              child: TextButton(
                onPressed: () {
                  list.clear();
                  list.add(MangaList(
                      name: "章节1",
                      urls: List.generate(20, (index) => "item:${index}")
                          .toList()));
                  setState(() {});
                },
                child: Text("重置"),
              ),
              decoration: BoxDecoration(
                color: Colors.black,
                borderRadius: BorderRadius.all(
                  Radius.circular(50),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
@xuelongqy
Copy link
Owner

目前还不支持center,这个还得研究一下。有兴趣可以加群一起讨论

@xuelongqy
Copy link
Owner

v3.0.4已修复

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

No branches or pull requests

2 participants