Advertisement
Guest User

Untitled

a guest
Jun 29th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 1.75 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'dart:async';
  3.  
  4. class CustomRefreshIndicator extends StatefulWidget {
  5.   const CustomRefreshIndicator({
  6.     Key key,
  7.     this.child,
  8.     this.displacement: 40.0,
  9.     this.onRefresh,
  10.     this.color,
  11.     this.backgroundColor,
  12.     this.notificationPredicate: defaultScrollNotificationPredicate,
  13.     this.isFirstRefresh,
  14.   })  : assert(child != null),
  15.         assert(onRefresh != null),
  16.         assert(notificationPredicate != null),
  17.         super(key: key);
  18.  
  19.   final Widget child;
  20.   final double displacement;
  21.   final RefreshCallback onRefresh;
  22.   final Color color;
  23.   final Color backgroundColor;
  24.   final ScrollNotificationPredicate notificationPredicate;
  25.   final bool isFirstRefresh;
  26.  
  27.   @override
  28.   CustomRefreshIndicatorState createState() =>
  29.       new CustomRefreshIndicatorState();
  30. }
  31.  
  32. class CustomRefreshIndicatorState extends State<CustomRefreshIndicator> {
  33.   final GlobalKey<RefreshIndicatorState> _refreshKey =
  34.       new GlobalKey<RefreshIndicatorState>();
  35.  
  36.   @override
  37.   void initState() {
  38.     super.initState();
  39.     if (widget.isFirstRefresh) {
  40.       _initRefreshAsync();
  41.     }
  42.   }
  43.  
  44.   void _initRefreshAsync() async {
  45.     while (_refreshKey.currentState == null) {
  46.       await Future.delayed(const Duration(
  47.           milliseconds: 17)); //Wait about 1 frame, try every frame
  48.     }
  49.     _refreshKey.currentState.show();
  50.   }
  51.  
  52.   @override
  53.   Widget build(BuildContext context) {
  54.     return new RefreshIndicator(
  55.       key: _refreshKey,
  56.       child: widget.child,
  57.       displacement: widget.displacement,
  58.       onRefresh: widget.onRefresh,
  59.       color: widget.color,
  60.       backgroundColor: widget.backgroundColor,
  61.       notificationPredicate: widget.notificationPredicate,
  62.     );
  63.   }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement