Ive been struggling to get images to either fill or cover in a Flutter list and all be the same width and height. The images from a Wordpress json feed are all different sizes and aspect ratios. The images always are the same width with all the different code i ve tried but i cant seem to get them to be an even consistent height with each row in the list.
import package:flutter/material.dart ;
import data.dart ; // Import your Post model
class RecentPosts extends StatelessWidget {
final List<Post> posts;
const RecentPosts({Key? key, required this.posts}) : super(key: key);
@override
Widget build(BuildContext context) {
const double imageHeight = 100.0; // Fixed height for images
const double imageWidth = 120.0; // Fixed width for images
return ListView.builder(
itemCount: posts.length,
itemBuilder: (context, index) {
final post = posts[index];
return Card(
clipBehavior: Clip.antiAlias,
margin: const EdgeInsets.only(bottom: 16.0),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Image.network(
post.imageUrl,
width: imageWidth,
height: imageHeight,
fit: BoxFit.cover,
),
Expanded(
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
post.title,
style: const TextStyle(
fontWeight: FontWeight.bold,
fontSize: 16.0,
),
overflow: TextOverflow.ellipsis,
maxLines: 2,
),
const SizedBox(height: 4.0),
Text(
post.excerpt,
style: TextStyle(
color: Colors.grey[600],
fontSize: 12.0,
),
overflow: TextOverflow.ellipsis,
maxLines: 2,
),
],
),
),
),
],
),
);
},
);
}
}
Ive尝试了名单、电网和卡片,所有问题都是相同的。