• 导航参数传递


    代码:

    import 'package:flutter/material.dart';

    class Product{
    final String title;//标题
    final String desciption;//描述
    Product(this.title,this.desciption);//构造函数
    }


    void main() {
    runApp(MaterialApp(
    title: 'xxxxx',
    home: ProductList(
    productLists:List.generate(20, (i)=>Product('商品 $i','这是一个商品详情,编号 $i')))
    ),
    );
    }
    class ProductList extends StatelessWidget {
    final List<Product> productLists;
    ProductList({Key key,@required this.productLists}):super(key:key);


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

    class ProductDetail extends StatelessWidget {
    final Product productDetail;
    ProductDetail({Key key,@required this.productDetail}) : super(key: key);

    @override
    Widget build(BuildContext context) {
    return Scaffold(
    appBar: AppBar( title: Text('${productDetail.title}')),
    body: Center(
    child: Text('${productDetail.desciption}')
    )
    );
    }
    }
     
    总结:

    //导航传参

    //创建一个类—下一级的类

    class xxx extends StatelessWidget {

    final 模型1 模型对象1

    创建的类要接受的参数

    xxx({Key key @required this.模型对象1}):super(key : key);

     

     

     

    }

    // 创建模型

    class 模型1 {

    final string 属性1

    final string 属性2

    模型1(this.属性1,this.属性2)构造函数

    }

     

     

     

    //创建上一级的类——要压栈的类

    class yyy extends statelessWidget{

    final List<模型1> 模型对象2

    yyy{{Key key @required this.模型对象2}}:super(key:key)

     

    }

     

    //此处很微妙 模型对象1 是要传入下级页面生产的模型 ,模型对象2是当前页面要传入的数据模型

    Navigator.push(context,MateriaPageRoute){

        builder:(context)=>xxx(模型对象1:模型对象2)

    }

  • 相关阅读:
    大三学习进度55
    大三学习进度56
    深入理解Java:注解(Annotation)自定义注解入门
    Django2实战示例 第三章 扩展博客功能
    Django2实战示例 第二章 增强博客功能
    12月07日总结观影数据集之大数据分析数据清洗
    12月06日总结
    12月04日阅读笔记
    12月03日总结
    12月05日总结
  • 原文地址:https://www.cnblogs.com/pp-pping/p/12166637.html
Copyright © 2020-2023  润新知