Skip to content

Instantly share code, notes, and snippets.

@PiN73
Created May 4, 2021 11:55
Show Gist options
  • Save PiN73/a1e41ec89bfac6413222c0ce6d8234ad to your computer and use it in GitHub Desktop.
Save PiN73/a1e41ec89bfac6413222c0ce6d8234ad to your computer and use it in GitHub Desktop.
Flutter transition using Navigator 2.0
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