selectFromListenable<U extends Listenable, T> static method

Widget selectFromListenable<U extends Listenable, T>({
  1. Key? key,
  2. bool maintainHistory = false,
  3. required U listenable,
  4. required T selector(),
  5. int? getDepth(
    1. T value
    )?,
  6. List<ValueHistoryEntry<T>>? initialHistory,
  7. required AnimatedValueWidgetBuilder<T> builder,
  8. RouteTransitionsBuilder transitionsBuilder = defaultRouteTransitionsBuilder,
  9. Duration transitionDuration = _kDefaultTransitionDuration,
  10. required void onPop(
    1. T,
    2. T
    ),
  11. bool takeFocus = false,
  12. bool maintainState = true,
  13. bool opaque = true,
  14. int? popPriority,
})

Creates an ImplicitNavigator that pushes new pages when a value selected from a listenable changes.

A selector function is used to pick a value from listenable. If the listenable changes but selector returns the same value, no new pages are pushed.

For example, the following creates a navigator that pushes and pops pages as a tab controller changes tabs:

return ImplicitNavigator.selectFromListenable<ScrollController, double>(
  listenable: myTabController,
  selector: () => myTabController.index,
  builder (context, tabIndex, animation, secondaryAnimation) {
    return MyTabPage(index: tabIndex);
  },
  onPop: (poppedIndex, indexAfterPop) {
    myTabController.index = indexAfterPop;
  },
);

If non-null, getDepth will be called on each value and used to set ImplicitNavigator.depth. getDepth MUST return the same depth for a given value every time it's called on that value. If it returns inconsistent depths, ImplicitNavigator may push redundant pages and will not pop pages properly.

Implementation

static Widget selectFromListenable<U extends Listenable, T>({
  Key? key,
  bool maintainHistory = false,
  required U listenable,
  required T Function() selector,
  int? Function(T value)? getDepth,
  List<ValueHistoryEntry<T>>? initialHistory,
  required AnimatedValueWidgetBuilder<T> builder,
  RouteTransitionsBuilder transitionsBuilder = defaultRouteTransitionsBuilder,
  Duration transitionDuration = _kDefaultTransitionDuration,
  required void Function(T, T) onPop,
  bool takeFocus = false,
  bool maintainState = true,
  bool opaque = true,
  int? popPriority,
}) {
  // Animated builder is actually just a misnamed `ListenableBuilder`.
  return AnimatedBuilder(
    key: key,
    animation: listenable,
    builder: (context, child) {
      return ImplicitNavigator<T>(
        maintainHistory: maintainHistory,
        value: selector(),
        depth: getDepth?.call(selector()),
        initialHistory: initialHistory,
        builder: builder,
        transitionsBuilder: transitionsBuilder,
        transitionDuration: transitionDuration,
        onPop: onPop,
        takeFocus: takeFocus,
        maintainState: maintainState,
        opaque: opaque,
        popPriority: popPriority,
      );
    },
  );
}