Created
November 27, 2018 15:46
-
-
Save ScottS2017/3288c7e7e9a014430e56dd6be4c259ab to your computer and use it in GitHub Desktop.
Response to SO question: https://stackoverflow.com/questions/53491536/how-to-navigate-to-another-page-on-load-in-flutter
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
import 'package:flutter/material.dart'; | |
void main() => runApp(new MyApp()); | |
class MyApp extends StatelessWidget { | |
/// Use these booleans to see how each approach works, use the one that fits your needs | |
/// *** For some strange reason HotReload is not responding to changes in these bools | |
/// but if you use HotRestart then it's fine. | |
/// | |
/// Are you going to be using the setting button? | |
final bool useSettingButton = true; | |
/// Is the user already logged in? | |
final bool userIsLoggedIn = true; | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: useSettingButton == true ? | |
UsingSettingsButton(userIsLoggedIn: userIsLoggedIn) : | |
DoesNotUseSettingButton(loggedIn: userIsLoggedIn), | |
); | |
} | |
} | |
class DoesNotUseSettingButton extends StatelessWidget { | |
final bool loggedIn; | |
DoesNotUseSettingButton({ | |
@required this.loggedIn, | |
}); | |
@override | |
Widget build(BuildContext context) { | |
Widget result; | |
if (loggedIn == true) { | |
result = LoggedIn(); | |
} else { | |
result = NotLoggedIn(); | |
} | |
return result; | |
} | |
} | |
class UsingSettingsButton extends StatelessWidget { | |
const UsingSettingsButton({ | |
Key key, | |
@required this.userIsLoggedIn, | |
}) : super(key: key); | |
final bool userIsLoggedIn; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Rera Farm'), | |
actions: <Widget>[ | |
PopupMenuButton( | |
itemBuilder: (BuildContext context) { | |
return <PopupMenuEntry>[ | |
PopupMenuItem( | |
child: ListTile( | |
title: Text('Settings'), | |
onTap: () { | |
Navigator.push(context, MaterialPageRoute( | |
builder: (BuildContext context) => | |
userIsLoggedIn == true ? | |
LoggedIn() : | |
NotLoggedIn(), | |
), | |
); | |
}, | |
), | |
), | |
]; | |
}, | |
) | |
], | |
), | |
body: Container( | |
height: double.infinity, | |
width: double.infinity, | |
color: Colors.blue, | |
), | |
); | |
} | |
} | |
class LoggedIn extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
height: double.infinity, | |
width: double.infinity, | |
color: Colors.green, | |
); | |
} | |
} | |
class NotLoggedIn extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
height: double.infinity, | |
width: double.infinity, | |
color: Colors.red, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment