A cikin Flutter, Navigator kayan aiki ne mai ƙarfi don sarrafa yanki na tsakiya da kewayawa shafi a cikin app ɗin ku. Yana ba ku damar gina ƙa'idodi tare da tsararren gine-gine da kewayawa mai sauƙi tsakanin fuska.
Ma'ana Routes
Don fara amfani da Navigator, kuna buƙatar bayyana routes a cikin app ɗin ku. Routes allo ne guda ɗaya waɗanda masu amfani za su iya kewayawa zuwa. Kuna iya ayyana routes ta amfani da MaterialApp kuma ku samar da tarin routes, inda route aka tsara kowannensu zuwa Widget.
Misali:
MaterialApp(
initialRoute: '/',
routes: {
'/':(context) => HomePage(),
'/second':(context) => SecondPage(),
},
)
A cikin misalin da ke sama, mun bayyana biyu routes: '/'(home page)
da '/second'(second page
). Kuna iya ƙara yawan adadin routes da ake buƙata.
Kewayawa Tsakanin Shafuka
Don kewaya tsakanin shafuka, zaku iya amfani da Navigator hanyoyin's. Hanya ɗaya gama gari ita ce tura suna, wanda ke ba ka damar kewaya zuwa wani shafi ta samar da sunan waccan route.
Misali:
// Navigate to the second page
Navigator.pushNamed(context, '/second');
Bugu da ƙari, zaku iya amfani da hanyar turawa don kewaya zuwa wani route kuma canza tsakanin shafuka.
Shigar da Bayanai Tsakanin Shafuka
Kuna iya ƙaddamar da bayanai tsakanin shafuka ta amfani da hanyar turaNamed tare da ma'aunin gardama.
Misali:
Navigator.pushNamed(
context,
'/second',
arguments: 'Data from the home page',
);
Sannan, zaku iya samun damar bayanan daga shafi na biyu ta amfani da ModalRoute.of da abubuwan saitin:
class SecondPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
String data = ModalRoute.of(context).settings.arguments;
// Use the data here
}
}
Komawa Shafin Baya
Don komawa zuwa shafin da ya gabata, zaku iya amfani da hanyar pop na Navigator. Wannan zai rufe shafin na yanzu kuma ya koma shafin da ya gabata a cikin tari.
Misali:
// Go back to the previous page
Navigator.pop(context);
Kammalawa
Navigator a cikin Flutter yana ba ku damar sarrafa jaha ta tsakiya kuma kewaya tsakanin shafuka cikin sauƙi. Ta amfani da Navigator, zaku iya gina ƙa'idodi tare da tsararren gine-gine kuma ku samar da ingantacciyar ƙwarewar mai amfani yayin kewayawa tsakanin allo.