Created
January 24, 2022 23:36
-
-
Save isaacadariku/b53ece3a549ecdf4ba918a36e54926b3 to your computer and use it in GitHub Desktop.
A check container with populated list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(Amenities()); | |
} | |
class CheckContainerModel { | |
final String title; | |
bool isCheck; | |
CheckContainerModel({required this.title, this.isCheck = false}); | |
CheckContainerModel copyWith({String? title, bool? isCheck}) { | |
return CheckContainerModel( | |
title: title ?? this.title, | |
isCheck: isCheck ?? this.isCheck, | |
); | |
} | |
} | |
class Amenities extends StatefulWidget { | |
const Amenities({Key? key}) : super(key: key); | |
@override | |
State<StatefulWidget> createState() { | |
return _AmenitiesState(); | |
} | |
} | |
class _AmenitiesState extends State<Amenities> { | |
List<CheckContainerModel> checkContainers = [ | |
CheckContainerModel(title: 'Gas Cooker', isCheck: false), | |
CheckContainerModel(title: 'Washing Machine', isCheck: false), | |
CheckContainerModel(title: 'Wifi', isCheck: false), | |
CheckContainerModel(title: 'Television', isCheck: false), | |
CheckContainerModel(title: 'Water Heater', isCheck: false), | |
]; | |
void onCheckTap(CheckContainerModel container) { | |
final index = checkContainers.indexWhere( | |
(element) => element.title == container.title, | |
); | |
bool previousIsCheck = checkContainers[index].isCheck; | |
checkContainers[index].isCheck = !previousIsCheck; | |
setState(() {}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
backgroundColor: Colors.transparent, | |
appBar: AppBar( | |
title: const Text("Sunflower"), | |
), | |
body: Center( | |
child: Wrap( | |
spacing: 5.0, | |
runSpacing: 5.0, | |
children: [ | |
...checkContainers.map((container) { | |
return InkWell( | |
splashColor: Colors.cyanAccent, | |
onTap: () => onCheckTap(container), | |
child: Container( | |
padding: const EdgeInsets.all(12), | |
color: container.isCheck ? Colors.blue : Colors.white, | |
child: Text( | |
container.title, | |
textAlign: TextAlign.center, | |
style: TextStyle( | |
fontSize: 14, | |
color: container.isCheck ? Colors.white : Colors.black, | |
), | |
), | |
), | |
); | |
}).toList(), | |
], | |
), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment