Mark Favourite App in ListView simple

 import "package:flutter/material.dart";


void main() {
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({super.key});

@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Mark Favourite App",
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.grey,
),
home: const MarkFavouriteApp(),
);
}
}

class MarkFavouriteApp extends StatefulWidget {
const MarkFavouriteApp({super.key});

@override
State<MarkFavouriteApp> createState() {
return MarkFavouriteAppState();
}
}

class MarkFavouriteAppState extends State<MarkFavouriteApp> {
List<String> fruitList = ["Orange", "Apple", "Banana", "Mango", "Pineapple"];

List<String> tempFruitList = [];

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text(
"Mark Favourite App",
style: TextStyle(fontWeight: FontWeight.bold),
),
centerTitle: true,
backgroundColor: Colors.grey,
),
body: ListView.builder(
itemCount: fruitList.length,
itemBuilder: (context, index) {
return Card(
child: ListTile(
title: Text(fruitList[index].toString()),
trailing: Icon(Icons.favorite,
color: tempFruitList.contains(fruitList[index].toString())
? Colors.red
: Colors.white),
onTap: () {
if (tempFruitList.contains(fruitList[index].toString())) {
tempFruitList.remove(fruitList[index].toString());
} else {
tempFruitList.add(fruitList[index].toString());
}
setState(() {});
},
),
);
},
),
);
}
}

Comments

Popular posts from this blog

Second GET API Calling with Bloc simple Example in Flutter

Stack Container Scrollable Card widget UI with Custom Widget

Pagination with Bloc Pattern in Flutter