diff --git a/sheet/example/lib/examples/route/navigation/go_router_pop_scope.dart b/sheet/example/lib/examples/route/navigation/go_router_pop_scope.dart new file mode 100644 index 0000000..595c40b --- /dev/null +++ b/sheet/example/lib/examples/route/navigation/go_router_pop_scope.dart @@ -0,0 +1,101 @@ +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; +import 'package:go_router/go_router.dart'; +import 'package:sheet/route.dart'; + +class GoRouterPopScopeApp extends StatelessWidget { + @override + Widget build(BuildContext context) { + return MaterialApp.router( + title: 'PopScope Example', + routerConfig: _router, + theme: ThemeData.light(), + ); + } + + final GoRouter _router = GoRouter( + routes: [ + GoRoute( + path: '/', + builder: (_, __) => HomeScreen(), + routes: [ + GoRoute( + path: 'detail', + pageBuilder: (_, __) => SheetPage(child: DetailScreen()), + ), + ], + ), + ], + ); +} + +class HomeScreen extends StatelessWidget { + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: CupertinoNavigationBar( + leading: BackButton(onPressed: () => Navigator.of(context, rootNavigator: true).pop()), + middle: Text('Home') + ), + body: Center( + child: ElevatedButton( + onPressed: () => context.go('/detail'), + child: Text('Open Sheet'), + ), + ), + ); + } +} + +class DetailScreen extends StatefulWidget { + @override + State createState() => _DetailScreenState(); +} + +class _DetailScreenState extends State { + bool canPop = false; + + @override + Widget build(BuildContext context) { + return PopScope( + canPop: canPop, + onPopInvokedWithResult: (didPop, result) async { + if (didPop) return; + if (await _confirmExit(context)) Navigator.pop(context, result); + }, + child: Scaffold( + appBar: CupertinoNavigationBar(middle: Text('Detail')), + body: Center( + child: Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Text("canPop"), + Switch(value: canPop, onChanged: (value) => setState(() => canPop = value)), + ], + ), + ), + ), + ); + } + + Future _confirmExit(BuildContext context) async { + return await showCupertinoModalPopup( + context: context, + builder: (context) => CupertinoActionSheet( + title: Text('Should Close?'), + actions: [ + CupertinoActionSheetAction( + isDestructiveAction: true, + onPressed: () => Navigator.pop(context, true), + child: Text('OK'), + ), + CupertinoActionSheetAction( + onPressed: () => Navigator.pop(context, false), + child: Text('Cancel'), + ), + ], + ), + ) ?? + false; + } +} diff --git a/sheet/example/lib/route_example_page.dart b/sheet/example/lib/route_example_page.dart index 382e080..5b06336 100644 --- a/sheet/example/lib/route_example_page.dart +++ b/sheet/example/lib/route_example_page.dart @@ -20,6 +20,7 @@ import 'package:sheet/sheet.dart'; import 'examples/route/examples/modal_with_nested_scroll.dart'; import 'examples/route/navigation/go_router.dart'; import 'examples/route/navigation/go_router_advanced.dart'; +import 'examples/route/navigation/go_router_pop_scope.dart'; class RouteExamplePage extends StatelessWidget { const RouteExamplePage({super.key}); @@ -95,6 +96,16 @@ class RouteExamplePage extends StatelessWidget { ), ), ), + ListTile( + title: const Text('Go router - PopScope'), + onTap: () => Navigator.of(context).push( + MaterialExtendedPageRoute( + fullscreenDialog: true, + builder: (BuildContext context) => + GoRouterPopScopeApp(), + ), + ), + ), const SectionTitle('STYLES'), ListTile( title: const Text('Material fit'), diff --git a/sheet/lib/src/route/sheet_route.dart b/sheet/lib/src/route/sheet_route.dart index 83cd883..04c24e1 100644 --- a/sheet/lib/src/route/sheet_route.dart +++ b/sheet/lib/src/route/sheet_route.dart @@ -469,7 +469,8 @@ class __SheetRouteContainerState extends State<_SheetRouteContainer> ); if (route.popDisposition == RoutePopDisposition.doNotPop) { _sheetController.position.stopPreventingDrag(); - route.onPopInvoked(false); + _firstAnimation = true; + route.onPopInvokedWithResult(false, null); } else { route.willPop().then( (RoutePopDisposition disposition) {