package:flutter/material.dart
package, your screens, commonly derived from Scaffold
class, is already under navigation stack management by Flutter.
RaisedButton(
color: Colors.black,
child: Text('Go to SecondPage', style: TextStyle(color: Colors.white),),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondPage()),
);
},
),
push
and pop
.
class FristPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text('Redirect Page',),
backgroundColor: Colors.black,
),
body: Center(
child: RaisedButton(
color: Colors.black,
child: Text('Go to SecondPage', style: TextStyle(color: Colors.white),),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondPage()),
);
},
),
),
);
}
}
class SecondPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text("Second Page"),
backgroundColor: Colors.black,
),
body: Center(
child: RaisedButton(
color: Colors.black,
onPressed: () {
Navigator.pop(context);
},
child: Text('Go back!',style: TextStyle(color: Colors.white),),
),
),
);
}
}
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Navigation Basics',
home: FristPage(),
));
}
class FristPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text('Redirect Page',),
backgroundColor: Colors.black,
),
body: Center(
child: RaisedButton(
color: Colors.black,
child: Text('Go to SecondPage', style: TextStyle(color: Colors.white),),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondPage()),
);
},
),
),
);
}
}
class SecondPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text("Second Page"),
backgroundColor: Colors.black,
),
body: Center(
child: RaisedButton(
color: Colors.black,
onPressed: () {
Navigator.pop(context);
},
child: Text('Go back!',style: TextStyle(color: Colors.white),),
),
),
);
}
}