Chapter 2: Flutter Navigation: Getting Started

Flutter Navigation: Getting Startedarrow-up-right

大綱

  • Getting Started

  • Creating a Widget to Navigate To

  • Navigation using Routes

  • Popping the Stack

  • Returning a Value

  • Creating Custom Transitions

  • Where to Go From Here?

Creating a Widget to Navigate To

  • create a new file named memberwidget.dart:

import 'package:flutter/material.dart';

import 'member.dart';

class MemberWidget extends StatefulWidget {
 // Add a Member property for the widget.
  final Member member;

  MemberWidget(this.member) {
    if (member == null) {
      // Make sure that the member argument is not-null in the widget constructor
      throw ArgumentError("member of MemberWidget cannot be null. Received: '$member'");
    }
  }

@override
  // Use a MemberState class for the state, passing along a Member object to the MemberState.
createState() => MemberState(member);
}

class MemberState extends State<MemberWidget> {
  final Member member;
 //  given MemberState a Member property and a constructor.
  MemberState(this.member);

  @override
  Widget build(BuildContext context) {
    // creating a Scaffold, a material design container, which holds an AppBar and a Padding with a child Image for the member avatar.
    return Scaffold(
      appBar: AppBar(
        title: Text(member.login),
      ),
      body: Padding(
        padding: EdgeInsets.all(16.0),
        child: Image.network(member.avatarUrl)
        ),
    );
  }
}
  • Navigation in Flutter is centered around the idea of routes.

  • Routes are similar in concept to the routes that you would use in a REST API, where each route is relative to some root. The widget main() acts like the root in your app (“/”) and all other routes are relative to that one.

  • One way to use routes is with PageRoute

    • Since you’re working with a Flutter MaterialApp, you’ll use the MaterialPageRoute

Popping the Stack

  • memberwidget增加一個back button, 來進行pop動作

Returning a Value

  • Routes can return values

  • Add the following private async method to MemberState

  • Inside MemberState‘s build(), add a RaisedButton that calls _showOKScreen() as another child of the Column

Creating Custom Transitions

Where to Go From Here?

Last updated