( I know this type of question have been asked before but I could not understand the solution)
Hi guy's, I am still learning flutter. I want to display cards with movie names and their prices. This is the code
import 'package:flutter/material.dart';
void main() {
runApp( MaterialApp(
home: HomePage(),
));
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Card(
child: ListTile(leading: Text("${Movie.name}"),trailing: Text($"Movie.price"),),
);
}
}
class Movie {
String? name;
int? price;
Movie({this.name,this.price});
}
List cinemas = [
Movie(name:"kingkong",price:25),
Movie(name:"hanuman",price:25),
Movie(name:"king polo",price:25),
Movie(name:"ashiqyue",price:25),
Movie(name:"hero",price:25),
Movie(name:"zero",price:25),
Movie(name:"kilo",price:25),
];
CodePudding user response:
You can do that by using ListView.builder like below :
ListView.builder(
itemCount: cinemas.length,
itemBuilder: (context, index) => Card(
child: ListTile(
leading: Text("${cinemas[index].name}"),
trailing: Text("${cinemas[index].price}"),
),
))
Also making List cinemas to List<Movie> cinemas is good practice as you are creating a list of particular object
CodePudding user response:
Try This code if you have List with fixed number of items in your List and you can use ListView.builder when number of items in List is not Fixed(when fetching List from API)
Column(
children: List.generate(cinemas.length, (index) => Card(
child: ListTile(
leading: Text("${cinemas[index].name}"),
trailing: Text("${cinemas[index].price}"),
),
),
),
)
