簡體   English   中英

我如何獲取列表的信息<map>到 Flutter 中的另一個屏幕?</map>

[英]How can i get the information of a List<Map> to another screen in Flutter?

我是 Flutter 的新手,我正在使用該框架為我的大學開展一個項目。 我有這個使用列表的屏幕,並且正在按我的需要工作:

class BarberPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: BarberList(),
      ),
    );
  }
}

class BarberList extends StatefulWidget {
  BarberList({Key key}) : super(key: key);

  // static final String path = "lib/src/pages/lists/list2.dart";
  _BarberListState createState() => _BarberListState();
}

class _BarberListState extends State<BarberList> {
  final TextStyle dropdownMenuItem =
  TextStyle(color: Colors.black, fontSize: 18);

  final primary = Color(0xFF63736B);
  final secondary = Color(0xFF0C0E0B);
  final green = Color(0xFF79FF00);

  final List<Map> barberLists = [
    {
      "name": "Barbearia do PA",
      "location": "Rua Paraná, 184 - Cidade de Deus",
      "type": "De 09h ás 18h - Seg. à Sex.",
      "phone": "(37) 99122-3338",
      "logoText":
      "https://cdn.pixabay.com/photo/2017/01/13/01/22/rocket-1976107_960_720.png"
    },
    {
      "name": "Barbearia do PA",
      "location": "Rua Paraná, 184 - Cidade de Deus",
      "type": "De 09h ás 18h - Seg. à Sex.",
      "phone": "(37) 99122-3338",
      "logoText":
      "https://cdn.pixabay.com/photo/2017/01/13/01/22/rocket-1976107_960_720.png"
    },
    {
      "name": "Barbearia do PA",
      "location": "Rua Paraná, 184 - Cidade de Deus",
      "type": "De 09h ás 18h - Seg. à Sex.",
      "phone": "(37) 99122-3338",
      "logoText":
      "https://cdn.pixabay.com/photo/2017/01/13/01/22/rocket-1976107_960_720.png"
    },
    {
      "name": "Barbearia do PA",
      "location": "Rua Paraná, 184 - Cidade de Deus",
      "type": "De 09h ás 18h - Seg. à Sex.",
      "phone": "(37) 99122-3338",
      "logoText":
      "https://cdn.pixabay.com/photo/2017/01/13/01/22/rocket-1976107_960_720.png"
    },
    {
      "name": "Barbearia do PA",
      "location": "Rua Paraná, 184 - Cidade de Deus",
      "type": "De 09h ás 18h - Seg. à Sex.",
      "phone": "(37) 99122-3338",
      "logoText":
      "https://cdn.pixabay.com/photo/2017/01/13/01/22/rocket-1976107_960_720.png"
    },
    {
      "name": "Barbearia do PA",
      "location": "Rua Paraná, 184 - Cidade de Deus",
      "type": "De 09h ás 18h - Seg. à Sex.",
      "phone": "(37) 99122-3338",
      "logoText":
      "https://cdn.pixabay.com/photo/2017/01/13/01/22/rocket-1976107_960_720.png"
    },
    {
      "name": "Barbearia do PA",
      "location": "Rua Paraná, 184 - Cidade de Deus",
      "type": "De 09h ás 18h - Seg. à Sex.",
      "phone": "(37) 99122-3338",
      "logoText":
      "https://cdn.pixabay.com/photo/2017/01/13/01/22/rocket-1976107_960_720.png"
    },
    {
      "name": "Barbearia do PA",
      "location": "Rua Paraná, 184 - Cidade de Deus",
      "type": "De 09h ás 18h - Seg. à Sex.",
      "phone": "(37) 99122-3338",
      "logoText":
      "https://cdn.pixabay.com/photo/2017/01/13/01/22/rocket-1976107_960_720.png"
    },
  ];

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: Scaffold(
        backgroundColor: Color(0xfff0f0f0),
        body: SingleChildScrollView(
          child: Container(
            height: MediaQuery
                .of(context)
                .size
                .height,
            width: MediaQuery
                .of(context)
                .size
                .width,
            child: Stack(
              children: <Widget>[
                Container(
                  padding: EdgeInsets.only(top: 145),
                  height: MediaQuery
                      .of(context)
                      .size
                      .height,
                  width: double.infinity,
                  child: ListView.builder(
                      itemCount: barberLists.length,
                      itemBuilder: (BuildContext context, int index) {
                        return buildList(context, index);
                      }),
                ),
                Container(
                  height: 140,
                  width: double.infinity,
                  decoration: BoxDecoration(
                      color: green,
                      borderRadius: BorderRadius.only(
                          bottomLeft: Radius.circular(30),
                          bottomRight: Radius.circular(30))),
                  child: Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 30),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        IconButton(
                          onPressed: () {
                            Navigator.pop(context);
                          },
                          icon: Icon(
                            Icons.arrow_back,
                            color: Colors.white,
                          ),
                        ),
                        Text(
                          "Barbearias",
                          style: TextStyle(color: Colors.white, fontSize: 24),
                        ),
                        IconButton(
                          onPressed: () {},
                          icon: Icon(
                            Icons.filter_list,
                            color: Colors.white,
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
                Container(
                  child: Column(
                    children: <Widget>[
                      SizedBox(
                        height: 110,
                      ),
                      Padding(
                        padding: EdgeInsets.symmetric(horizontal: 20),
                        child: Material(
                          elevation: 5.0,
                          borderRadius: BorderRadius.all(Radius.circular(30)),
                          child: TextField(
                            // controller: TextEditingController(text: locations[0]),
                            cursorColor: Theme
                                .of(context)
                                .primaryColor,
                            style: dropdownMenuItem,
                            decoration: InputDecoration(
                                hintText: "Procurar Barbearias",
                                hintStyle: TextStyle(
                                    color: Colors.black38, fontSize: 16),
                                prefixIcon: Material(
                                  elevation: 0.0,
                                  borderRadius:
                                  BorderRadius.all(Radius.circular(30)),
                                  child: Icon(Icons.search),
                                ),
                                border: InputBorder.none,
                                contentPadding: EdgeInsets.symmetric(
                                    horizontal: 25, vertical: 13)),
                          ),
                        ),
                      ),
                    ],
                  ),
                )
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget buildList(BuildContext context, int index) {
    return Container(
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(25),
        color: Colors.white,
      ),
      width: double.infinity,
      height: 120,
      margin: EdgeInsets.symmetric(vertical: 10, horizontal: 20),
      padding: EdgeInsets.symmetric(vertical: 10, horizontal: 20),
      child: GestureDetector(
        onTap: () {
          Scaffold.of(context).showSnackBar(SnackBar(
            content: Text(index.toString()),
          ));
        },
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Container(
              width: 60,
              height: 60,
              margin: EdgeInsets.only(right: 15, top: 20),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(50),
                border: Border.all(width: 3, color: secondary),
                image: DecorationImage(
                    image: CachedNetworkImageProvider(
                        barberLists[index]['logoText']),
                    fit: BoxFit.fill),
              ),
            ),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text(
                    barberLists[index]['name'],
                    style: TextStyle(
                        color: primary,
                        fontWeight: FontWeight.bold,
                        fontSize: 18),
                  ),
                  SizedBox(
                    height: 6,
                  ),
                  Row(
                    children: <Widget>[
                      Icon(
                        Icons.location_on,
                        color: secondary,
                        size: 20,
                      ),
                      SizedBox(
                        width: 5,
                      ),
                      Text(barberLists[index]['location'],
                          style: TextStyle(
                              color: primary, fontSize: 13, letterSpacing: .3)),
                    ],
                  ),
                  SizedBox(
                    height: 6,
                  ),
                  Row(
                    children: <Widget>[
                      Icon(
                        Icons.calendar_today,
                        color: secondary,
                        size: 20,
                      ),
                      SizedBox(
                        width: 5,
                      ),
                      Text(barberLists[index]['type'],
                          style: TextStyle(
                              color: primary, fontSize: 13, letterSpacing: .3)),
                    ],
                  ),
                  SizedBox(
                    height: 6,
                  ),
                  Row(
                    children: <Widget>[
                      Icon(
                        Icons.call_end,
                        color: secondary,
                        size: 20,
                      ),
                      SizedBox(
                        width: 5,
                      ),
                      Text(barberLists[index]['phone'],
                          style: TextStyle(
                              color: primary, fontSize: 13, letterSpacing: .3)),
                    ],
                  ),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}

我需要通過按下來查看此列表中每個項目的信息,但我不知道如何在新屏幕上獲取信息。 我試圖按照這個例子“ https://flutter.dev/docs/cookbook/navigation/passing-data ”,但它使用了 class 並且在這個列表中不會像這樣工作。 我真的需要這方面的幫助。

謝謝!

您可以使用 Navigator.push
您可以在下面復制粘貼運行完整代碼

代碼片段

Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) => DetailScreen(detailData: barberLists[index]),
            ),
          );
...
class DetailScreen extends StatelessWidget {
  final Map detailData;

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

工作演示

在此處輸入圖像描述

完整代碼

import 'package:flutter/material.dart';
//import 'package:cached_network_image/cached_network_image.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: BarberList(),
    );
  }
}

class BarberList extends StatefulWidget {
  BarberList({Key key}) : super(key: key);

  // static final String path = "lib/src/pages/lists/list2.dart";
  _BarberListState createState() => _BarberListState();
}

class _BarberListState extends State<BarberList> {
  final TextStyle dropdownMenuItem =
  TextStyle(color: Colors.black, fontSize: 18);

  final primary = Color(0xFF63736B);
  final secondary = Color(0xFF0C0E0B);
  final green = Color(0xFF79FF00);

  final List<Map> barberLists = [
    {
      "name": "Barbearia do PA",
      "location": "Rua Paraná, 184 - Cidade de Deus",
      "type": "De 09h ás 18h - Seg. à Sex.",
      "phone": "(37) 99122-3338",
      "logoText":
      "https://cdn.pixabay.com/photo/2017/01/13/01/22/rocket-1976107_960_720.png"
    },
    {
      "name": "Barbearia do PA",
      "location": "Rua Paraná, 184 - Cidade de Deus",
      "type": "De 09h ás 18h - Seg. à Sex.",
      "phone": "(37) 99122-3338",
      "logoText":
      "https://cdn.pixabay.com/photo/2017/01/13/01/22/rocket-1976107_960_720.png"
    },
    {
      "name": "Barbearia do PA",
      "location": "Rua Paraná, 184 - Cidade de Deus",
      "type": "De 09h ás 18h - Seg. à Sex.",
      "phone": "(37) 99122-3338",
      "logoText":
      "https://cdn.pixabay.com/photo/2017/01/13/01/22/rocket-1976107_960_720.png"
    },
    {
      "name": "Barbearia do PA",
      "location": "Rua Paraná, 184 - Cidade de Deus",
      "type": "De 09h ás 18h - Seg. à Sex.",
      "phone": "(37) 99122-3338",
      "logoText":
      "https://cdn.pixabay.com/photo/2017/01/13/01/22/rocket-1976107_960_720.png"
    },
    {
      "name": "Barbearia do PA",
      "location": "Rua Paraná, 184 - Cidade de Deus",
      "type": "De 09h ás 18h - Seg. à Sex.",
      "phone": "(37) 99122-3338",
      "logoText":
      "https://cdn.pixabay.com/photo/2017/01/13/01/22/rocket-1976107_960_720.png"
    },
    {
      "name": "Barbearia do PA",
      "location": "Rua Paraná, 184 - Cidade de Deus",
      "type": "De 09h ás 18h - Seg. à Sex.",
      "phone": "(37) 99122-3338",
      "logoText":
      "https://cdn.pixabay.com/photo/2017/01/13/01/22/rocket-1976107_960_720.png"
    },
    {
      "name": "Barbearia do PA",
      "location": "Rua Paraná, 184 - Cidade de Deus",
      "type": "De 09h ás 18h - Seg. à Sex.",
      "phone": "(37) 99122-3338",
      "logoText":
      "https://cdn.pixabay.com/photo/2017/01/13/01/22/rocket-1976107_960_720.png"
    },
    {
      "name": "Barbearia do PA",
      "location": "Rua Paraná, 184 - Cidade de Deus",
      "type": "De 09h ás 18h - Seg. à Sex.",
      "phone": "(37) 99122-3338",
      "logoText":
      "https://cdn.pixabay.com/photo/2017/01/13/01/22/rocket-1976107_960_720.png"
    },
  ];

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: Scaffold(
        backgroundColor: Color(0xfff0f0f0),
        body: SingleChildScrollView(
          child: Container(
            height: MediaQuery
                .of(context)
                .size
                .height,
            width: MediaQuery
                .of(context)
                .size
                .width,
            child: Stack(
              children: <Widget>[
                Container(
                  padding: EdgeInsets.only(top: 145),
                  height: MediaQuery
                      .of(context)
                      .size
                      .height,
                  width: double.infinity,
                  child: ListView.builder(
                      itemCount: barberLists.length,
                      itemBuilder: (BuildContext context, int index) {
                        return buildList(context, index);
                      }),
                ),
                Container(
                  height: 140,
                  width: double.infinity,
                  decoration: BoxDecoration(
                      color: green,
                      borderRadius: BorderRadius.only(
                          bottomLeft: Radius.circular(30),
                          bottomRight: Radius.circular(30))),
                  child: Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 30),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        IconButton(
                          onPressed: () {
                            Navigator.pop(context);
                          },
                          icon: Icon(
                            Icons.arrow_back,
                            color: Colors.white,
                          ),
                        ),
                        Text(
                          "Barbearias",
                          style: TextStyle(color: Colors.white, fontSize: 24),
                        ),
                        IconButton(
                          onPressed: () {},
                          icon: Icon(
                            Icons.filter_list,
                            color: Colors.white,
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
                Container(
                  child: Column(
                    children: <Widget>[
                      SizedBox(
                        height: 110,
                      ),
                      Padding(
                        padding: EdgeInsets.symmetric(horizontal: 20),
                        child: Material(
                          elevation: 5.0,
                          borderRadius: BorderRadius.all(Radius.circular(30)),
                          child: TextField(
                            // controller: TextEditingController(text: locations[0]),
                            cursorColor: Theme
                                .of(context)
                                .primaryColor,
                            style: dropdownMenuItem,
                            decoration: InputDecoration(
                                hintText: "Procurar Barbearias",
                                hintStyle: TextStyle(
                                    color: Colors.black38, fontSize: 16),
                                prefixIcon: Material(
                                  elevation: 0.0,
                                  borderRadius:
                                  BorderRadius.all(Radius.circular(30)),
                                  child: Icon(Icons.search),
                                ),
                                border: InputBorder.none,
                                contentPadding: EdgeInsets.symmetric(
                                    horizontal: 25, vertical: 13)),
                          ),
                        ),
                      ),
                    ],
                  ),
                )
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget buildList(BuildContext context, int index) {
    return Container(
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(25),
        color: Colors.white,
      ),
      width: double.infinity,
      height: 120,
      margin: EdgeInsets.symmetric(vertical: 10, horizontal: 20),
      padding: EdgeInsets.symmetric(vertical: 10, horizontal: 20),
      child: GestureDetector(
        onTap: () {
          Scaffold.of(context).showSnackBar(SnackBar(
            content: Text(index.toString()),
          ));

          Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) => DetailScreen(detailData: barberLists[index]),
            ),
          );

        },
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Container(
              width: 60,
              height: 60,
              margin: EdgeInsets.only(right: 15, top: 20),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(50),
                border: Border.all(width: 3, color: secondary),
                /*image: DecorationImage(
                    image: CachedNetworkImageProvider(
                        barberLists[index]['logoText']),
                    fit: BoxFit.fill),*/
              ),
            ),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text(
                    barberLists[index]['name'],
                    style: TextStyle(
                        color: primary,
                        fontWeight: FontWeight.bold,
                        fontSize: 18),
                  ),
                  SizedBox(
                    height: 6,
                  ),
                  Row(
                    children: <Widget>[
                      Icon(
                        Icons.location_on,
                        color: secondary,
                        size: 20,
                      ),
                      SizedBox(
                        width: 5,
                      ),
                      Text(barberLists[index]['location'],
                          style: TextStyle(
                              color: primary, fontSize: 13, letterSpacing: .3)),
                    ],
                  ),
                  SizedBox(
                    height: 6,
                  ),
                  Row(
                    children: <Widget>[
                      Icon(
                        Icons.calendar_today,
                        color: secondary,
                        size: 20,
                      ),
                      SizedBox(
                        width: 5,
                      ),
                      Text(barberLists[index]['type'],
                          style: TextStyle(
                              color: primary, fontSize: 13, letterSpacing: .3)),
                    ],
                  ),
                  SizedBox(
                    height: 6,
                  ),
                  Row(
                    children: <Widget>[
                      Icon(
                        Icons.call_end,
                        color: secondary,
                        size: 20,
                      ),
                      SizedBox(
                        width: 5,
                      ),
                      Text(barberLists[index]['phone'],
                          style: TextStyle(
                              color: primary, fontSize: 13, letterSpacing: .3)),
                    ],
                  ),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}

class DetailScreen extends StatelessWidget {
  // Declare a field that holds the Todo.
  final Map detailData;

  // In the constructor, require a Todo.
  DetailScreen({Key key, @required this.detailData}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // Use the Todo to create the UI.
    return Scaffold(
      appBar: AppBar(
        title: Text(detailData["name"]),
      ),
      body: Padding(
        padding: EdgeInsets.all(16.0),
        child: Text(detailData["location"]),
      ),
    );
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM