Flutter dialog嵌套ListView

- 日理万妓 2021-08-28 00:17 561阅读 0赞

实现效果如下:

在这里插入图片描述
这里主要是实现弹框中的内容,因为不确定个数,又不能限制死宽高,百度了很久,搜了都是给定了一个宽高实现都,索性自己试了又试,终于弄了出来,故特意记录一下,粗略代码如下:

代码:

  1. ///
  2. /// 功能:
  3. /// 描述:运费规则dialog
  4. /// crated by xudailong on 2020/3/8.
  5. ///
  6. class OrderFreightDialog extends Dialog{
  7. //标题默认高度
  8. double defaultTitleHeight = 40.0;
  9. List<FreightTplInfos> freightList = new List();
  10. OrderFreightDialog({
  11. Key key,
  12. @required this.freightList,
  13. }) : super(key: key);
  14. @override
  15. Widget build(BuildContext context) {
  16. return new Padding(
  17. padding: const EdgeInsets.all(10.0),
  18. child: new Material(
  19. type: MaterialType.transparency,
  20. child: new Column(
  21. mainAxisAlignment: MainAxisAlignment.center,
  22. children: <Widget>[
  23. //白色背景
  24. new Container(
  25. decoration: ShapeDecoration(
  26. color: Color(0xffffffff),
  27. shape: RoundedRectangleBorder(
  28. borderRadius: BorderRadius.all(
  29. Radius.circular(8.0),
  30. ),
  31. ),
  32. ),
  33. margin: const EdgeInsets.all(20),
  34. padding: const EdgeInsets.only(bottom: 20),
  35. child: new Column(
  36. children: <Widget>[
  37. //标题
  38. new Padding(
  39. padding: const EdgeInsets.all(10.0),
  40. child: Container(
  41. height: defaultTitleHeight,
  42. child: Center(
  43. child: new Text(
  44. '运费模板',
  45. style: new TextStyle(fontSize: 16.0, color: Color(0xff666666)),
  46. ),
  47. ),
  48. ),
  49. ),
  50. (freightList==null || freightList.length ==0)?Container():SingleChildScrollView(
  51. child: ListView(
  52. shrinkWrap: true,
  53. children: _buildFirstWidget(),
  54. ),
  55. ),
  56. //底部的分隔线
  57. GestureDetector(
  58. child: Container(
  59. alignment: Alignment.center,
  60. child: Text('知道了',style: TextStyle(fontSize: MyDimen.dp16,color: MyColor.norFontColor,fontFamily: MyFont.nboldFont),),
  61. height: MyDimen.dp40,
  62. width: MyDimen.dp235,
  63. decoration: BoxDecoration(
  64. border: Border.all(color: MyColor.norFontColor,width: MyDimen.dot5),
  65. borderRadius: BorderRadius.all(Radius.circular(40))
  66. ),
  67. ),
  68. onTap: (){
  69. Navigator.of(context).pop();
  70. },
  71. behavior: HitTestBehavior.opaque,
  72. )
  73. ],
  74. ),
  75. ),
  76. ],
  77. ),
  78. ),
  79. );
  80. }
  81. List<Widget> _buildFirstWidget(){
  82. List<Widget> data = new List();
  83. for(int i=0;i<freightList.length;i++){
  84. Widget firstContent = Container(
  85. padding: EdgeInsets.only(left: MyDimen.dp20,right: MyDimen.dp20,bottom: MyDimen.dp20),
  86. child: Column(
  87. crossAxisAlignment: CrossAxisAlignment.start,
  88. children: <Widget>[
  89. Container(
  90. child: Text.rich(
  91. TextSpan(
  92. text: '${freightList[i].titel}',
  93. style: TextStyle(
  94. fontSize: MyDimen.dp12,
  95. color: MyColor.subFontColor,
  96. fontWeight: FontWeight.w700),
  97. children: <TextSpan>[
  98. TextSpan(
  99. text: ' ${freightList[i].totalWeightConent}',
  100. style: TextStyle(
  101. fontSize: MyDimen.dp12,
  102. color: MyColor.norFontColor),
  103. ),
  104. ]),
  105. ),
  106. color: MyColor.divideLightLine
  107. ),
  108. SingleChildScrollView(
  109. scrollDirection: Axis.horizontal,
  110. child:Container(
  111. child: Row(
  112. children: _buildImgs(freightList[i].imgs),
  113. ),
  114. ),
  115. ),
  116. ],
  117. ),
  118. );
  119. data.add(firstContent);
  120. }
  121. return data;
  122. }
  123. List<Widget> _buildImgs(List<String> imgs) {
  124. List<Widget> mContent = new List();
  125. if(imgs == null || imgs.length==0){
  126. mContent.add(Container());
  127. return mContent;
  128. }
  129. for(int i=0;i<imgs.length;i++){
  130. Widget img = Container(
  131. child:ClipRRect(
  132. child: FadeInImage.assetNetwork(placeholder: ImageUtil.getLocalPath(ImageConstant.default_img), image: '${imgs[i]}',fit: BoxFit.cover,),
  133. borderRadius: BorderRadius.all(Radius.circular(6)),
  134. ),
  135. width: MyDimen.dp80,
  136. height: MyDimen.dp80,
  137. margin: EdgeInsets.only(right: MyDimen.dp12,top: 15,bottom: 15),
  138. );
  139. mContent.add(img);
  140. }
  141. return mContent;
  142. }
  143. }

更多文章可查看个人博客主页

徐代龙的技术专栏

Github搭建个人博客(2019最新版,亲测)

发表评论

表情:
评论列表 (有 0 条评论,561人围观)

还没有评论,来说两句吧...

相关阅读