|8| Routes

Routes:
 Routes



import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,

      home: MyHome(),
    );
  }
}

class MyHome extends StatefulWidget {
  @override  _MyHomeState createState() => _MyHomeState();
}

class _MyHomeState extends State<MyHome> {
  @override  Widget build(BuildContext ctx) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Page 1"),
        backgroundColor: Colors.black45,
        elevation: 1.0,
      ),
      body: Center(
        child: FlatButton(
          color: Colors.blue[300],
          onPressed: (){
            Navigator.push(ctx, PageTwo());
          },
          child: Text("Go to Page 2"),
        ),
      ),
    );
  }
}

class PageTwo extends MaterialPageRoute<Null> {
  PageTwo() : super(builder: (BuildContext ctx) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Page 2"),
        backgroundColor: Colors.black45,
        elevation: 1.0,
      ),
      body: Center(
        child: RaisedButton(
          color: Colors.blue[300],
          onPressed: () {
            Navigator.push(
                ctx,
                PageThree()
            );
          },
          child: Text("Go to Page Last"),
        ),
      ),
    );
  });
}

class PageThree extends MaterialPageRoute<Null> {
  PageThree() : super(builder: (BuildContext ctx) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Last Page!"),
        backgroundColor: Colors.black45,
        elevation: 2.0,
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.close),
            onPressed: (){
              Navigator.pop(ctx);
            },
          )
        ],
      ),
      body: Center(
        child: MaterialButton(
          color: Colors.red[300],
          onPressed: (){
            Navigator.popUntil(ctx, ModalRoute.withName(Navigator.defaultRouteName));
          },
          child: Text("Go Home!"),
        ),
      ),
    );
  });
}

Comments

Popular posts from this blog

|9| Transform Slider

|6| Stack