robert-luoqing / flutter_list_view

MIT License
47 stars 17 forks source link

boundcing scroll offset #4

Closed yyong37 closed 2 years ago

yyong37 commented 2 years ago

预期: 下拉回弹会定位到顶部第一个元素. 但是现在会回弹至第2,3个元素才停止

expect: bouncing and stop edge first item

----screen top edge-----
------  <-- expect stop position
-------
------- <-- current version stop scroll offset
-------
------- 

class BingoPage extends StatefulWidget {
  @override
  State<BingoPage> createState() => _BingoPageState();
}

class _BingoPageState extends State<BingoPage> {
  final logic = Get.find<BingoLogic>();

  final data = <String>[];

  @override
  void initState() {
    // mock chat list insert data
    Timer.periodic(Duration(seconds: 1), (timer) {
      setState(() {
        data.insert(0, DateTime.now().toString());
      });
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: FlutterListView(
            reverse: true,
            delegate: FlutterListViewDelegate(
              (BuildContext context, int index) => ListTile(title: Text('List Item ${data[index]}')),
              childCount: data.length,
              keepPosition: true,
            )));
  }
}
robert-bitguild commented 2 years ago

@rizzi37 I can't reproduce the issue that you mentioned. Please copy/paste the whole FlutterListView element to me to verify it.

  1. Did you have set reverse to true,
  2. Did you have wrap SmartRefresher or other refresh element on it.
yyong37 commented 2 years ago

@robert-bitguild thanks for your fast reply, I have updated my origin issue with the same code.

robert-bitguild commented 2 years ago

@rizzi37 我认为这个是正常行为,我对比了ListView的官方源码(ListView也有这个问题),这是因为你在下拉过程中,Timer.periodic(Duration(seconds: 1), (timer) 里创建了新的项而导致滚动后退了。现实情况下,这种情况一般不会考虑,即下拉过程中有UI刷新。

另外,keepPosition: true时最好实现 onItemKey: (index) ,否测keepPosition不会成效。因为我不知道你insert的行在那里。onItemKey要返加该行的唯一值。

  return Scaffold(
        body: FlutterListView(
            reverse: true,
            delegate: FlutterListViewDelegate(
              (BuildContext context, int index) =>
                  ListTile(title: Text('List Item ${data[index]}')),
              childCount: data.length,
              keepPosition: true,
              onItemKey: (index) {
                return data[index];
              },
            )));
yyong37 commented 2 years ago

收到.再次表示感谢.