仅将_id传递给下一个屏幕颤动

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了仅将_id传递给下一个屏幕颤动相关的知识,希望对你有一定的参考价值。

[我有一个项目列表,当我单击一个项目时,它将转到下一个屏幕,因此当我尝试传递给它时,我可以显示所单击项目的相应详细信息,将整个对象数据传递给下一个页面

So this is the first screen,when i click on first card item "Receptio Area" it will redirect to next page

This is the second page

这是首页代码颤振视图

         return Column(
            children: <Widget>[
              GestureDetector(
                onTap: () {
                  Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (context) => ImageScreen(data.Area_Id)));
                },
                child: Card(
                  elevation: 15,
                  child: Column(
                    mainAxisSize: MainAxisSize.max,
                    children: <Widget>[
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: <Widget>[
                          Expanded(
                            flex: 3,
                            child: Padding(
                              padding: EdgeInsets.all(30),
                              child: Text(
                                data.Area_Name.toString(),
                                style: TextStyle(
                                    fontWeight: FontWeight.bold,
                                    fontSize: 14,
                                    color: Colors.black87),
                              ),
                            ),
                          ),

                        ],
                      )
                    ],
                  ),
                ),
              ),

  Future<String> getDailyList() async {
    Future dailystatus = SharedPrefrence().getbranchId();
    Future daily = SharedPrefrence().getsectionId();
    Future token = SharedPrefrence().getToken();

    dailystatus.then((data) async {
      var branchid = data;

      daily.then((data) async {
        var section_id = data;

        token.then((data) async {
          var token = data;
          var response = await http.post(Urls.BRANCH_MAPPER,
              headers: {
                "Content-Type": "application/json",
                "Authorization": "Bearer $token",
              },
              body: json.encode({
                "Report_Time_Type": "1",
                "Branch_Id": branchid,
                "Section_Id": section_id,
              }));


          print('Respone ${response.body}');
          if (response.statusCode == 200) {
            try {
              var resp = response.body;
              Map<String, dynamic> value = json.decode(resp);
              var report = value['AreaList'];
              for (int i = 0; i < report.length; i++) {
                var data = report[i];
                var areaName = data["Area_Id"]["Area_Name"];
                Dailylist.add(DailyModel.fromJson(data, areaName));
              }
              setState(() {
                array_lenth = Dailylist.length;
              });
            } catch (e) {
              e.toString();
            }
          }
        });
      });
    });
  }
}

和此模型的第一页

   class  DailyModel {

      String id;
      String From_Time;
      String To_Time;
      String Area_Name;
      String WeakDay;
      String Report_Time_Type;
      String Area_Id;

      DailyModel({this.id,this.From_Time,this.To_Time,this.Area_Name,this.WeakDay,this.Report_Time_Type,this.Area_Id});

      DailyModel.fromJson(json, areaName)
          : id = json['_id'].toString(),
            From_Time = json['From_Time'].toString(),
            To_Time = json['To_Time'].toString(),
            WeakDay = json['WeakDay'].toString(),
            Area_Id = json['_Id'].toString(),
            Report_Time_Type = json['Report_Time_Type'].toString(),
            Area_Name = areaName;
    }

第二页颤振视图

class ImageScreen extends StatelessWidget {
  String area_id = "";
  ImageScreen(this.area_id);

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return ImageScreenState(area_id);
  }
}

class ImageScreenState extends StatefulWidget {
  String area_id = "";

  ImageScreenState(this.area_id);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return ImagesScreenStateful(area_id);
  }
}

class ImagesScreenStateful extends State<ImageScreenState> {
  String area_id = "";

  ImagesScreenStateful(this.area_id);

  var rating = 0.0;
  List Imagelist = List<ImageModel>();
  int array_lenth = 0;

  void initState() {
    getImageList(area_id);
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text(
          'Images',
          style: TextStyle(fontWeight: FontWeight.bold, color: Colors.white),
        ),
        backgroundColor: Colors.deepOrange,
      ),
      body: Container(
        padding: EdgeInsets.all(10),
        child: ListView.separated(
          shrinkWrap: true,
          separatorBuilder: (BuildContext context, int index) =>
              const Divider(),
//          itemCount: 5,
          itemCount: array_lenth,
          itemBuilder: (BuildContext context, int index) {
            ImageModel data = Imagelist[index];
            return Card(
              elevation: 8,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(15.0),
              ),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: <Widget>[
                  CircleAvatar(
                    backgroundImage:
                    NetworkImage(data.image_path.toString()),
                    radius: 40,
                  ),
                  Row(
                    children: <Widget>[
                      Column(
                        children: <Widget>[
                          Padding(
                            padding: EdgeInsets.only(top: 8, right: 180),
                            child: Text(
                             data.Name,
                              style: TextStyle(
                                fontSize: 15,
                                color: Colors.deepOrange[700],
                              ),
                              textAlign: TextAlign.left,
                            ),
                          ),
                          Padding(
                            padding: EdgeInsets.only(top: 8, right: 130),
                            child: Text(
                              data.Area_Name.toString(),
                              style: TextStyle(
                                fontSize: 13,
                                color: Colors.black87,
                                fontWeight: FontWeight.bold,
                              ),
                              textAlign: TextAlign.left,
                            ),
                          ),
                          Padding(
                            padding: EdgeInsets.only(top: 8, right: 70),
                            child: Text(
                              data.Created_At,
                              style: TextStyle(
                                fontSize: 13,
                                color: Colors.blueGrey,
                              ),
                              textAlign: TextAlign.left,
                            ),
                          ),

                        ],
                      ),
                    ],
                  ),
                ],
              ),
            );
          },
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          Navigator.push(
            context,
            MaterialPageRoute(builder: (context) => TakePictureScreen()),
          );
        },
        child: Icon(Icons.camera_alt),
        backgroundColor: Colors.deepOrangeAccent[700],
      ),
    );
  }

  Future<String> getImageList(String area_id) async {

    print("resp:" + area_id);
    Future token = SharedPrefrence().getToken();

    token.then((data) async {
      var token = data;
      var response = await http.post(Urls.GetByArea,
          headers: {
            "Content-Type": "application/json",
            "Authorization": "Bearer $token",
          },
          body: json.encode({
            "Area_Id": area_id,
          }));
//        print('Respone ${response.body}');

      print('Respone ${response.body}');
      if (response.statusCode == 200) {
        try {
          var resp = response.body;
          Map<String, dynamic> value = json.decode(resp);
          var report = value['Image_List'];
          for (int i = 0; i < report.length; i++) {
            var data = report[i];
            var areaName = data["Area_Id"]["Area_Name"];
            var branchName = data["Branch_Id"]["Name"];
            Imagelist.add(ImageModel.fromJson(data, areaName,branchName));
          }
          setState(() {
            array_lenth = Imagelist.length;
          });
        } catch (e) {
          e.toString();
        }
      }
    });
  }
}

第二页模型

class  ImageModel {

  String id;
  String image_path;
  String Area_Name;
  String Name;
  String Created_At;
  String image_temp_path;


  ImageModel({this.id,this.image_path,this.Created_At,this.Area_Name,this.Name,this.image_temp_path});

  ImageModel.fromJson(json, areaName,branchName)
      : id = json['_id'].toString(),
        image_path = json['image_path'].toString(),
        image_temp_path = json['image_temp_path'].toString(),
        Created_At = json['Created_At'].toString(),
        Name = branchName,
        Area_Name = areaName;
} 
答案

我确实认为这是正确的ImageModel data = Imagelist[index];

以上是关于仅将_id传递给下一个屏幕颤动的主要内容,如果未能解决你的问题,请参考以下文章

在颤动中使用 StreamBuilder 时将数据传递到下一个屏幕

如何在颤动中将列表数据从一个屏幕传递到另一个屏幕(StatefulWidget)

如何使用 BLoC 在颤动的屏幕之间传递数据?

如何在颤动中将数据从一个小部件传递到另一个小部件?

如何在颤动的 5 个元素的列中仅将一个元素居中

如何在javascript fetch中存储访问令牌并传递给下一个api调用