56

Flutter-页面跳转传参

 4 years ago
source link: https://www.tuicool.com/articles/y22qMnf
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

路由(Route)在移动开发中通常指页面(Page),这跟web开发中单页应用的Route概念意义是相同的,Route在Android中通常指一个Activity,在iOS中指一个ViewController。所谓路由管理,就是管理页面之间如何跳转,通常也可被称为导航管理。Flutter中的路由管理和原生开发类似,无论是Android还是iOS,导航管理都会维护一个路由栈,路由入栈(push)操作对应打开一个新页面,路由出栈(pop)操作对应页面关闭操作,而路由管理主要是指如何来管理路由栈。

新建首页页面

import 'package:flutter/material.dart';

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("首页"),
      ),
      body: Center(
        child: Text("我是首页"),
      ),
    );
  }
}

在点击跳转页面导入 import 'homePage.dart' ;

处理按钮点击事件

floatingActionButton: new FloatingActionButton(
        onPressed: (){
          //导航到首页路由
          Navigator.push(context,
            MaterialPageRoute(builder: (context){
              return HomePage();
            }),
          );
        },
        tooltip: 'Increment',
        child: new Icon(Icons.add),
      ),

页面传值

进入新页面,传入参数

//导航到首页路由
Navigator.push(context,
  MaterialPageRoute(builder: (context){
    return HomePage(
        text:"我是首页"
    );
  }),
);

新页面接收参数

class HomePage extends StatelessWidget {
  //接收参数
  HomePage({
    Key key,
    @required this.text, //接收一个text参数
  }) : super(key: key);
  final String text;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("首页"),
      ),
      body: Center(
        child: Text(text),
      ),
    );
  }
}

https://book.flutterchina.club/chapter2/flutter_router.html


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK