Instantly share code, notes, and snippets.
-
Star
(1)
1
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save RodBr/30d50d12ceb62b6aa861959b14dec1e8 to your computer and use it in GitHub Desktop.
Navigation
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'; | |
import 'package:get/get.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return GetMaterialApp( | |
theme: ThemeData( | |
primaryColor: Colors.purple, | |
buttonTheme: ButtonThemeData( | |
textTheme: ButtonTextTheme.primary, | |
buttonColor: Colors.green, | |
), | |
textTheme: TextTheme(bodyText2: TextStyle(fontSize: 24))), | |
initialRoute: 'Page1', | |
namedRoutes: { | |
'Page1': GetRoute(page: Page1()), | |
'Page2': GetRoute( | |
page: Page2(), | |
transition: Transition.rotate, | |
transitionDuration: Duration(milliseconds: 500)), | |
'Page3': GetRoute(page: Page3(), binding: Page3Binding()) | |
}, | |
); | |
} | |
} | |
class Page1 extends StatelessWidget { | |
const Page1({Key key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(title: Text('Page 1')), | |
backgroundColor: Colors.blue[100], | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text('Parameters demo'), | |
RaisedButton( | |
onPressed: () async { | |
var result = await Get.toNamed( | |
'Page2?message=Hi from Page 1&status=true'); | |
if (result is String) { | |
Get.snackbar('Returned', result, | |
backgroundColor: Colors.orange); | |
} | |
}, | |
child: Text('Page 2'), | |
), | |
SizedBox(height: 20), | |
Text('Controller demo'), | |
RaisedButton( | |
onPressed: () => Get.toNamed('Page3', arguments: 'Hi Page 3'), | |
child: Text('Page 3'), | |
), | |
], | |
), | |
), | |
); | |
} | |
} | |
class Page2 extends StatelessWidget { | |
const Page2({Key key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
final String message = Get.parameters['message']; | |
final String status = Get.parameters['status']; | |
return Scaffold( | |
appBar: AppBar(title: Text('Page 2')), | |
backgroundColor: Colors.pink[100], | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text('Parameters demo'), | |
SizedBox(height: 20), | |
Text('$message - $status', style: TextStyle(color: Colors.red)), | |
SizedBox(height: 20), | |
RaisedButton( | |
onPressed: () => Get.back(result: 'Hi back'), | |
child: Text('Get back')), | |
], | |
))); | |
} | |
} | |
class Page3 extends StatelessWidget { | |
const Page3({Key key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
String message = Get.arguments; | |
return GetBuilder<Page3Controller>( | |
builder: (controller) => Scaffold( | |
appBar: AppBar(title: Text('Page 3')), | |
backgroundColor: Colors.purple[100], | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text('Agruments demo'), | |
SizedBox(height: 20), | |
Text('Page: $message', style: TextStyle(color: Colors.red)), | |
SizedBox(height: 20), | |
Text(controller.getArgs(), style: TextStyle(color: Colors.blue)), | |
SizedBox(height: 20), | |
RaisedButton( | |
onPressed: () => controller.getOff(), child: Text('Return')), | |
], | |
))), | |
); | |
} | |
} | |
class Page3Controller extends GetController { | |
onInit() { | |
print(Get.arguments); | |
} | |
getArgs() { | |
return 'Controller: ${Get.arguments}'; | |
} | |
getOff() { | |
Get.offAllNamed('Page1'); | |
} | |
} | |
class Page3Binding implements Bindings { | |
@override | |
void dependencies() { | |
Get.lazyPut<Page3Controller>(() => Page3Controller()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment