RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1071661
Accepted
Денис Акулов
Денис Акулов
Asked:2020-01-19 17:09:17 +0000 UTC2020-01-19 17:09:17 +0000 UTC 2020-01-19 17:09:17 +0000 UTC

Flutter 在单独的小部件中填充列表视图项

  • 772

告诉我如何填写我在单独的小部件中拥有的项目,或者我需要在那里

Group84ItemWidget()

在此处输入图像描述

class OrderTestWidget extends StatefulWidget {

  OrderTestWidget({Key key, this.title}) : super(key: key);

  final String title;
  @override
  _OrderTestWidgetState createState() => _OrderTestWidgetState();


}


class _OrderTestWidgetState extends State<OrderTestWidget> {


  var breedList = Order();
  void getCatData() async {
    var catJson = await CatAPI().getCatBreeds();
    print(catJson);
    var catMap = json.decode(catJson);
        print(catMap);
    setState(() {
      breedList = Order.fromJson(catMap);

      print(breedList.orderdata);  
    });
  }

  @override
  void initState() {
    super.initState();
    getCatData();
  }

  @override
  Widget build(BuildContext context) { 
    return Scaffold(
      extendBodyBehindAppBar: true,
      appBar: AppBar(
        title: Text(
          AppLocalizations.of(context).ordertestwidgetTitle,
          textAlign: TextAlign.center,
          style: TextStyle(
            color: AppColors.secondaryText,
            fontWeight: FontWeight.w400,
            fontSize: 21,
          ),
        ),
        automaticallyImplyLeading: false,
        backgroundColor: Color.fromARGB(255, 0, 163, 238),
      ),
      body: Container(
        constraints: BoxConstraints.expand(),
        decoration: BoxDecoration(
          color: Color.fromARGB(0, 0, 0, 0),
        ),
        child: Column(       
          crossAxisAlignment: CrossAxisAlignment.stretch,

          children: [     
            Container(
              width: MediaQuery.of(context).size.width,
              height: MediaQuery.of(context).size.height,
              child:  ListView.builder(
              itemCount: (breedList == null ||
              breedList.orderdata == null ||
              breedList.orderdata.length == 0)
              ? 0
              : breedList.orderdata.length,
              //  itemCount: 15,
              itemBuilder: (context, index) {

              return GestureDetector(

                onTap: () {
                Navigator.push(context, MaterialPageRoute(builder: (context) {
                return OrderDetaliWidget(catId: breedList.orderdata[index].id,catBreed: breedList.orderdata[index].name);
                }));},

                child: Card (child: Group84ItemWidget(),),

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

模型

@JsonSerializable()
class Order {
  String apistatus;
  String apimessage;
  List<OrderData> orderdata;
  Order({this.apistatus,this.apimessage,this.orderdata});

  factory Order.fromJson(Map<String, dynamic> parsedJson) {
  var list = parsedJson['data'] as List;
   // print(list.toString());
    List<OrderData> dataList = list.map((i) => OrderData.fromJson(i)).toList();        
    return Order( 
           apistatus:parsedJson['api_status'].toString(),
           apimessage:parsedJson['api_message'],    
           orderdata: dataList
           );         
      }
}

这里的 Group84ItemWidget 只是静态填充的左侧数据,我需要从请求中用数据填充它们,而 Group84ItemWidget 也指 Group81ItemWidget 有一个 gridview with users

class Group84ItemWidget extends StatelessWidget { 
  void onButtonPressed(BuildContext context) { 
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      constraints: BoxConstraints.expand(height: 300),
      decoration: BoxDecoration(
       // color: AppColors.primaryText,
        boxShadow: [
         // Shadows.secondaryShadow,
        ],
        borderRadius: BorderRadius.all(Radius.circular(35)),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          //заголовок
          Align(
            alignment: Alignment.topLeft,
            child: Container(
              margin: EdgeInsets.only(left: 11, top: 12),
              child: Text(
                AppLocalizations.of(context).group84itemwidgetTextTirleText,
                textAlign: TextAlign.left,
                style: TextStyle(
                  color: Color.fromARGB(255, 0, 0, 0),
                  fontWeight: FontWeight.w400,
                  fontSize: 16,
                ),
              ),
            ),
          ),
          Align(
            //текст описания задания
            alignment: Alignment.topLeft,
            child: Container(
              width: 350,
              height: 75,
              margin: EdgeInsets.only(left: 11, top: 5),
              child: Text(
                AppLocalizations.of(context).group84itemwidgetTextSubtitleText,
                textAlign: TextAlign.left,
                style: TextStyle(
                  color: Color.fromARGB(255, 0, 0, 0),
                  fontWeight: FontWeight.w400,
                  fontSize: 11,
                  height: 1.2,
                ),
              ),
            ),
          ),
          Container(
            height: 90,
            margin: EdgeInsets.only(left: 11, top: 8, right: 8),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                //григ с воркерами
                Align(
                  alignment: Alignment.topLeft,
                  child: Container(
                    width: 300,
                    height: 90,
                    child: GridView.builder(
                      scrollDirection: Axis.horizontal,
                      gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
                        maxCrossAxisExtent: 90.42969,
                        childAspectRatio: 1.48245,
                        mainAxisSpacing: 10,
                      ),
                      itemBuilder: (context, index) => Group81ItemWidget(),
                    ),
                  ),
                ),
                Expanded(
                  flex: 1,

                  child: Align(
                    alignment: Alignment.topLeft,

                    //высота контейнера с ценой
                    child: Container(              
                     // color: AppColors.color4,
                      height: 70,
                      margin: EdgeInsets.only(left: 15, top: 0),
                      child: Column(                     
                        crossAxisAlignment: CrossAxisAlignment.stretch,
                        children: [
                          Align(
                            //текст цена за час
                            alignment: Alignment.topRight,
                            child: Container(
                              width: 68,
                              height: 30,
                              child: Text(
                                AppLocalizations.of(context).group84itemwidgetBatttonAddText,
                                textAlign: TextAlign.center,
                                style: TextStyle(
                                  color: Color.fromARGB(255, 0, 0, 0),
                                  fontWeight: FontWeight.w400,
                                  fontSize: 28,
                                ),
                              ),
                            ),
                          ),
                          //текст час или день
                          Align(
                            alignment: Alignment.center,
                            child: Container(
                              width: 50,

                              child: Text(
                                AppLocalizations.of(context).group84itemwidgetLabelText,
                                textAlign: TextAlign.center,
                                style: TextStyle(
                                  color: Color.fromARGB(255, 0, 0, 0),
                                  fontWeight: FontWeight.w400,
                                  fontSize: 24,
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
          Expanded(
            flex: 1,
            child: Container(
              margin: EdgeInsets.only(left: 15, top: 16, right: 8, bottom: 14),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: [
                  Align(
                    alignment: Alignment.bottomLeft,
                    child: Container(
                      width: 206,
                      height: 48,
                      margin: EdgeInsets.only(bottom: 7),
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.end,
                        crossAxisAlignment: CrossAxisAlignment.stretch,
                        children: [
                          Align(
                            alignment: Alignment.topLeft,
                            child: Container(
                              width: 206,
                              height: 20,
                              margin: EdgeInsets.only(bottom: 10),
                              child: Stack(
                                alignment: Alignment.center,
                                children: [
                                  Positioned(
                                    left: 19,
                                    bottom: 4,
                                    child: Text(
                                      AppLocalizations.of(context).group84itemwidgetTextAdressText,
                                      textAlign: TextAlign.left,
                                      style: TextStyle(
                                        color: AppColors.black,
                                        fontWeight: FontWeight.w400,
                                        fontSize: 16,
                                      ),
                                    ),
                                  ),
                                  Positioned(
                                    left: 0,
                                    bottom: 8,
                                    child: Image.asset(
                                      "assets/images/path-285.png",
                                      fit: BoxFit.none,
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          ),
                          Align(
                            alignment: Alignment.topLeft,
                            child: Container(
                              width: 191,
                              height: 16,
                              margin: EdgeInsets.only(left: 3),
                              child: Row(
                                crossAxisAlignment: CrossAxisAlignment.stretch,
                                children: [
                                  Align(
                                    alignment: Alignment.bottomLeft,
                                    child: Container(
                                      width: 17,
                                      height: 17,
                                      child: Image.asset(
                                        "assets/images/path-287.png",
                                        fit: BoxFit.none,
                                      ),
                                    ),
                                  ),
                                  Align(
                                    alignment: Alignment.bottomLeft,
                                    child: Container(
                                      width: 84,
                                      height: 15,
                                      margin: EdgeInsets.only(left: 8, bottom: 1),
                                      child: Text(
                                        AppLocalizations.of(context).group84itemwidgetTextDateText,
                                        textAlign: TextAlign.left,
                                        style: TextStyle(
                                          color: AppColors.black,
                                          fontWeight: FontWeight.w400,
                                          fontSize: 15,
                                        ),
                                      ),
                                    ),
                                  ),
                                  Align(
                                    alignment: Alignment.bottomLeft,
                                    child: Container(
                                      width: 17,
                                      height: 17,
                                      margin: EdgeInsets.only(left: 14),
                                      child: Image.asset(
                                        "assets/images/time.png",
                                        fit: BoxFit.none,
                                      ),
                                    ),
                                  ),
                                  //текст время
                                  Align(
                                    alignment: Alignment.bottomLeft,
                                    child: Container(
                                      width: 45,
                                      margin: EdgeInsets.only(left: 6, bottom: 1),
                                      child: Text(
                                        AppLocalizations.of(context).group84itemwidgetTextTimeText,
                                        textAlign: TextAlign.left,
                                        style: TextStyle(
                                          color: AppColors.black,
                                          fontWeight: FontWeight.w400,
                                          fontSize: 15,
                                        ),
                                      ),
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                  Spacer(),
                  Align(
                    alignment: Alignment.bottomLeft,
                    child: Container(
                      width: 55,
                      height: 55,
                      margin: EdgeInsets.only(right: 13),
                      child: Stack(
                        alignment: Alignment.center,
                        children: [
                          Positioned(
                            right: 0,
                            bottom: 0,
                            child: Container(
                              width: 55,
                              height: 55,
                              decoration: BoxDecoration(
                                boxShadow: [
                                  //тень круга с воркерами
                                  BoxShadow(
                                    color: Color.fromARGB(0, 0, 0, 0),
                                    offset: Offset(0, 0),
                                    blurRadius: 6,
                                  ),
                                ],
                              ),
                              child: Image.asset(
                                "assets/images/path-301.png",
                                fit: BoxFit.none,
                              ),
                            ),
                          ),
                          Positioned(
                            right: 16,
                            bottom: 23,
                            child: Text(
                              AppLocalizations.of(context).group84itemwidgetTextWorkerText,
                              textAlign: TextAlign.left,
                              style: TextStyle(
                                color: AppColors.primaryText,
                                fontWeight: FontWeight.w400,
                                fontSize: 24,
                              ),
                            ),
                          ),
                          Positioned(
                            right: 6,
                            bottom: 12,
                            child: Text(
                              AppLocalizations.of(context).group84itemwidgetTextWorkerCountText,
                              textAlign: TextAlign.left,
                              style: TextStyle(
                                color: AppColors.primaryText,
                                fontWeight: FontWeight.w400,
                                fontSize: 12,
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                  Align(
                    alignment: Alignment.topLeft,
                    child: Container(
                      width: 66,
                      height: 44,
                      margin: EdgeInsets.only(left: 13, top: 4),
                      child: FlatButton(
                        onPressed: () => this.onButtonPressed(context),
                        color: AppColors.primaryElement,
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.all(Radius.circular(20)),
                        ),
                        textColor: Color.fromARGB(0, 0,0, 0),
                        padding: EdgeInsets.all(0),
                        child: Text(
                          AppLocalizations.of(context).group84itemwidgetButtonButton,
                          textAlign: TextAlign.left,
                          style: TextStyle(
                            color: Color.fromARGB(255, 255, 255, 255),
                            fontWeight: FontWeight.w400,
                            fontSize: 14,
                          ),
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}
listview
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    MiT
    2020-01-19T19:26:02Z2020-01-19T19:26:02Z

    希望我正确理解了一切......

    奇怪的问题,因为你已经用OrderDetaliWidget(). 应用程序的架构非常繁重,随着时间的推移将无法维护。我假设您是初学者,并且在学习的同时正在做一个项目。这里有一些材料可以帮助你。


    替换OrderTestWidget()为Group84ItemWidget()以Group84ItemWidget(breedList.orderdata[index])使其按索引接受我们的模型:

    class _OrderTestWidgetState extends State<OrderTestWidget> {
      // ...
      @override
      Widget build(BuildContext context) {
        // ...
        child: ListView.builder(
          // ...
            itemBuilder: (context, index) {
              return GestureDetector(
                // ...
                child: Card(
                  child: Group84ItemWidget(breedList.orderdata[index]),
                 ),
               );
             }
      }
    }
    

    添加了一个Group84ItemWidget构造函数,在实例化时需要我们的模型。并且模型本身被添加为一个字段。现在,在此类中的任何位置,您都可以使用特定模型的数据:

    class Group84ItemWidget extends StatelessWidget {
      Group84ItemWidget(this.item);
      final Order item;
    
      @override
      Widget build(BuildContext context) {
        // ...
        Text(item.apimessage);
        // ...
      }
    }
    

    Group84ItemWidget在s中做同样的事情Group81ItemWidget。在这种情况下,连接将是这样的:OrderTestWidget -> Group84ItemWidget -> Group81ItemWidget

    • 0

相关问题

  • Flutter ListView 全屏

  • 在应用关闭时保存列表视图数据

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    如何从列表中打印最大元素(str 类型)的长度?

    • 2 个回答
  • Marko Smith

    如何在 PyQT5 中清除 QFrame 的内容

    • 1 个回答
  • Marko Smith

    如何将具有特定字符的字符串拆分为两个不同的列表?

    • 2 个回答
  • Marko Smith

    导航栏活动元素

    • 1 个回答
  • Marko Smith

    是否可以将文本放入数组中?[关闭]

    • 1 个回答
  • Marko Smith

    如何一次用多个分隔符拆分字符串?

    • 1 个回答
  • Marko Smith

    如何通过 ClassPath 创建 InputStream?

    • 2 个回答
  • Marko Smith

    在一个查询中连接多个表

    • 1 个回答
  • Marko Smith

    对列表列表中的所有值求和

    • 3 个回答
  • Marko Smith

    如何对齐 string.Format 中的列?

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5