Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. @immutable
  2. class Reachable extends StatefulWidget {
  3. final Widget child;
  4. final int index;
  5.  
  6. final ValueChanged<bool> onFocusChanged;
  7. final VoidCallback onSelect;
  8.  
  9. Reachable({
  10. @required this.child,
  11. @required this.index,
  12. this.onFocusChanged,
  13. this.onSelect,
  14. });
  15.  
  16. @override
  17. ReachableState createState() => ReachableState();
  18. }
  19.  
  20. class ReachableState extends State<Reachable> {
  21. StreamSubscription<int> _focusSubscription;
  22. StreamSubscription<int> _selectionSubscription;
  23.  
  24. @override
  25. Widget build(BuildContext context) => widget.child;
  26.  
  27. @override
  28. void didChangeDependencies() {
  29. _focusSubscription?.cancel();
  30. _selectionSubscription?.cancel();
  31.  
  32. _focusSubscription =
  33. PullToReachScope.of(context).focusIndex.listen(_onFocusChanged);
  34.  
  35. _selectionSubscription =
  36. PullToReachScope.of(context).selectIndex.listen(_onSelectionChanged);
  37.  
  38. super.didChangeDependencies();
  39. }
  40.  
  41. @override
  42. void dispose() {
  43. _focusSubscription?.cancel();
  44. _selectionSubscription?.cancel();
  45. super.dispose();
  46. }
  47.  
  48. // -----
  49. // Handle Notifications
  50. // -----
  51.  
  52. void _onFocusChanged(int newIndex) {
  53. if (widget.onFocusChanged == null) return;
  54. widget.onFocusChanged(newIndex == widget.index);
  55. }
  56.  
  57. void _onSelectionChanged(int newIndex) {
  58. if (widget.onSelect == null) return;
  59. if (newIndex == widget.index) widget.onSelect();
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement