Flutter Development (Day-04):
- This is the fifth post from this blog.In this post we are going to discuss about basic Navigation between screens like Intent in android.
- All source code : Click Here.
lib/main.dart:
import 'package:allaboutflutterblog/tuts/navigation.dart';import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
home: FirstScreen(),
debugShowCheckedModeBanner: false,
),
);
}
import 'package:flutter/material.dart';
class FirstScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Navigation Example"),
),
body: Container(
alignment: Alignment.center,
child: FlatButton(
color: Colors.redAccent,
child: Text(
"Go to Next Screen",
style: TextStyle(color: Colors.white),
),
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(builder: (context) {
return SecondScreen();
}),
);
},
),
),
);
}
}
class SecondScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Navigation Example"),
),
body: Container(
alignment: Alignment.center,
child: FlatButton(
child: Text(
"Go Back",
style: TextStyle(color: Colors.white),
),
color: Colors.redAccent,
onPressed: () {
Navigator.of(context).pop();
},
),
),
);
}
}
Explanation:
- Here Navigator is a material widget which enables us to navigate between screens.
- Context : Which tells the app that currently where we the app is locating.
- Push - pushes the widget and Pop removes that current widget from the Stack.
- MaterialPageRoute : Enables a Material Route which navigates to next page.
- Source Code: here
Comments
Post a Comment