import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class PullRefreshWidget extends StatefulWidget {
  final Widget child;
  const PullRefreshWidget({super.key, required this.child});

  @override
  State<PullRefreshWidget> createState() => _PullRefreshWidgetState();
}

class _PullRefreshWidgetState extends State<PullRefreshWidget> {
  final RefreshController refreshController = RefreshController(initialRefresh: false);

  void onRefresh() async {
    await Future.delayed(Duration(milliseconds: 1000));
    refreshController.refreshCompleted();
  }

  void onLoading() async {
    await Future.delayed(Duration(milliseconds: 1000));
    if (mounted) setState(() {});
    refreshController.loadComplete();
  }

  @override
  Widget build(BuildContext context) {
    return SmartRefresher(
        controller: refreshController,
        enablePullDown: true,
        onRefresh: onRefresh,
        onLoading: onLoading,
        child: widget.child);
  }
}
