Navigation: onGenerateRoute

Bu navigation türünü daha sık kullanırız.

if 'li yapı :

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(accentColor: Colors.grey),
      //home: HomePage(),
      
//         initialRoute: HomePage.routeName,
//         routes: {
//           HomePage.routeName: (context) => HomePage(),
//           RoutePink.routeName: (context) => RoutePink(),
//           RouteGreen.routeName: (context) => RouteGreen(),
//           RouteGrey.routeName: (context) => RouteGrey(),
//         },

      onGenerateRoute: (settings) {
        if (settings.name == HomePage.routeName) {
          return MaterialPageRoute(builder: (context) => HomePage());}
        if (settings.name == RoutePink.routeName) {
          return MaterialPageRoute(builder: (context) => RoutePink());}
        if (settings.name == RouteGreen.routeName) {
          return MaterialPageRoute(builder: (context) => RouteGreen());}
        if (settings.name == RouteGrey.routeName) {
          return MaterialPageRoute(builder: (context) => RouteGrey());}
           
        return null;
        
      },
    );
  }
}

class Kullanici {
  String ad;
  int yas;
  String adres;

  Kullanici({this.ad, this.yas, this.adres});
}

class HomePage extends StatelessWidget {
  final Kullanici kullanici_1 =
      Kullanici(ad: 'Metin', adres: 'Beşiktaş', yas: 40);

  static String routeName = '/';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.yellow,
      appBar: AppBar(title: Text('Sayfalar Arası Geçiş / Navigation')),
      body: Center(
          child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
        Text('HomePage'),
        SizedBox(height: 15.0),
        RaisedButton(
            child: Text('Git -> Route Pink'),
            onPressed: () {
              Navigator.pushNamed(context, RoutePink.routeName,
                  arguments: kullanici_1);
            })
      ])),
    );
  }
}

///////////////////////////////////////////////
///   ROUTE PINK
///////////////////////////////////////////////

class RoutePink extends StatelessWidget {
  static String routeName = '/routePink';

  //Kullanici yerelKullanici = ModalRoute.of(context).settings.arguments;

  @override
  Widget build(BuildContext context) {
    Kullanici yerelKullanici = ModalRoute.of(context).settings.arguments;

    return Scaffold(
      backgroundColor: Colors.pink,
      appBar: AppBar(title: Text('Route Pink')),
      body: Center(
          child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
//         Text('Kullacıcı adı: ${yerelKullanici.ad}, Yaş : //${yerelKullanici.yas}, Adres : ${yerelKullanici.adres}'),
        Text('Pink Sayfası'),
        RaisedButton(
            child: Text('Git -> Route Green'),
            onPressed: () {
              Navigator.pushNamed(context, RouteGreen.routeName);
            }),
        RaisedButton(
            child: Text('Geri Dön'),
            onPressed: () {
              Navigator.pop(context);
            }),
      ])),
    );
  }
}

///////////////////////////////////////////////
///   ROUTE GREEN
///////////////////////////////////////////////
class RouteGreen extends StatelessWidget {
  static String routeName = '/routeGreen';
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.green,
      appBar: AppBar(title: Text('Route Green')),
      body: Center(
          child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
        Text('Şu an RouteGreen en üstte'),
        SizedBox(height: 35.0),
        RaisedButton(
            child: Text('Git -> Route Grey'),
            onPressed: () {
              Navigator.pushNamed(context, RouteGrey.routeName);
            }),
        SizedBox(height: 15.0),
        RaisedButton(
            child: Text('Geri Dön'),
            onPressed: () {
              Navigator.pop(context);
            }),
      ])),
    );
  }
}

///////////////////////////////////////////////
///   ROUTE GREY
///////////////////////////////////////////////
class RouteGrey extends StatelessWidget {
  static String routeName = '/routeGrey';
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey,
      appBar: AppBar(title: Text('Route Grey')),
      body: Center(
          child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
        Text('Şu an RouteGrey en üstte'),
        RaisedButton(child: Text('Git -> ....'), onPressed: () {}),
        RaisedButton(
            child: Text('Geri Dön'),
            onPressed: () {
              Navigator.pop(context);
            }),
      ])),
    );
  }
}

switch - case 'li yapı

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        theme: ThemeData(accentColor: Colors.grey),
        //home: HomePage(),

