Listview.builder 在 Flutter 中实现时不显示我的图像
Listview.builder not showing my images when implementing in Flutter
我正在尝试实现一个水平列表视图,它显示我从 Flutter image_picker 插件中提取的图像。
如果我不使用 Listview 并且只显示单个图像,它工作正常。但是我正在尝试使用多个图像,并且一旦我将其放入 Listview 中,小部件就会显示为黑色。我的 Listview 代码如下:
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () async {
Navigator.pushReplacementNamed(context, 'inventory');
return false;
},
child: Scaffold(
appBar: AppBar(
backgroundColor: Colors.green[700],
title: Text(
'MyApp',
textAlign: TextAlign.center,
),
leading: new IconButton(
icon: new Icon(Icons.arrow_back),
onPressed: () {
Navigator.pushReplacementNamed(
context,
'inventory');
},
),
actions: <Widget>[
Padding(
padding: EdgeInsets.only(right: 10.0),
child: Container(
width: 50,
height: 50,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.white, //remove this when you add image.
),
child: GestureDetector(
onTap: (){
Navigator.pushNamed(context,'profile');
},
child: Image(
image:NetworkImage("imageUrl goes here"),
width: 120,
height: 120,
fit: BoxFit.cover,
),
),
),
)
],
),
body: SafeArea(
child: Column(
children: [
pickedFile == null ?
GestureDetector(
onTap: () {
_showMyDialog();
setState(() {
});
},
child: Container(
width: 200,
height: 200,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Click to add a Photo',textAlign:
TextAlign.center,style: TextStyle(fontSize: 24),),
SizedBox(height: 20,),
Icon(
Icons.add_circle_outline,
color: Colors.grey[700],
size: 30,
),
],
),
margin: EdgeInsets.all(20.0),
decoration: BoxDecoration(
border: Border.all(width: 2,color: Colors.grey),
shape: BoxShape.circle
),
),
)
:
GestureDetector(
onTap: () {
_showMyDialog();
setState(() {
});
},
child: ListView.builder(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemCount: _imageFileList!.length,
itemBuilder: (BuildContext context, int index) {
return kIsWeb
? Image.network(_imageFileList![index].path)
: Image.file(File(_imageFileList!
[index].path));
}
)
],
),
),),
);
}
}
我没有显示整个代码,因为页面太长,但页面的其余部分工作正常。如果我删除 Listview 构建器,而是仅使用下面的测试,它就可以正常工作。我做错了什么?
child: Row(
children: [
kIsWeb
? Container(
child: Image.network(_imageFileList![index].path))
: Container(
child: Image.file(File(_imageFileList![index].path)));
}
),
],
),
请帮忙。
**用我的完整小部件编辑了上面的代码
要么使用 Row
,要么使用 ListView
,不能同时使用。
您可以将 Row 与 List.generate
一起使用:
Row(
children: List.generate(_imageFileList!.length,
(i) => kIsWeb
? Container(child: Image.network(_imageFileList![index].path))
: Container(child: Image.file(File(_imageFileList![index].path))
),
或 ListView
没有 Row
的情况。 ListView
可能是您要使用的小部件。如果内容大于宽度,Row
将溢出,而 ListView
在这种情况下将充当其自己的 ScrollView。
我还会仔细检查您是否需要 Expanded
小部件。这通常用于 inside 一行(或类似的小部件)。
设法解决了我的问题。它与需要添加的高度限制有关。在 link
中找到了解决方案
我正在尝试实现一个水平列表视图,它显示我从 Flutter image_picker 插件中提取的图像。
如果我不使用 Listview 并且只显示单个图像,它工作正常。但是我正在尝试使用多个图像,并且一旦我将其放入 Listview 中,小部件就会显示为黑色。我的 Listview 代码如下:
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () async {
Navigator.pushReplacementNamed(context, 'inventory');
return false;
},
child: Scaffold(
appBar: AppBar(
backgroundColor: Colors.green[700],
title: Text(
'MyApp',
textAlign: TextAlign.center,
),
leading: new IconButton(
icon: new Icon(Icons.arrow_back),
onPressed: () {
Navigator.pushReplacementNamed(
context,
'inventory');
},
),
actions: <Widget>[
Padding(
padding: EdgeInsets.only(right: 10.0),
child: Container(
width: 50,
height: 50,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.white, //remove this when you add image.
),
child: GestureDetector(
onTap: (){
Navigator.pushNamed(context,'profile');
},
child: Image(
image:NetworkImage("imageUrl goes here"),
width: 120,
height: 120,
fit: BoxFit.cover,
),
),
),
)
],
),
body: SafeArea(
child: Column(
children: [
pickedFile == null ?
GestureDetector(
onTap: () {
_showMyDialog();
setState(() {
});
},
child: Container(
width: 200,
height: 200,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Click to add a Photo',textAlign:
TextAlign.center,style: TextStyle(fontSize: 24),),
SizedBox(height: 20,),
Icon(
Icons.add_circle_outline,
color: Colors.grey[700],
size: 30,
),
],
),
margin: EdgeInsets.all(20.0),
decoration: BoxDecoration(
border: Border.all(width: 2,color: Colors.grey),
shape: BoxShape.circle
),
),
)
:
GestureDetector(
onTap: () {
_showMyDialog();
setState(() {
});
},
child: ListView.builder(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemCount: _imageFileList!.length,
itemBuilder: (BuildContext context, int index) {
return kIsWeb
? Image.network(_imageFileList![index].path)
: Image.file(File(_imageFileList!
[index].path));
}
)
],
),
),),
);
}
}
我没有显示整个代码,因为页面太长,但页面的其余部分工作正常。如果我删除 Listview 构建器,而是仅使用下面的测试,它就可以正常工作。我做错了什么?
child: Row(
children: [
kIsWeb
? Container(
child: Image.network(_imageFileList![index].path))
: Container(
child: Image.file(File(_imageFileList![index].path)));
}
),
],
),
请帮忙。
**用我的完整小部件编辑了上面的代码
要么使用 Row
,要么使用 ListView
,不能同时使用。
您可以将 Row 与 List.generate
一起使用:
Row(
children: List.generate(_imageFileList!.length,
(i) => kIsWeb
? Container(child: Image.network(_imageFileList![index].path))
: Container(child: Image.file(File(_imageFileList![index].path))
),
或 ListView
没有 Row
的情况。 ListView
可能是您要使用的小部件。如果内容大于宽度,Row
将溢出,而 ListView
在这种情况下将充当其自己的 ScrollView。
我还会仔细检查您是否需要 Expanded
小部件。这通常用于 inside 一行(或类似的小部件)。
设法解决了我的问题。它与需要添加的高度限制有关。在 link
中找到了解决方案