Skip to content

Instantly share code, notes, and snippets.

@slightfoot
Forked from dumazy/main.dart
Last active October 23, 2024 18:30
Show Gist options
  • Save slightfoot/0bc4b3546d53bb2fae04ef4942defdca to your computer and use it in GitHub Desktop.
Save slightfoot/0bc4b3546d53bb2fae04ef4942defdca to your computer and use it in GitHub Desktop.
Updating PopScope based on inner Navigator
// MIT License
//
// Copyright (c) 2022 Fré Dumazy & Simon Lightfoot
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Builder(
builder: (context) {
return Scaffold(
appBar: AppBar(title: const Text('Home')),
body: Center(
child: ElevatedButton(
onPressed: () => Navigator.of(context).push(
MaterialPageRoute(
builder: (_) => const NestedNavScreen(),
),
),
child: const Text('Open top level'),
),
),
);
},
),
);
}
}
class NestedNavScreen extends StatefulWidget {
const NestedNavScreen({super.key});
static void navigateToIndex(BuildContext context, int index) {
context
.findAncestorStateOfType<_NestedNavScreenState>()!
.navigateToIndex(index);
}
static void pop(BuildContext context) {
context.findAncestorStateOfType<_NestedNavScreenState>()!.pop();
}
@override
State<NestedNavScreen> createState() => _NestedNavScreenState();
}
class _NestedNavScreenState extends State<NestedNavScreen> {
var _pages = <NestedPage>[
NestedPage(index: 1),
];
void navigateToIndex(int index) {
setState(() {
_pages = List.of(_pages) //
..add(NestedPage(index: index));
});
}
void pop() {
setState(() {
_pages = List.of(_pages) //
..removeLast();
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Nested Nav screen')),
body: PopScope(
canPop: _pages.length == 1,
onPopInvokedWithResult: (didPop, _) {
if (didPop) {
return;
} else {
pop();
}
},
child: Navigator(
onDidRemovePage: (page) {
_pages.remove(page);
},
pages: _pages,
),
),
);
}
}
class NestedPage extends MaterialPage {
const NestedPage._({
required this.index,
required super.name,
required super.child,
});
factory NestedPage({
required int index,
}) {
return NestedPage._(
index: index,
name: '$index',
child: NestedScreen(index: index),
);
}
final int index;
}
class NestedScreen extends StatelessWidget {
const NestedScreen({super.key, required this.index});
final int index;
@override
Widget build(BuildContext context) {
return Center(
child: Column(
children: [
Text('Nested page: $index'),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
if (Navigator.of(context).canPop())
ElevatedButton(
onPressed: () {
NestedNavScreen.pop(context);
},
child: const Text('Go back'),
),
ElevatedButton(
onPressed: () {
NestedNavScreen.navigateToIndex(context, index + 1);
},
child: const Text('Open next'),
),
],
),
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment