Navigator

Navigator,第1张

import 'package:flutter/material.dart';



void main() {

  runApp(MaterialApp(

    title: "导航01",

    home: new MyApp(),

  ));

}



//页面导航 父子级

class MyApp extends StatelessWidget {

  @override

  Widget build(BuildContext context) {

    // TODO: implement build

    return Scaffold(

      appBar: AppBar(title: Text('导航页面')),

      body: Center(

        child: RaisedButton(

          child: Text('查看页面'),

          onPressed: () {

            Navigator.push(context,

                MaterialPageRoute(builder: (context) => new SecondScreen()));

          },

        ),

      ),

    );

  }

}



class SecondScreen extends StatelessWidget {

  @override

  Widget build(BuildContext context) {

    return Scaffold(

      appBar: AppBar(

        title: Text('页面2'),

      ),

      body: Center(

        child: RaisedButton(

          child: Text('返回'),

          onPressed: () {

            Navigator.pop(context);

          },

        ),

      ),

    );

  }

}

import 'package:flutter/material.dart';

class Product {
  final String title; //商品标题
  final String description; //商品描述
  Product(this.title, this.description);
}

void main() {
  runApp(MaterialApp(
    title: '导航数据传递和 接受',
    home: ProductList(
        products: List.generate(20, (i) => Product('商品$i', '这是一个商品详情编号$i'))),
  ));
}

class ProductList extends StatelessWidget {
  final List products;

  const ProductList({Key? key, required this.products}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(title: Text('商品列表')),
      body: ListView.builder(
        itemCount: products.length,
        itemBuilder: (context, index) {
          return ListTile(
            title: Text(products[index].title),
            onTap: () {
              Navigator.push(
                  context,
                  MaterialPageRoute(
                      builder: (context) =>
                          new ProductDetail(product: products[index])));
            },
          );
        },
      ),
    );
  }
}

class ProductDetail extends StatelessWidget {
  final Product product;

  const ProductDetail({Key? key, required this.product}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new Scaffold(
      appBar: AppBar(
        title: Text('${product.title}'),
      ),
      body: Center(
        child: Text('${product.description}'),
      ),
    );
  }
}
import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    title: '页面返回数据',
    home: FirstPage(),
  ));
}

class FirstPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('数据返回'),
      ),
      body: Center(
        child: RouteButton(),
      ),
    );
  }
}

class RouteButton extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return RaisedButton(
        onPressed: () {
          _navgateToPage(context);
        },
        child: Text('去页面'));
  }
}

//响应事件
_navgateToPage(BuildContext context) async {
  final result = await Navigator.push(
      context, MaterialPageRoute(builder: (context) => PageIndex()));

  Scaffold.of(context).showSnackBar(SnackBar(content: Text('$result')));
}

class PageIndex extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Page页面'),
      ),
      body: Center(
        child: Column(
          children: [
            RaisedButton(
                child: Text('内容1'),
                onPressed: () {
                  Navigator.pop(context, '返回数据:1');
                }),
            RaisedButton(
                child: Text('内容1'),
                onPressed: () {
                  Navigator.pop(context, '返回数据:2');
                }),
          ],
        ),
      ),
    );
  }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/langs/716559.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-25
下一篇 2022-04-25

发表评论

登录后才能评论

评论列表(0条)

保存