I am attempting to have a list screen return am array of selected items to a prior screen. No problem in Android, but no luck in iOS. The right swipe does not call the onWillPop function that returns an array of selected items from the list.
I have created a CustomMaterialPageRoute:
class CustomMaterialPageRoute extends MaterialPageRoute { @protected bool get hasScopedWillPopCallback { return false; } CustomMaterialPageRoute({ required WidgetBuilder builder, required RouteSettings settings, bool maintainState = true, bool fullscreenDialog = false, }) : super( builder: builder, settings: settings, maintainState: maintainState, fullscreenDialog: fullscreenDialog, ); }
I call the list screen via:
void _getTurnpointsForTask() async { final result = await Navigator.pushNamed(context,TurnpointsForTask.routeName, arguments: TurnpointsSearchInAppBarScreen.TASK_TURNPOINT_OPTION,); if (result is List<Turnpoint>) { // result comes back null. BlocProvider.of<TaskBloc>(context).add(TurnpointsAddedToTaskEvent(result)); } }
The route code is:
if (settings.name == TurnpointsForTask.routeName) { final viewOption = settings.arguments as String; return CustomMaterialPageRoute( builder: (context) { return TurnpointsForTask(viewOption: viewOption); }, settings: settings, );
}
The list screen build starts with:
@override Widget build(BuildContext context) { return ConditionalWillPopScope( onWillPop: _onWillPop, shouldAddCallback: widget._hasChanges, //<_hasChanges is true child: Scaffold( key: _scaffoldKey, ...
The _onWillPop code which is called with Android back button, but not with iOS right swipe:
Future<bool> _onWillPop() async { if (widget.viewOption == TurnpointsSearchInAppBarScreen.TASK_TURNPOINT_OPTION) { ScaffoldMessenger.of(context).removeCurrentSnackBar(); Navigator.of(context).pop(widget.turnpointsForTask); <<< return array of items } else { Navigator.of(context).pop(); } return true; }
What am I missing?
Not sure this is the best solution, but it works.
@override
Widget build(BuildContext context) {
if (Platform.isAndroid) {
return ConditionalWillPopScope(
onWillPop: _onWillPop,
shouldAddCallback: true,
child: _buildScaffold(context),
);
} else {
//iOS
return GestureDetector(
behavior: HitTestBehavior.deferToChild, // if you have lower level drag/drop widgets
onHorizontalDragUpdate: (details) {
if (details.delta.direction >= 0) {
_onWillPop();
}
},
child: _buildScaffold(context),
);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.