Skip to content

Instantly share code, notes, and snippets.

@JulianBissekkou
Created February 17, 2019 19:05
Show Gist options
  • Save JulianBissekkou/b8ca603ad0b3c8735285640356d0c56f to your computer and use it in GitHub Desktop.
Save JulianBissekkou/b8ca603ad0b3c8735285640356d0c56f to your computer and use it in GitHub Desktop.
@immutable
class Reachable extends StatefulWidget {
final Widget child;
final int index;
final ValueChanged<bool> onFocusChanged;
final VoidCallback onSelect;
Reachable({
@required this.child,
@required this.index,
this.onFocusChanged,
this.onSelect,
});
@override
ReachableState createState() => ReachableState();
}
class ReachableState extends State<Reachable> {
StreamSubscription<int> _focusSubscription;
StreamSubscription<int> _selectionSubscription;
@override
Widget build(BuildContext context) => widget.child;
@override
void didChangeDependencies() {
_focusSubscription?.cancel();
_selectionSubscription?.cancel();
_focusSubscription =
PullToReachScope.of(context).focusIndex.listen(_onFocusChanged);
_selectionSubscription =
PullToReachScope.of(context).selectIndex.listen(_onSelectionChanged);
super.didChangeDependencies();
}
@override
void dispose() {
_focusSubscription?.cancel();
_selectionSubscription?.cancel();
super.dispose();
}
// -----
// Handle Notifications
// -----
void _onFocusChanged(int newIndex) {
if (widget.onFocusChanged == null) return;
widget.onFocusChanged(newIndex == widget.index);
}
void _onSelectionChanged(int newIndex) {
if (widget.onSelect == null) return;
if (newIndex == widget.index) widget.onSelect();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment