flutter - 如何将数据从 ListView 传递到下一个屏幕,如 flutter 中的图像

标签 flutter dart

我需要将字符串和图像从 ListView 传递到下一个屏幕,并且该字符串和图像在 ListView 中 我应该如何通过它们。

我在 categories.dart 中有项目 import 'package:flutter/material.dart';## 标题 ##

List categories = [
  {
    "name": "Indian",
    "color1": Color.fromARGB(100, 0, 0, 0),
    "color2": Color.fromARGB(100, 0, 0, 0),
    "img": "assets/indian.jpg"},
  {
    "name": "Italian",
    "color1": Color.fromARGB(100, 0, 0, 0),
    "color2": Color.fromARGB(100, 0, 0, 0),
    "img": "assets/food1.jpeg"
  },
  {
    "name": "Chinese",
    "color1": Color.fromARGB(100, 0, 0, 0),
    "color2": Color.fromARGB(100, 0, 0, 0),
    "img": "assets/food2.jpeg"
  },
  {
    "name": "Nigerian",
    "color1": Color.fromARGB(100, 0, 0, 0),
    "color2": Color.fromARGB(100, 0, 0, 0),
    "img": "assets/food3.jpeg"
  },
  {
    "name": "Spanish",
    "color1": Color.fromARGB(100, 0, 0, 0),
    "color2": Color.fromARGB(100, 0, 0, 0),
    "img": "assets/food4.jpeg"
  },
  {
    "name": "Mexican",
    "color1": Color.fromARGB(100, 0, 0, 0),
    "color2": Color.fromARGB(100, 0, 0, 0),
    "img": "assets/food5.jpeg"
  },

];
  1. 我的 ListView 在第一个屏幕上是 home.dart
     Container(
              height: MediaQuery.of(context).size.height/6,
              child: ListView.builder(
                primary: false,
                scrollDirection: Axis.horizontal,
                shrinkWrap: true,
                itemCount: categories == null ? 0:categories.length,
                itemBuilder: (BuildContext context, int index) {
                  Map cat = categories[index];


                  return

                  new InkWell(

                      onTap:() {
                          Navigator.of(context)
                            .push(MaterialPageRoute<Null>(builder: (BuildContext context) {
                            print(cat);
                          return new Manu_screen();

                            }));
                      },

                      child: Padding(
                        padding: EdgeInsets.only(right: 10.0),
                        child: ClipRRect(

                          borderRadius: BorderRadius.circular(8.0),
                          child: Stack(

                            children: <Widget>[

                              Image.asset(
                                cat["img"],
                                height: MediaQuery.of(context).size.height/6,
                                width: MediaQuery.of(context).size.height/6,
                                fit: BoxFit.cover,

                              ),

                              Container(


                                decoration: BoxDecoration(
                                  gradient: LinearGradient(
                                    begin: Alignment.topCenter,
                                    end: Alignment.bottomCenter,
                                    // Add one stop for each color. Stops should increase from 0 to 1
                                    stops: [0.2, 0.7],
                                    colors: [
                                      cat['color1'],
                                      cat['color2'],
                                    ],
                                    // stops: [0.0, 0.1],
                                  ),
                                ),

                                height: MediaQuery.of(context).size.height/6,
                                width: MediaQuery.of(context).size.height/6,

                              ),


                              Center(

                                child: Container(
                                  height: MediaQuery.of(context).size.height/6,
                                  width: MediaQuery.of(context).size.height/6,
                                  padding: EdgeInsets.all(1),

                                  constraints: BoxConstraints(
                                    minWidth: 20,
                                    minHeight: 20,



                                  ),
                                  child: Center(
                                    child: Text(
                                      cat["name"],
                                      style: TextStyle(
                                        color: Colors.white,
                                        fontSize: 20,
                                        fontWeight: FontWeight.bold,

                                      ),
                                      textAlign: TextAlign.center,
                                    ),
                                  ),
                                ),
                              ),

                            ],
                          ),
                        ),
                      )
                  );
                },
              ),

            ),
  1. 我需要将类别中的数据显示到我的第二个屏幕上,我如何获取它

最佳答案

您可以将参数传递给您的第二个屏幕:

Navigator.of(context).push(
    MaterialPageRoute(
        builder: (BuildContext context) {
            print(cat);
            return new Manu_screen(cat);
        }
    )
);
// Snippet from @Can Taşpınar
class Manu_screen extends StatelessWidget {
  final Map cat;

  Manu_screen({Key key, this.cat}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Text(cat["name"]),
    );
  }
}

关于flutter - 如何将数据从 ListView 传递到下一个屏幕,如 flutter 中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56933549/

相关文章:

dart - flutter ,发送参数到前一个屏幕

ios - Flutter::iOS多种方案不同的Firebase项目

flutter - 如何在不识别键的情况下获取列表<map<string,dynamic>数据每个映射键和值

flutter - 使用 `src` 文件夹内的 `lib` 子文件夹有什么好处

dart - 使用 Inherited Widget 时如何处理 bloc?

user-interface - 如何在 Flutter 中更改 radio 的非事件颜色?

android - 如何在 flutter 中使用 chrome 自定义标签页

flutter - 如何在Flutter中增加计数器并禁用按钮?

dictionary - flutter_map插件不会更新当前位置

android - Flutter:删除所有没有路由名称的路由