pull_to_refresh的使用

1.pull_to_refresh

官方链接见https://pub-web.flutter-io.cn/packages/pull_to_refresh

支持Android/iOS/linux/MacOS/Web/Windows

2.pubspec.yaml添加依赖

1
  pull_to_refresh: ^2.0.0 

3.示例

  • pull_to_refresh_page.dart
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

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

  @override
  PullToRefreshWidgetState createState() => PullToRefreshWidgetState();
}

class PullToRefreshWidgetState extends State<PullToRefreshWidget> {
  RefreshController _refreshController =
  RefreshController(initialRefresh: false);
  List<String> _items = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"];
  int _count = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('支持下拉的列表'),
      ),
      body: SmartRefresher(
        enablePullDown: true,
        enablePullUp: true,
        header: const WaterDropHeader(
          complete: Text(
            '刷新完成',
            style: TextStyle(color: Colors.blue),
          ),
          failed: Text(
            '刷新失败',
            style: TextStyle(color: Colors.red),
          ),
        ),
        footer: CustomFooter(
          builder: (context, mode) {
            Widget body;

            if (mode == LoadStatus.idle) {
              body = const Text('卡拉刷新');
            } else if (mode == LoadStatus.loading) {
              body = const CupertinoActivityIndicator();
            } else if (mode == LoadStatus.failed) {
              body = const Text('加载失败,请重试');
            } else if (mode == LoadStatus.canLoading) {
              body = const Text('释放加载更多');
            } else {
              body = const Text('没有更多数据了');
            }
            debugPrint('mode:$mode');

            return SizedBox(
              height: 40,
              child: Center(
                child: body,
              ),
            );
          },
        ),
        controller: _refreshController,
        onRefresh: _onRefresh, // 下拉刷新
        onLoading: _onLoading, // 上拉加载更多
        child: ListView.builder(
            itemBuilder: (context, index) {
              return ListTile(title: Text('列表项 显示内容:${_items[index]} '));
            },
            itemCount: _items.length),
      ),
    );
  }

  void _onRefresh() async {
    await Future.delayed(const Duration(milliseconds: 500));

    _items.clear();

    for (int i = 0; i < 10; i++) {
      _items.add('${i + 1}');
    }

    ++_count;

    if (mounted) setState(() {});

    if (_count > 3) {
      _refreshController.refreshFailed();
      _count = 0;
    } else {
      _refreshController.refreshCompleted();
    }
  }

  void _onLoading() async {
    await Future.delayed(const Duration(milliseconds: 500));

    int count = _items.length + 1;

    for (int i = 0; i < 10; i++) {
      _items.add('${i + count}');
    }

    if (mounted) setState(() {});

    _refreshController.loadComplete();
  }
}

4.小结

  • 同类组件对比(2024年3月27日)
组件 LIKES PUB PINTS POPULARITY
pull_to_refresh 2472 120 99%
flutter_refresh 0 30 71%
easy_refresh 347 130 98%
smart_refresher 3 100 82%

建议优先pull_to_refresh;其它的如何选择,大家各任所爱,想尝试新的组件也可以体验下。

  • pull_to_refres组件
    • 提供了丰富的自定义选项,可以定制下拉刷新和上拉加载更多的样式。
    • 支持多种刷新指示器(indicator)以及自定义indicator的功能。 上述示例,仅仅作为展示。