이미지 피커가 있는 버튼을 누르면 같은 페이지에 있는 리스트뷰에 이미지를 보여주는 기능을 구현하고 있습니다.
버튼을 누를때 setState 함수내에서
따로 만들어둔 리스트에 이미지 경로를 넣고, 따로 만든 ListView빌더 위젯을 넣어주는 식으로 구현했는데 안되네요
onPressed: () async {
final picker = ImagePicker();
var image = await picker.pickImage(source: ImageSource.gallery);
if (image != null) {
setState((){
useImage = File(image.path);
widget.a.add(useImage); //이미지경로를 기존 만들어둔 리스트 a 에 넣어줌
Picture(); // 따로 만들어둔 ListView 위젯
});
//따로 만들어둔 ListView 위젯
class Picture extends StatefulWidget {
Picture({Key? key, this.a}) : super(key: key);
var a;
@override
State<Picture> createState() => _PictureState();
}
class _PictureState extends State<Picture> {
@override
Widget build(BuildContext context) {
return ListView.builder(
itemBuilder: (BuildContext ctx, int idx) {
return Container(
width: 30, height: 30, child:
widget.a[idx].runtimeType == String
? Image.network(widget.a[idx])
: Image.file(widget.a[idx]),
);
},
itemCount: widget.a != null ? (widget.a.length) : 0, //widget.pictureList.length,
shrinkWrap: true,
scrollDirection : Axis.horizontal
);
}
}