selectFromListenable<U extends Listenable, T> static method
Widget
selectFromListenable<U extends Listenable, T>({
- Key? key,
- bool maintainHistory = false,
- required U listenable,
- required T selector(),
- int? getDepth(
- T value
- List<
ValueHistoryEntry< ? initialHistory,T> > - required AnimatedValueWidgetBuilder<
T> builder, - RouteTransitionsBuilder transitionsBuilder = defaultRouteTransitionsBuilder,
- Duration transitionDuration = _kDefaultTransitionDuration,
- required void onPop(
- T,
- T
- bool takeFocus = false,
- bool maintainState = true,
- bool opaque = true,
- 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,
);
},
);
}