Navigatitor.pushNamed ve Veri taşıma ModalRoute.of

Musa BAL
3 min readSep 26, 2021

Map ile sayfaları oluşturduk

Transfer edilecek değişkenleri class ile tanımladık, tip yarattık

String tipli veriyi diğer sayfaya gönderiyoruz

BÜTÜN KODLAR

import 'package:flutter/cupertino.dart';
import "package:flutter/material.dart";
import 'package:flutter/services.dart';
import 'dart:ui';

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

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(accentColor: Colors.grey),
//home: HomePage(), // Map route yapısında buna gerek yok
initialRoute: "/", // anasayfayı belirtiyoruz

routes: { // Map kullanarak route ları belirledik
"/": (context) => HomePage(),
"/routePink": (context) => RoutePink(),
"/routeGreen": (context) => RouteGreen(),
"/routeGrey": (context) => RouteGrey()
});
}
}



//SAYFALAR ARASI TAŞINACAK VERİYİ AYRI BİR CLASS İLE TANIMLAYALIM

class Kullanici{

//INSTANCE VARIABLES

String? ad;
int? yas;
String? adres;

//CONSTRUCTOR

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

}







class HomePage extends StatelessWidget { // Kullanıcı tipini class ile yaratmıştık
final Kullanici kullanici_1 = Kullanici(ad:"Musa", yas:38, adres: "BURSA"); // Kullanıcı tipini türettik
// gönderilecek değişken final ile tanımlandı
@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"),
RaisedButton(
child: Text("Git -> Route Pink"),
onPressed: () {
Navigator.pushNamed( //Navigator.pushNamed
context, "/routePink", arguments: kullanici_1); //parametre olarak gönderildi
})
])),
);
}
}

///////////////////////////////////////////////
/// ROUTE PINK
///////////////////////////////////////////////
class RoutePink extends StatelessWidget {


Kullanici kullanici_2 = Kullanici(ad:"yesil", yas: 22, adres: "istanbul");

@override
Widget build(BuildContext context) {

// homepageden gelen veriyi context oluştuktan sonra burada alıyoruz

final Kullanici yerelKullanici = ModalRoute.of(context)!.settings.arguments as Kullanici;

return Scaffold(
backgroundColor: Colors.pink,
appBar: AppBar(title: Text("Route Pink")),
body: Center(
child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
Text("ad : ${yerelKullanici.ad} yas: ${yerelKullanici.yas} adres: ${yerelKullanici.adres}"), // text içinde kullanılıyor.

RaisedButton(
child: Text("Git -> Route Green"),
onPressed: () {
Navigator.pushNamed(context, "/routeGreen", arguments: kullanici_2);
}),
RaisedButton(
child: Text("Geri Dön"),
onPressed: () {
Navigator.pop(context);
}),
])),
);
}
}

///////////////////////////////////////////////
/// ROUTE GREEN
///////////////////////////////////////////////
class RouteGreen extends StatelessWidget {



String greyPageSend="yeşil sayfadan gele mesaj"; //String veriyi argüman olarak göndericez


@override
Widget build(BuildContext context) {
final Kullanici yerelKullanici = ModalRoute.of(context)!.settings.arguments as Kullanici;
return Scaffold(
backgroundColor: Colors.green,
appBar: AppBar(title: Text("Route Green")),
body: Center(
child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
Text("ad : ${yerelKullanici.ad} yas: ${yerelKullanici.yas} adres: ${yerelKullanici.adres}"), // text içinde kullanılıyor.
RaisedButton(
child: Text("Git -> Route Grey"),
onPressed: () {
Navigator.pushNamed(context, "/routeGrey",arguments: greyPageSend); //String değişkenimizi parametre olarak gönderiyoruz
}),
RaisedButton(
child: Text("Geri Dön"),
onPressed: () {
Navigator.pop(context); //green kalsın hepsini kaldır. logOut için
}),
])),
);
}
}

///////////////////////////////////////////////
/// ROUTE GREY
///////////////////////////////////////////////
class RouteGrey extends StatelessWidget {
@override
Widget build(BuildContext context) {

final String gelenMesaj =ModalRoute.of(context)!.settings.arguments as String; //String veriyi gönderilen sayfada karşılıyoruz

return Scaffold(
backgroundColor: Colors.grey,
appBar: AppBar(title: Text("Route Grey")),
body: Center(
child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
Text("mesaj : $gelenMesaj"), //String ifadeyi text içine yazdırdık

RaisedButton(child: Text("Git -> …."), onPressed: () {}),
RaisedButton(
child: Text("Geri Dön"),
onPressed: () {
Navigator.pop(context);
//Navigator.popUntil(context, (route)=>route.isFirst);
//Navigator.popUntil(context, (route)=>route.isFirst);}, //en baştaki sayfaya git
},
),
])),
);
}
}

--

--