Skip to content

Instantly share code, notes, and snippets.

@5lineofcode
Created August 4, 2020 06:49
Show Gist options
  • Save 5lineofcode/aa64315e042ffd5b72db6693899257d4 to your computer and use it in GitHub Desktop.
Save 5lineofcode/aa64315e042ffd5b72db6693899257d4 to your computer and use it in GitHub Desktop.
Contoh Chekbox di ListView Builder
import 'package:flutter/material.dart';
final Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: ProductListLayout(),
),
),
);
}
}
class ProductListLayout extends StatefulWidget {
@override
_ProductListLayoutState createState() => _ProductListLayoutState();
}
class _ProductListLayoutState extends State<ProductListLayout> {
List productList = [
{
"checked": false,
"product_name": "GG Filter 12",
"photo":
"https://4.bp.blogspot.com/-PVRmD2vvLkw/WMobYiQhlKI/AAAAAAAABuM/WUnPxyb-fqAdXqi9MlpH_uXqe4yfAISigCLcB/s1600/20170316_084612.jpg",
"price": 16500,
},
{
"checked": false,
"product_name": "SK KRETEK 12",
"photo":
"https://ecs7.tokopedia.net/img/cache/700/product-1/2019/5/5/58429851/58429851_e4924499-b958-4abb-8170-408da31a4a3f_700_700.jpg",
"price": 19700,
},
{
"checked": false,
"product_name": "GG KALENG",
"photo":
"https://s0.bukalapak.com/img/55009519831/original/EKSLUSIF_GUDANG_GARAM_FILTER_KALENG_50___ROKOK_GG_FILTER_KAL.jpg",
"price": 32300,
}
];
@override
Widget build(BuildContext context) {
var productTotal = 0;
var selectedProducts =
productList.where((product) => product["checked"] == true).toList();
selectedProducts.forEach((product) {
productTotal += product["price"];
});
return Scaffold(
appBar: AppBar(
title: Text("Product"),
),
body: Column(
children: <Widget>[
Expanded(
child: ListView.builder(
itemCount: productList.length,
itemBuilder: (context, index) {
var product = productList[index];
return Card(
child: Container(
padding: EdgeInsets.all(8.0),
child: Row(
children: <Widget>[
Checkbox(
value: product["checked"],
onChanged: (value) {
product["checked"] = value;
setState(() {});
},
),
Image.network(
product["photo"],
height: 70.0,
width: 70.0,
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
product["product_name"],
style: TextStyle(
fontSize: 14.0,
color: Colors.orange[700],
),
),
Text(
product["price"].toString(),
style: TextStyle(
fontSize: 12.0,
color: Colors.green[800],
fontWeight: FontWeight.bold,
),
),
],
),
],
),
),
);
},
),
),
Container(
color: Colors.black,
padding: EdgeInsets.all(14.0),
child: Center(
child: Text(
productTotal.toString(),
style: TextStyle(
color: Colors.white,
fontSize: 24.0,
),
),
),
),
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment