Created
February 18, 2022 12:43
-
-
Save lavahasif/a31c6bdedf066f8a5b49baec3188987c to your computer and use it in GitHub Desktop.
Flutter ListView Item and Detail Click Listener
This file contains hidden or 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(MaterialApp(home: Scaffold(appBar: AppBar(), body: sta()))); | |
class sta extends StatefulWidget { | |
const sta({Key? key}) : super(key: key); | |
@override | |
State<sta> createState() => _staState(); | |
} | |
var isShow = false; | |
var getdata = Diohelper.getdata(); | |
class _staState extends State<sta> { | |
@override | |
Widget build(BuildContext context) { | |
List mwidge = []; | |
int index = 0; | |
getdata.forEach((element) { | |
element.index = index; | |
mwidge.add(ListTile( | |
onTap: () { | |
Navigator.push(context, | |
MaterialPageRoute(builder: (context) => DetailPage(element))); | |
}, | |
hoverColor: Colors.amber, | |
title: Text(element.name.toString()), | |
trailing: element.isShow | |
? SizedBox( | |
width: 100, | |
height: 50, | |
child: OutlinedButton( | |
onPressed: () { | |
Scaffold.of(context).showSnackBar( | |
SnackBar(content: Text(element.name.toString()))); | |
}, | |
child: Text("Show")), | |
) | |
: Container( | |
width: 100, | |
), | |
)); | |
index++; | |
}); | |
return GestureDetector( | |
child: Center( | |
child: ListView( | |
children: [...mwidge], | |
), | |
), | |
); | |
} | |
} | |
class DetailPage extends StatefulWidget { | |
productModel model; | |
DetailPage(this.model, {Key? key}) : super(key: key); | |
@override | |
_DetailPageState createState() => _DetailPageState(); | |
} | |
class _DetailPageState extends State<DetailPage> { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(), | |
body: Container( | |
child: ListView( | |
children: [ | |
Text("Name:" + widget.model.name.toString()), | |
Text("Category:" + widget.model.category.toString()), | |
Text("price:" + widget.model.price.toString()) | |
], | |
), | |
)); | |
} | |
} | |
class productModel { | |
String? name; | |
String? price; | |
String? category; | |
bool isShow = false; | |
int index = 0; | |
productModel({this.name, this.price, this.category, this.isShow = false}); | |
productModel.fromJson(Map<String, dynamic> json) { | |
name = json['name']; | |
price = json['price']; | |
category = json['category']; | |
isShow = json['isShow']; | |
} | |
Map<String, dynamic> toJson() { | |
final Map<String, dynamic> data = new Map<String, dynamic>(); | |
data['name'] = this.name; | |
data['price'] = this.price; | |
data['category'] = this.category; | |
data['isShow'] = this.isShow; | |
return data; | |
} | |
} | |
class Diohelper { | |
static List<productModel> getdata() { | |
List<productModel> list = []; | |
list.add(productModel(name: "broast", price: "100", category: "chicken")); | |
list.add(productModel(name: "mandi", price: "100", category: "chicken")); | |
list.add(productModel(name: "mandi", price: "100", category: "veg")); | |
return list; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment