routes
MaterialApp({ title, color, theme, initialRoute, routes, locale, localizationsDelegates, supportedLocales, onGenerateTitle, home,..})
açıklama
adlandırılmış sayfalar olarak isimlendirilen sayfaların yollarının tanımlandığı map listesi. kısaca uygulamanın sayfaları.
konum
MaterialApp({ title, color, theme, initialRoute, routes, locale, localizationsDelegates, supportedLocales, onGenerateTitle, home,..})
parametreler
//Rout...............................................................................................
initialRoute: "/", // varsayılan ilk açılacak ana sayfa ,
routes: {
'/sayfabir': (context) => new SayafaBir(),
'/sayfaiki' : (context) => new SayafaIki(),
'/sayfauc' : (context) => new SayafaUc(),
'/sayfadort' : (context) => new SayafaDort()
},
örnek uygulama


kaynak: Anılcan Çakır
GitHub: GitHub
medium: Makale
dosya adı:main.dart
import 'package:app/pages/home.page.dart';
import 'package:app/pages/login.page.dart';
import 'package:app/services/auth.service.dart';
import 'package:flutter/material.dart';
AuthService appAuth = new AuthService();
void main() async {
// Set default home.
Widget _defaultHome = new LoginPage();
// Get result of the login function.
bool _result = await appAuth.login();
if (_result) {
_defaultHome = new HomePage();
}
// Run app!
runApp(new MaterialApp(
title: 'App',
home: _defaultHome,
routes: <String, WidgetBuilder>{
// Set routes for using the Navigator.
'/home': (BuildContext context) => new HomePage(),
'/login': (BuildContext context) => new LoginPage()
},
));
}
dosya adı: home.page.dart
import 'package:app/main.dart';
import 'package:flutter/material.dart';
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) => new Scaffold(
appBar: new AppBar(
title: new Text('Home'),
),
body: new Container(
margin: new EdgeInsets.only(
top: 50.0
),
alignment: Alignment.center,
child: new Column(
children: [
new Text('Welcome to App!'),
new FlatButton(
child: new Text(
'Logout'
),
onPressed: () {
appAuth.logout().then(
(_) => Navigator.of(context).pushReplacementNamed('/login')
);
}
)
],
),
),
);
}
dosya adı: login.page.dart
import 'package:app/main.dart';
import 'package:flutter/material.dart';
class LoginPage extends StatefulWidget {
@override
State createState() => new _LoginPageState();
}
class _LoginPageState extends State {
String _status = 'no-action';
@override
Widget build(BuildContext context) => new Scaffold(
appBar: new AppBar(
title: new Text('Login'),
),
body: new Container(
child: new Center(
child: new RaisedButton(
child: new Text(
'Login for App (${this._status})'
),
onPressed: () {
setState(() => this._status = 'loading');
appAuth.login().then((result) {
if (result) {
Navigator.of(context).pushReplacementNamed('/home');
} else {
setState(() => this._status = 'rejected');
}
});
}
),
),
),
);
}
dosya adı: auth.service.dart
import 'dart:async';
import 'dart:math';
class AuthService {
// Login
Future login() async {
// Simulate a future for response after 2 second.
return await new Future.delayed(
new Duration(
seconds: 2
), () => new Random().nextBool()
);
}
// Logout
Future logout() async {
// Simulate a future for response after 1 second.
return await new Future.delayed(
new Duration(
seconds: 1
)
);
}
}