flutter wrap 流式布局

Posted pp-pping

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了flutter wrap 流式布局相关的知识,希望对你有一定的参考价值。

热门商品代码:

import ‘package:flutter/material.dart‘;

import ‘package:flutter_project/service/service_method.dart‘;
import ‘dart:convert‘;

import ‘package:flutter_screenutil/flutter_screenutil.dart‘;//使用json.decode
class HotGoods extends StatefulWidget {
HotGoods({Key key}) : super(key: key);
 
@override
_HotGoodsState createState() => _HotGoodsState();
}


class _HotGoodsState extends State<HotGoods> {
int page = 1;
List <Map> hotGoodsList = [];
@override
void initState() {
 
super.initState();
_getHotGoods();
 
}
//火爆专区接口
void _getHotGoods(){
getHotGoodSContent(page).then((val){
var data = json.decode(val.toString());
List<Map> newGoodList = (data[‘data‘] as List).cast();//强转类型 从字符型转map
setState(() {
hotGoodsList.addAll(newGoodList);//将新数据newGoodList组合到老数据hotGoodsList,还是返回到老数据hotGoodsList
page++;
});
});

}

Widget hotTitle = Container(
margin: EdgeInsets.only(top: 10.0),//上间距
padding: EdgeInsets.all(5.0),//内间距
alignment: Alignment.center,
decoration: BoxDecoration(
border: Border(bottom:BorderSide(width: 0.5,color: Colors.black12)),
color: Colors.white
),
child: Text(‘火爆专区‘),
);
//火爆专区内容 --- 流式布局
Widget _wrapList(){
if (hotGoodsList.length!=0) {//集合有数据
 
List<Widget> listWidget = hotGoodsList.map((val){//把map集合数据 改成 widget 集合数据 .map 类似于 ForIn
return InkWell(
onTap: (){
print(‘点击了火爆专区的商品‘);
},
child: Container(
width: ScreenUtil().setWidth(372),
padding: EdgeInsets.all(5.0),
margin: EdgeInsets.only(bottom: 3.0),
child: Column(//纵向布局
children: <Widget>[
Image.network(val[‘image‘],width: ScreenUtil().setWidth(375),),
Text(
val[‘name‘],
overflow: TextOverflow.ellipsis,
maxLines: 1,
style: TextStyle(color: Colors.orange,fontSize: ScreenUtil().setSp(26)),//字号
),
Row(//横向布局
mainAxisAlignment: MainAxisAlignment.spaceEvenly,//横向布局 子控件平均分成两份
children: <Widget>[
 
Text(‘¥${val[‘mallPrice‘]}‘),
Text(
‘¥${val[‘price‘]}‘,
style: TextStyle(color: Colors.grey,decoration: TextDecoration.lineThrough),
)
],
 
)
],
),
),
);
 
}).toList();
return Wrap(
spacing: 2,
children: listWidget,
);
} else {
return Text(‘火爆专区无数据‘);
}

}
 

@override
Widget build(BuildContext context) {
 
 
return Container(
child: Column(
children: <Widget>[
hotTitle,
_wrapList()
],
),
);
}
}
 

以上是关于flutter wrap 流式布局的主要内容,如果未能解决你的问题,请参考以下文章

Flutter 流式布局自动换行(WrapFlow)

flutter中流式布局

Flutter 布局- FlowTableWrap详解

Flutter布局类组件

flutter-流式布局

Flutter 学习 布局类Widget