I want to create a horizontal ListView or GridView with fix height of images but width of image should be dynamic.
Below is the code witch I am trying to do but not able to get expected output. I have also added screenshot of design which illustrate the expected output
With List View
import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';
class ViewPager extends StatefulWidget {
createState() => _ViewPager();
}
class _ViewPager extends State<ViewPager> {
static final exercise_image1 = "https://drive.google.com/uc?export=download&id=1eDhXVJrNExRwi85pQGCOAwPYi7X-sA0l";
static final exercise_image2 = "https://drive.google.com/uc?export=download&id=1ifjpLw2ocBAIbEOIhsTtczf8dWuvexri";
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Column(
children: [
_imagesList(images: [exercise_image1, exercise_image2]),
],
),
);
}
Widget _imagesList({List<String> images}) {
return Container(
width: double.maxFinite,
height: 150,
color: Colors.red,
child: ListView(
scrollDirection: Axis.horizontal,
children: [
_imageListItem(imagePath: images.first),
_imageListItem(imagePath: images.last),
],
),
);
}
Widget _imageListItem({@required String imagePath}) {
return CachedNetworkImage(
width: 200, // I want this to be according to the image width
imageUrl: imagePath,
placeholder: (context, url) => Container(
child: CircularProgressIndicator(),
),
imageBuilder: (context, imageProvider) => Container(
decoration: BoxDecoration(
image: DecorationImage(
image: imageProvider,
fit: BoxFit.fitHeight
),
),
),
);
}
}
With Grid View
Same issue with Grid View, we set height of the grid view but not able to give dynamic width to the image
import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';
class ViewPager extends StatefulWidget {
createState() => _ViewPager();
}
class _ViewPager extends State<ViewPager> {
static final exercise_image1 = "https://drive.google.com/uc?export=download&id=1eDhXVJrNExRwi85pQGCOAwPYi7X-sA0l";
static final exercise_image2 = "https://drive.google.com/uc?export=download&id=1ifjpLw2ocBAIbEOIhsTtczf8dWuvexri";
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Container(
height: 200,
child: _imagesList(images: [exercise_image1, exercise_image2]),
),
),
);
}
Widget _imagesList({List<String> images}) {
return GridView.count(
crossAxisCount: 1,
scrollDirection: Axis.horizontal,
children: [
_imageListItem(imagePath: images[0]),
_imageListItem(imagePath: images[1]),
],
);
}
Widget _imageListItem({@required String imagePath}) {
return CachedNetworkImage(
imageUrl: imagePath,
placeholder: (context, url) => Container(
child: CircularProgressIndicator(),
),
imageBuilder: (context, imageProvider) => Container(
decoration: BoxDecoration(
image: DecorationImage(
image: imageProvider,
fit: BoxFit.fitHeight
),
),
),
);
}
}

