Line data Source code
1 : import 'dart:async'; 2 : 3 : import 'package:flutter/material.dart'; 4 : import 'package:flutter/widgets.dart'; 5 : 6 : /// A widget that wraps a [widget] with a [RefreshIndicator] designed to 7 : /// be used with BLoC pattern. 8 : /// 9 : /// When swipe to refresh gesture is detected a [onRefresh] callback is 10 : /// executed. The indicator remains visible until the widget is rebuilt. 11 : class RefreshView extends StatefulWidget { 12 : final Widget child; 13 : final VoidCallback onRefresh; 14 : final double displacement; 15 : final Color color; 16 : final Color backgroundColor; 17 : final ScrollNotificationPredicate notificationPredicate; 18 : final String semanticsLabel; 19 : final String semanticsValue; 20 : 21 0 : const RefreshView({ 22 : Key key, 23 : @required this.child, 24 : this.onRefresh, 25 : this.backgroundColor, 26 : this.color, 27 : this.displacement = 40.0, 28 : this.notificationPredicate = defaultScrollNotificationPredicate, 29 : this.semanticsLabel, 30 : this.semanticsValue, 31 0 : }) : assert(child != null), 32 0 : assert(displacement != null), 33 0 : assert(notificationPredicate != null), 34 0 : super(key: key); 35 : 36 0 : @override 37 0 : _RefreshViewState createState() => _RefreshViewState(); 38 : } 39 : 40 : class _RefreshViewState extends State<RefreshView> { 41 : Completer<void> _refreshCompleter = Completer(); 42 : 43 0 : @override 44 : Widget build(BuildContext context) { 45 0 : _refreshCompleter?.complete(); 46 0 : _refreshCompleter = Completer(); 47 : 48 0 : return RefreshIndicator( 49 0 : child: widget.child, 50 0 : onRefresh: _refresh, 51 0 : backgroundColor: widget.backgroundColor, 52 0 : color: widget.color, 53 0 : displacement: widget.displacement, 54 0 : notificationPredicate: widget.notificationPredicate, 55 0 : semanticsLabel: widget.semanticsLabel, 56 0 : semanticsValue: widget.semanticsValue, 57 : ); 58 : } 59 : 60 0 : Future<void> _refresh() { 61 0 : widget.onRefresh?.call(); 62 0 : return _refreshCompleter.future; 63 : } 64 : }