Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions sheet/example/lib/examples/route/navigation/go_router_pop_scope.dart
Original file line number Diff line number Diff line change
@@ -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<DetailScreen> createState() => _DetailScreenState();
}

class _DetailScreenState extends State<DetailScreen> {
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<bool> _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;
}
}
11 changes: 11 additions & 0 deletions sheet/example/lib/route_example_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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});
Expand Down Expand Up @@ -95,6 +96,16 @@ class RouteExamplePage extends StatelessWidget {
),
),
),
ListTile(
title: const Text('Go router - PopScope'),
onTap: () => Navigator.of(context).push(
MaterialExtendedPageRoute<void>(
fullscreenDialog: true,
builder: (BuildContext context) =>
GoRouterPopScopeApp(),
),
),
),
const SectionTitle('STYLES'),
ListTile(
title: const Text('Material fit'),
Expand Down
3 changes: 2 additions & 1 deletion sheet/lib/src/route/sheet_route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading