switchLayoutBuilderWithSwitchedFlag function
A switch layout builder for AnimatedSwitcher, which is almost the same as AnimatedSwitcher.defaultLayoutBuilder, but this builder wraps PreviouslySwitchedWidget with tracked key to specific widgets in Stack.
Note that you should use this layout builder when you are using any kinds of UpdatableDataView,
in order to invalidate the ScrollController for switched out Scrollable and Scrollbar, and
deal with The provided ScrollController is currently attached to more than one ScrollPosition
kinds of exceptions.
Implementation
Widget switchLayoutBuilderWithSwitchedFlag(Widget? currentChild, List<Widget> previousChildren) {
return Stack(
alignment: Alignment.center,
children: [
// previous widgets
for (var child in previousChildren) //
child is! KeyedSubtree
? child // unreachable, child is almost a KeyedSubtree
: KeyedSubtree(
key: child.key,
child: PreviouslySwitchedWidget(
key: child.key, // <<< must set the key to track widget correctly
child: child.child,
switched: true,
),
),
// current widget
if (currentChild != null)
currentChild is! KeyedSubtree
? currentChild // unreachable, currentChild is almost a KeyedSubtree
: KeyedSubtree(
key: currentChild.key,
child: PreviouslySwitchedWidget(
key: currentChild.key, // <<< must set the key to track widget correctly
child: currentChild.child,
switched: false,
),
),
],
);
}