//         initialRoute: HomePage.routeName,
//         routes: {
//           HomePage.routeName: (context) => HomePage(),
//           RoutePink.routeName: (context) => RoutePink(),
//           RouteGreen.routeName: (context) => RouteGreen(),
//           RouteGrey.routeName: (context) => RouteGrey(),
//         },

//       onGenerateRoute: (settings) {
//         if (settings.name == HomePage.routeName) {
//           return MaterialPageRoute(builder: (context) => HomePage());}
//         if (settings.name == RoutePink.routeName) {
//           return MaterialPageRoute(builder: (context) => RoutePink());}
//         if (settings.name == RouteGreen.routeName) {
//           return MaterialPageRoute(builder: (context) => RouteGreen());}
//         if (settings.name == RouteGrey.routeName) {
//           return MaterialPageRoute(builder: (context) => RouteGrey());}

//         return null;

//       },

        onGenerateRoute: (settings) {
          
          switch (settings.name) {
            case HomePage.routeName:
              return MaterialPageRoute(builder: (context) => HomePage());
            case RoutePink.routeName:
              return MaterialPageRoute(builder: (context) => RoutePink(settings.arguments));
            case RouteGreen.routeName:
              return MaterialPageRoute(builder: (context) => RouteGreen());
            case RouteGrey.routeName:
              return MaterialPageRoute(builder: (context) => RouteGrey());
            default:
              return null;
          }
        });
  }
}

class Kullanici {
  String ad;
  int yas;
  String adres;

  Kullanici({this.ad, this.yas, this.adres});
}

class HomePage extends StatelessWidget {
  final Kullanici kullanici_1 =
      Kullanici(ad: 'Metin', adres: 'Beşiktaş', yas: 40);

  static const String routeName = '/';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.yellow,
      appBar: AppBar(title: Text('Sayfalar Arası Geçiş / Navigation')),
      body: Center(
          child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
        Text('HomePage'),
        SizedBox(height: 15.0),
        RaisedButton(
            child: Text('Git -> Route Pink'),
            onPressed: () {
              Navigator.pushNamed(context, RoutePink.routeName,
                  arguments: kullanici_1);
            })
      ])),
    );
  }
}

///////////////////////////////////////////////
///   ROUTE PINK
///////////////////////////////////////////////

class RoutePink extends StatelessWidget {
  static const String routeName = '/routePink';
  
  final Kullanici yerelKullanici;
  
  RoutePink(this.yerelKullanici);

  @override
  Widget build(BuildContext context) {
  //  Kullanici yerelKullanici = ModalRoute.of(context).settings.arguments;

    return Scaffold(
      backgroundColor: Colors.pink,
      appBar: AppBar(title: Text('Route Pink')),
      body: Center(
          child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
         Text('Kullacıcı adı: ${yerelKullanici.ad}, Yaş : ${yerelKullanici.yas}, Adres : ${yerelKullanici.adres}'),
        Text('Pink Sayfası'),
        RaisedButton(
            child: Text('Git -> Route Green'),
            onPressed: () {
              Navigator.pushNamed(context, RouteGreen.routeName);
            }),
        RaisedButton(
            child: Text('Geri Dön'),
            onPressed: () {
              Navigator.pop(context);
            }),
      ])),
    );
  }
}

///////////////////////////////////////////////
///   ROUTE GREEN
///////////////////////////////////////////////
class RouteGreen extends StatelessWidget {
  static const String routeName = '/routeGreen';
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.green,
      appBar: AppBar(title: Text('Route Green')),
      body: Center(
          child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
        Text('Şu an RouteGreen en üstte'),
        SizedBox(height: 35.0),
        RaisedButton(
            child: Text('Git -> Route Grey'),
            onPressed: () {
              Navigator.pushNamed(context, RouteGrey.routeName);
            }),
        SizedBox(height: 15.0),
        RaisedButton(
            child: Text('Geri Dön'),
            onPressed: () {
              Navigator.pop(context);
            }),
      ])),
    );
  }
}

///////////////////////////////////////////////
///   ROUTE GREY
///////////////////////////////////////////////
class RouteGrey extends StatelessWidget {
  static const String routeName = '/routeGrey';
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey,
      appBar: AppBar(title: Text('Route Grey')),
      body: Center(
          child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
        Text('Şu an RouteGrey en üstte'),
        RaisedButton(child: Text('Git -> BİTTİ!'), onPressed: () {}),
        RaisedButton(
            child: Text('Geri Dön'),
            onPressed: () {
              Navigator.pop(context);
            }),
      ])),
    );
  }
}

Last updated

Was this helpful?