ListView Widget
import 'package:flutter/material.dart';
class ResponsiveListView extends StatelessWidget {
final List<String> items;
ResponsiveListView({required this.items});
@override
Widget build(BuildContext context) {
double screenWidth = MediaQuery.of(context).size.width;
double scaleFactor = screenWidth / 375.0; // Assuming the base screen width is 375.0 (adjust as needed)
double dynamicItemHeight = 50.0 * scaleFactor; // Adjust the initial item height as needed
return ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
return Container(
height: dynamicItemHeight,
alignment: Alignment.center,
margin: EdgeInsets.symmetric(vertical: 8.0),
color: Colors.blue, // Set your own background color or styling
child: Text(
items[index],
style: TextStyle(fontSize: 16.0), // Set your own text styling
),
);
},
);
}
}
//main() function
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Responsive ListView Example'),
),
body: Center(
child: ResponsiveListView(
items: List.generate(10, (index) => 'Item $index'),
),
),
),
);
}
}
ResponsiveListView is a custom widget that takes a list of items (items) as a parameter.
Inside the build method, we use MediaQuery to get the current screen width.
We calculate a scaleFactor based on the assumption that the base screen width is 375.0. You may adjust this value depending on your design.
The dynamicItemHeight is calculated by multiplying the base item height (50.0) by the scaleFactor. You can adjust this value based on your design requirements.
The ListView.builder is used to dynamically build the list with the calculated item height.
You can customize this example based on your specific needs and adjust the scaling logic as necessary. Consider adding more properties to the ResponsiveListView class to handle other ListView properties or styling based on your requirements.
Inside the build method, we use MediaQuery to get the current screen width.
We calculate a scaleFactor based on the assumption that the base screen width is 375.0. You may adjust this value depending on your design.
The dynamicItemHeight is calculated by multiplying the base item height (50.0) by the scaleFactor. You can adjust this value based on your design requirements.
The ListView.builder is used to dynamically build the list with the calculated item height.
You can customize this example based on your specific needs and adjust the scaling logic as necessary. Consider adding more properties to the ResponsiveListView class to handle other ListView properties or styling based on your requirements.
Comments
Post a Comment