Created
February 4, 2024 01:01
-
-
Save tomalabaster/d7c8060511397e84f6b76718fa2a973b to your computer and use it in GitHub Desktop.
PopScope.onPopInvokedBehaviour
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file | |
// for details. All rights reserved. Use of this source code is governed by a | |
// BSD-style license that can be found in the LICENSE file. | |
import 'package:flutter/material.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
debugShowCheckedModeBanner: false, | |
theme: ThemeData( | |
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), | |
useMaterial3: true, | |
), | |
home: MyHomePage(), | |
); | |
} | |
} | |
class MyHomePage extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(title: const Text('PopScope Example')), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
TextButton( | |
onPressed: () => Navigator.of(context).push(MaterialPageRoute(builder: (context) => CanPopTrue())), | |
child: const Text('Page with canPop true'), | |
), | |
TextButton( | |
onPressed: () => Navigator.of(context).push(MaterialPageRoute(builder: (context) => CanPopFalse())), | |
child: const Text('Page with canPop false'), | |
), | |
TextButton( | |
onPressed: () => Navigator.of(context).push(MaterialPageRoute(builder: (context) => CanPopFalseButAllowedNotCatchingSecondaryCall())), | |
child: const Text('Page with canPop false but allowed'), | |
), | |
TextButton( | |
onPressed: () => Navigator.of(context).push(MaterialPageRoute(builder: (context) => CanPopFalseButAllowedCatchingSecondaryCall())), | |
child: const Text('Page with canPop false but allowed'), | |
), | |
], | |
), | |
), | |
); | |
} | |
} | |
class CanPopTrue extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return PopScope( | |
canPop: true, | |
onPopInvoked: (didPop) { | |
print('$runtimeType didPop: $didPop'); | |
}, | |
child: Scaffold( | |
appBar: AppBar(title: const Text('Can Pop True')), | |
body: const Center(child: Text('Can Pop True')), | |
), | |
); | |
} | |
} | |
class CanPopFalse extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return PopScope( | |
canPop: false, | |
onPopInvoked: (didPop) { | |
print('$runtimeType didPop: $didPop'); | |
}, | |
child: Scaffold( | |
appBar: AppBar(title: const Text('Can Pop False')), | |
body: const Center(child: Text('Can Pop False (re-run to go back)')), | |
), | |
); | |
} | |
} | |
class CanPopFalseButAllowedNotCatchingSecondaryCall extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return PopScope( | |
canPop: false, | |
onPopInvoked: (didPop) { | |
print('$runtimeType didPop: $didPop'); | |
Navigator.of(context).pop(); | |
}, | |
child: Scaffold( | |
appBar: AppBar(title: const Text('Can Pop False But Allowed')), | |
body: const Center(child: Text('Can Pop False But Allowed but not catching the secondary call to onPopInvoked (re-run to go back)')), | |
), | |
); | |
} | |
} | |
class CanPopFalseButAllowedCatchingSecondaryCall extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return PopScope( | |
canPop: false, | |
onPopInvoked: (didPop) { | |
print('$runtimeType didPop: $didPop'); | |
/// Unlike CanPopFalseButAllowedNotCatchingSecondaryCall, the | |
/// second time this callback is invoked we check if the pop | |
/// was allowed and then just don't run any Navigation related | |
/// functions. | |
/// | |
/// On Web the error in CanPopFalseButAllowedNotCatchingSecondaryCall | |
/// doesn't come through very well but on iOS/Android it provides a | |
/// bit more detail. | |
if (didPop) { | |
return; | |
} | |
Navigator.of(context).pop(); | |
}, | |
child: Scaffold( | |
appBar: AppBar(title: const Text('Can Pop False But Allowed')), | |
body: const Center(child: Text('Can Pop False But Allowed catching the secondary call to onPopInvoked')), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment