flutter如何引用传值_详解Flutter给新页面传值

直接上代码,一个完整的demo

import 'package:flutter/foundation.dart';

import 'package:flutter/material.dart';

class Todo {

final String title;

final String description;

Todo(this.title, this.description);

}

void main() {

runApp(new MaterialApp(

title: 'Passing Data',

home: new TodosScreen(

todos: new List.generate(

20,

(i) => new Todo(

'Todo $i',

'A description of what needs to be done for Todo $i',

),

),

),

));

}

class TodosScreen extends StatelessWidget {

final List todos;

TodosScreen({Key key, @required this.todos}) : super(key: key);

@override

Widget build(BuildContext context) {

return new Scaffold(

appBar: new AppBar(

title: new Text('Todos'),

),

body: new ListView.builder(

itemCount: todos.length,

itemBuilder: (context, index) {

return new ListTile(

title: new Text(todos[index].title),

// When a user taps on the ListTile, navigate to the DetailScreen.

// Notice that we're not only creating a new DetailScreen, we're

// also passing the current todo through to it!

onTap: () {

Navigator.push(

context,

new MaterialPageRoute(

builder: (context) => new DetailScreen(todo: todos[index]),

),

);

},

);

},

),

);

}

}

class DetailScreen extends StatelessWidget {

// Declare a field that holds the Todo

final Todo todo;

// In the constructor, require a Todo

DetailScreen({Key key, @required this.todo}) : super(key: key);

@override

Widget build(BuildContext context) {

// Use the Todo to create our UI

return new Scaffold(

appBar: new AppBar(

title: new Text("${todo.title}"),

),

body: new Padding(

padding: new EdgeInsets.all(16.0),

child: new Text('${todo.description}'),

),

);

}

}

其中

定义一个Todo 类

class Todo {

final String title;

final String description;

Todo(this.title, this.description);

}

生成一个Todo列表

final todos = new List.generate(

20,

(i) => new Todo(

'Todo $i',

'A description of what needs to be done for Todo $i',

),

);

把Todo List显示到ListView上

new ListView.builder(

itemCount: todos.length,

itemBuilder: (context, index) {

return new ListTile(

title: new Text(todos[index].title),

);

},

);

TodosScreen到DetailScreen

代码中的onTap处,注意传值

new ListView.builder(

itemCount: todos.length,

itemBuilder: (context, index) {

return new ListTile(

title: new Text(todos[index].title),

// When a user taps on the ListTile, navigate to the DetailScreen.

// Notice that we're not only creating a new DetailScreen, we're

// also passing the current todo to it!

onTap: () {

Navigator.push(

context,

new MaterialPageRoute(

builder: (context) => new DetailScreen(todo: todos[index]),

),

);

},

);

},

);

接收值

class DetailScreen extends StatelessWidget {

// Declare a field that holds the Todo

final Todo todo;

// In the constructor, require a Todo

DetailScreen({Key key, @required this.todo}) : super(key: key);

@override

Widget build(BuildContext context) {

// Use the Todo to create our UI

return new Scaffold(

appBar: new AppBar(

title: new Text("${todo.title}"),

),

body: new Padding(

padding: new EdgeInsets.all(16.0),

child: new Text('${todo.description}'),

),

);

}

}

中的

final Todo todo;

DetailScreen({Key key, @required this.todo}) : super(key: key);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值