我在容器上显示了100多个图像(每个图像的大小不超过100kb)。 从资产加载图像时,我在dart工具(内存模式)中看到图像对象+编解码器对象占用约225 mb的内存。我不知道为什么。 添加用于加载图片的代码段。
import 'package:flutter/material.dart';
class HomePage extends StatefulWidget{
@override
State<StatefulWidget> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
static const List<List<String>> _imageList = <List<String>>[
<String>['wild', '6D', '7D', '8D', '9D', '10D', 'QD', 'KD', 'AD', 'wild'],
<String>['5D', '3H', '2H', '2S', '3S', '4S', '5S', '6S', '7S', 'AC'],
<String>['4D', '4H', 'KD', 'AD', 'AC', 'KC', 'QC', '10C', '8S', 'KC'],
<String>['3D', '5H', 'QD', 'QH', '10H', '9H', '8H', '9C', '9S', 'QC'],
<String>['2D', '6H', '10D', 'KH', '3H', '2H', '7H', '8C', '10S', '10C'],
<String>['AS', '7H', '9D', 'AH', '4H', '5H', '6H', '7C', 'QS', '9C'],
<String>['KS', '8H', '8D', '2C', '3C', '4C', '5C', '6C', 'KS', '8C'],
<String>['QS', '9H', '7D', '6D', '5D', '4D', '3D', '2D', 'AS', '7C'],
<String>['10S', '10H', 'QH', 'KH', 'AH', '2C', '3C', '4C', '5C', '6C'],
<String>['wild', '9S', '8S', '7S', '6S', '5S', '4S', '3S', '2S', 'wild']
];
List<Widget> _getHorizontalList(List<String> list) {
List<Widget> horizontalWidgets = List();
for(int i=0;i<list.length;i++) {
horizontalWidgets.add(
Flexible(
flex:1,
child:Container(
child: Image.asset('assets/cards/'+list[i]+'.png',fit: BoxFit.contain,),
)
)
);
}
return horizontalWidgets;
}
List<Widget> _getVerticallist() {
List<Widget> verticalWidgets = List();
for(int i=0;i<_imageList.length;i++) {
List<String> horizontalList= _imageList[i];
verticalWidgets.add(
Flex(
direction: Axis.horizontal,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: _getHorizontalList(horizontalList),
));
}
return verticalWidgets;
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.lightBlue,
body: Align(
alignment: Alignment.center,
child: SizedBox(
width: 0.95*MediaQuery.of(context).size.width,
height: 0.8*MediaQuery.of(context).size.height,
child: Flex(
direction: Axis.vertical,
children: _getVerticallist(),
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
),
),
),
);
}
}
答案 0 :(得分:0)
我可以看到您在一个字符串列表中还有另一个字符串列表,因此您唯一要做的就是在这样的字符串中再次循环。
for(int i = 0; i < _imageList.length; i++) {
for(int j = 0; j < _imageList[i].length; j++) {
horizontalWidgets.Add(
Flexible (
flex: 1,
child: Container (
child: Image.asset('assets/cards/'+list[i][j]+'.png',fit: BoxFit.contain,),
)
)
)
}
}