Flutter
  • Flutter Tutorials Handbook
  • 01 Flutter: Hello World
    • Related Article
  • 02 Flutter: Stateless Widgets
  • 03 Flutter: Buttons and Stateful widgets
  • 04 Flutter: Basic Navigation & Routes
    • Simple routes using StatelessWidget
    • Routes using StatefulWidget
  • 05 Flutter: Using onChanged to show input text
  • 06 Flutter: Using onSubmitted to show input text after submit
  • 07 Flutter: Adding-Deleting text in TextField
  • 08 Flutter: Tab Navigation
  • 09 Flutter: HTTP requests and Rest API
  • 10 Flutter: ListView with JSON or List Data
  • 11 Flutter: Sliding menu using a Drawer
    • Simple Drawer
    • Drawer using variables
  • 12 Flutter: Animations
  • 13 Flutter: JSON Storage
  • 14 Flutter: Friendly Chat App
  • 15 Flutter: Changing icon color onfocus
  • 16 Flutter: Horizontal ListView and Tabs
  • 17 Flutter: RaisedButton
  • 18 Flutter: RaisedButton with parameters
  • 19 Flutter: FlatButton
  • 20 Flutter: IconButton
  • 21 Flutter: Updating data in TextField
  • 22 Flutter: Checkbox
  • 23 Flutter: Radio with Functions
  • 24 Flutter: Switch
  • 25 Flutter: Slider
  • 26 Flutter: DatePicker
  • 27 Flutter: appBar
  • 28 Flutter: Floating Action Button
  • 29 Flutter: Drawer which shows Toggle Menu
  • 30 Flutter: Footer Buttons
  • 31 Flutter: BottomNavigationBar
  • 32 Flutter: Using BottomSheets
  • 33 Flutter: Using SnackBar
  • 34 Flutter: AlertDialog
  • 35 Flutter: SimpleDialog
  • 36 Flutter: Rows and Columns
  • 37 Flutter: Using Cards
  • 38 Flutter: Using Expanded
  • 39 Flutter: ListviewBuilder using Dart maps
  • 40 Flutter: ListviewBuilder using Dart lists
  • 41 Flutter: Using Custom Widgets
  • 42 Flutter: Using Image Assets
  • 43 Flutter: ListviewBuilder with Refresh Indicator
Powered by GitBook
On this page
  1. 04 Flutter: Basic Navigation & Routes

Routes using StatefulWidget

main.dart
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var routes = <String, WidgetBuilder>{
      MyItemsPage.routeName: (BuildContext context) => new MyItemsPage(title: "MyItemsPage"),
    };
    return new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(title: 'Flutter Demo Home Page'),
      routes: routes,
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    Navigator.pushNamed(context, MyItemsPage.routeName);
  }

  @override
  Widget build(BuildContext context) {
    var button = new IconButton(icon: new Icon(Icons.access_alarm), onPressed: _onButtonPressed);
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new Column(
        children: <Widget>[
          new Text('Dog'),
          new Text('Cat'),
          button
        ],
      ),
      floatingActionButton: new FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: new Icon(Icons.add),
      ),
    );
  }

  void _onButtonPressed() {
    Navigator.pushNamed(context, MyItemsPage.routeName);
  }
}

class MyItemsPage extends StatefulWidget {
  MyItemsPage({Key key, this.title}) : super(key: key);

  static const String routeName = "/MyItemsPage";

  final String title;

  @override
  _MyItemsPageState createState() => new _MyItemsPageState();
}

/// // 1. After the page has been created, register it with the app routes
/// routes: <String, WidgetBuilder>{
///   MyItemsPage.routeName: (BuildContext context) => new MyItemsPage(title: "MyItemsPage"),
/// },
///
/// // 2. Then this could be used to navigate to the page.
/// Navigator.pushNamed(context, MyItemsPage.routeName);
///

class _MyItemsPageState extends State<MyItemsPage> {
  @override
  Widget build(BuildContext context) {
    var button = new IconButton(icon: new Icon(Icons.arrow_back), onPressed: _onButtonPressed);
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new Container(
        child: new Column(
          children: <Widget>[
            new Text('Item1'),
            new Text('Item2'),
            button
          ],
        ),
      ),
      floatingActionButton: new FloatingActionButton(
        onPressed: _onFloatingActionButtonPressed,
        tooltip: 'Add',
        child: new Icon(Icons.add),
      ),
    );
  }

  void _onFloatingActionButtonPressed() {
  }

  void _onButtonPressed() {
    Navigator.pop(context);
  }
}
PreviousSimple routes using StatelessWidgetNext05 Flutter: Using onChanged to show input text

Last updated 6 years ago