Created
May 4, 2021 11:55
-
-
Save PiN73/a1e41ec89bfac6413222c0ce6d8234ad to your computer and use it in GitHub Desktop.
Flutter transition using Navigator 2.0
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(MyApp()); | |
} | |
class MyApp extends StatefulWidget { | |
@override | |
_MyAppState createState() => _MyAppState(); | |
} | |
class _MyAppState extends State<MyApp> { | |
bool isLoggedIn = false; | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
theme: ThemeData( | |
// iOS behaviour on all platforms: | |
// platform: TargetPlatform.iOS, | |
), | |
home: Navigator( | |
pages: [ | |
if (isLoggedIn) | |
MaterialPage<void>( | |
key: ValueKey(1), | |
child: Scaffold( | |
backgroundColor: Colors.blue, | |
), | |
) | |
else | |
MaterialPage<void>( | |
key: ValueKey(2), | |
child: Scaffold( | |
backgroundColor: Colors.orange, | |
body: Center( | |
child: ElevatedButton( | |
onPressed: () => setState(() { | |
isLoggedIn = true; | |
}), | |
child: Text('Log in'), | |
), | |
), | |
), | |
), | |
], | |
onPopPage: (Route<dynamic> route, dynamic result) { | |
// ... | |
return route.didPop(result); | |
}, | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment