Flutter 页面之间传值— —push (以及返回值,类似与Android里面的OnActivityForResult)

 先上代码:

import 'package:flutter/material.dart';

void main() => runApp(new MaterialApp(
      title: "ArticlePage",
      home: new ArticleListScreen(),
    ));

class ArticleListScreen extends StatelessWidget {
  final List<Article> articles = new List.generate(
      10,
      (i) => new Article(
          title: 'Article $i',
          content: 'Article $i: The quick brown fox jumps over the lazy dog.'));

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('Article List'),
      ),
      body: new ListView.builder(
          itemCount: articles.length,
          itemBuilder: (context, index) {
            return new ListTile(
              title: new Text(articles[index].title),
              onTap: () async {
                String result = await Navigator.push(
                    context,
                    new MaterialPageRoute(
                        builder: (context) =>
                            new ContentScreen(articles[index])));
                if (result != null) {
                  Scaffold.of(context).showSnackBar(new SnackBar(
                      content: new Text("$result"),
                      duration: const Duration(seconds: 1)));
                }
              },
            );
          }),
    );
  }
}

class ContentScreen extends StatelessWidget {
  final Article article;

  ContentScreen(this.article);

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('${article.title}'),
      ),
      body: new Padding(
        padding: new EdgeInsets.all(15.0),
        child: new Column(
          children: <Widget>[
            new Text('${article.content}'),
            new Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: <Widget>[
                new RaisedButton(
                  onPressed: () {
                    Navigator.pop(context, 'Like');
                  },
                  child: new Text('Like'),
                ),
                new RaisedButton(
                  onPressed: () {
                    Navigator.pop(context, 'Unlike');
                  },
                  child: new Text("Unlike"),
                )
              ],
            )
          ],
        ),
      ),
    );
  }
}

class Article {
  String title;
  String content;

  Article({this.title, this.content});
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值