Last active
January 6, 2018 11:47
-
-
Save killermonk/140fd74fbda39ea643402d44918712ec to your computer and use it in GitHub Desktop.
An example of routing
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 'dart:async'; | |
import 'package:flutter/material.dart'; | |
void main() => runApp(new MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return new MaterialApp( | |
title: 'Flutter Demo', | |
theme: new ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: new MyHomePage(), | |
routes: <String, WidgetBuilder>{ | |
'/ScreenA': (BuildContext context) => new ScreenA(), | |
'/ScreenB': (BuildContext context) => new ScreenB(), | |
'/ScreenC': (BuildContext context) => new ScreenC(), | |
}, | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
MyHomePage({Key key}) : super(key: key); | |
@override | |
_MyHomePageState createState() => new _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
bool _loading; | |
Future<String> checkPreferences() async { | |
await new Future.delayed(new Duration(seconds: 1)); | |
return '/ScreenA'; | |
// return null; // Test what happens if no state is found | |
} | |
@override | |
void initState() { | |
super.initState(); | |
_loading = true; | |
checkPreferences().then((route) { | |
setState(() { | |
_loading = false; | |
if (route?.isNotEmpty == true) { | |
Navigator.of(context).pushReplacementNamed(route); | |
} | |
}); | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
final NavigatorState nav = Navigator.of(context); | |
final List<Widget> children = []; | |
if (_loading) { | |
children.add(new Center(child: new Text("Loading"))); | |
} else { | |
children.addAll(<Widget>[ | |
new MaterialButton( | |
child: new Center(child: new Text('Screen A')), | |
onPressed: () => nav.pushReplacementNamed('/ScreenA'), | |
), | |
new MaterialButton( | |
child: new Center(child: new Text('Screen B')), | |
onPressed: () => nav.pushReplacementNamed('/ScreenB'), | |
), | |
new MaterialButton( | |
child: new Center(child: new Text('Screen C')), | |
onPressed: () => nav.pushReplacementNamed('/ScreenC'), | |
), | |
]); | |
} | |
return new Scaffold( | |
appBar: new AppBar( | |
title: new Text('Routes test'), | |
), | |
body: new Column( | |
crossAxisAlignment: CrossAxisAlignment.center, | |
children: children, | |
), | |
); | |
} | |
} | |
class ScreenA extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return new Scaffold( | |
body: new Center( | |
child: new Text('Screen A'), | |
), | |
); | |
} | |
} | |
class ScreenB extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return new Scaffold( | |
body: new Center( | |
child: new Text('Screen B'), | |
), | |
); | |
} | |
} | |
class ScreenC extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return new Scaffold( | |
body: new Center( | |
child: new Text('Screen C'), | |
), | |
); | |
} | |
} |
i create another project and it s working..... how can be?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
yes in ios work , in android not....