Created
May 3, 2018 18:46
-
-
Save antronic/e4953ae41b2c15492d9be5a5e8c07bec to your computer and use it in GitHub Desktop.
Create Flutter app with WidgetApp class
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
// Special thanks: Hemanth Raj | |
// Ref: https://stackoverflow.com/questions/48024504/app-navigation-example-using-widgetsapp#answer-48030789 | |
import 'package:flutter/widgets.dart'; | |
void main() => runApp(new MyWidgetsApp()); | |
class MyWidgetsApp extends StatelessWidget { | |
Route generate(RouteSettings settings){ | |
Route page; | |
switch(settings.name){ | |
case "/": | |
page = new PageRouteBuilder( | |
pageBuilder: (BuildContext context,Animation<double> animation, | |
Animation<double> secondaryAnimation){ | |
return new Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
const Text("Home Page",textDirection: TextDirection.ltr,), | |
const Padding(padding: const EdgeInsets.all(10.0)), | |
new GestureDetector( | |
onTap: () => Navigator.of(context).pushNamed("/first"), | |
child: new Container( | |
padding: const EdgeInsets.all(10.0), | |
color:Colors.blue, | |
child: const Text("Go to First Page"), | |
), | |
), | |
const Padding(padding: const EdgeInsets.all(10.0)), | |
new GestureDetector( | |
onTap: () => Navigator.of(context).pushNamed("/abcd"), | |
child: new Container( | |
padding: const EdgeInsets.all(10.0), | |
color:Colors.blue, | |
child: const Text("Unkown Route"), | |
), | |
) | |
], | |
); | |
}, | |
transitionsBuilder: (_, Animation<double> animation, Animation<double> second, Widget child) { | |
return new FadeTransition( | |
opacity: animation, | |
child: new FadeTransition( | |
opacity: new Tween<double>(begin: 1.0, end: 0.0).animate(second), | |
child: child, | |
), | |
); | |
} | |
); | |
break; | |
case "/first": | |
page = new PageRouteBuilder( | |
pageBuilder: (BuildContext context,Animation<double> animation, | |
Animation<double> secondaryAnimation){ | |
return new Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
const Text("First Page",textDirection: TextDirection.ltr,), | |
const Padding(padding: const EdgeInsets.all(10.0)), | |
new GestureDetector( | |
onTap: () => Navigator.of(context).pop(), | |
child: new Container( | |
padding: const EdgeInsets.all(10.0), | |
color:Colors.blue, | |
child: const Text("Back"), | |
), | |
) | |
] | |
); | |
}, | |
transitionsBuilder: (_, Animation<double> animation, Animation<double> second, Widget child) { | |
return new FadeTransition( | |
opacity: animation, | |
child: new FadeTransition( | |
opacity: new Tween<double>(begin: 1.0, end: 0.0).animate(second), | |
child: child, | |
), | |
); | |
} | |
); | |
break; | |
} | |
return page; | |
} | |
Route unKnownRoute(RouteSettings settings){ | |
return new PageRouteBuilder( | |
pageBuilder: (BuildContext context,Animation<double> animation, | |
Animation<double> secondaryAnimation){ | |
return new Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
const Text("First Page",textDirection: TextDirection.ltr,), | |
const Padding(padding: const EdgeInsets.all(10.0)), | |
new GestureDetector( | |
onTap: () => Navigator.of(context).pop(), | |
child: new Container( | |
padding: const EdgeInsets.all(10.0), | |
color:Colors.blue, | |
child: const Text("Back"), | |
), | |
) | |
] | |
); | |
} | |
); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return new WidgetsApp( | |
onGenerateRoute: generate, | |
onUnknownRoute: unKnownRoute, | |
textStyle: const TextStyle(), | |
initialRoute: "/", | |
color: Colors.red | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment