Created
March 15, 2024 19:05
-
-
Save justinmc/c436b8d82136ec77c8026aa5318da804 to your computer and use it in GitHub Desktop.
How to return a value from a route dismissed by system back gesture. Will break predictive back in a root route, and maybe an antipattern overall.
This file contains hidden or 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
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
initialRoute: '/', | |
onGenerateRoute: (RouteSettings settings) { | |
return switch (settings.name) { | |
'/' => MaterialPageRoute( | |
builder: (BuildContext context) => const PageOne(), | |
), | |
'/two' => MaterialPageRoute<bool>( | |
builder: (BuildContext context) => const PageTwo(), | |
), | |
_ => MaterialPageRoute( | |
builder: (BuildContext context) => const Text('404'), | |
), | |
}; | |
}, | |
); | |
} | |
} | |
class PageOne extends StatefulWidget { | |
const PageOne({ | |
Key? key, | |
}) : super(key: key); | |
@override | |
State<PageOne> createState() => _PageOneState(); | |
} | |
class _PageOneState extends State<PageOne> { | |
bool? result; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text('Page One'), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
if (result != null) | |
Text('Last result from Page Two was: $result'), | |
TextButton( | |
onPressed: () async { | |
final bool? nextResult = await Navigator.pushNamed<bool>(context, '/two'); | |
if (nextResult != result) { | |
setState(() { | |
result = nextResult; | |
}); | |
} | |
}, | |
child: const Text('Go to Page Two'), | |
), | |
], | |
), | |
), | |
); | |
} | |
} | |
class PageTwo extends StatefulWidget { | |
const PageTwo({super.key}); | |
@override | |
State<PageTwo> createState() => _PageTwoState(); | |
} | |
class _PageTwoState extends State<PageTwo> { | |
final TextEditingController controller = TextEditingController(); | |
bool value = false; | |
void _pop() { | |
Navigator.pop<bool>(context, value); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text('Page Two'), | |
leading: IconButton( | |
onPressed: () { | |
_pop(); | |
}, | |
icon: const Icon(Icons.navigate_before), | |
), | |
), | |
body: PopScope( | |
canPop: false, | |
onPopInvoked: (bool didPop) { | |
if (didPop) { | |
return; | |
} | |
_pop(); | |
}, | |
child: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
const Text('Go back to return the Switch\'s value to Page One.'), | |
Switch( | |
value: value, | |
onChanged: (bool nextValue) { | |
setState(() { | |
value = nextValue; | |
}); | |
}, | |
), | |
], | |
), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment