Created
March 8, 2020 19:14
-
-
Save JaveedIshaq/585f9b23cde574ee62e4dd3d1fc82530 to your computer and use it in GitHub Desktop.
Passing Data from child widget to parent widget
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'; | |
class Article { | |
String name, detail; | |
Article(this.name, this.detail); | |
} | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
final articleList = List<Article>.generate(20, (i) { | |
return Article("Article number $i", "This is the detail of article $i"); | |
}); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
appBar: AppBar( | |
title: Text("Flutter List View with Divider"), | |
), | |
body: Builder( | |
builder: (BuildContext context) { | |
return MyList(articleList, (index) { | |
final snackBar = SnackBar(content: Text('List Item $index is Clicked')); | |
Scaffold.of(context).showSnackBar(snackBar); | |
} | |
); | |
}, | |
), | |
) | |
); | |
} | |
} | |
class MyList extends StatelessWidget { | |
final articles; | |
final ValueSetter<int> onItemTap; | |
MyList(this.articles, this.onItemTap); | |
@override | |
Widget build(BuildContext context) { | |
return ListView.separated( | |
itemBuilder: (BuildContext context, int index) { | |
return ListTile( | |
title: Text(articles[index].name), | |
subtitle: Text(articles[index].detail), | |
onTap: () { | |
onItemTap(index); | |
}, | |
); | |
}, | |
separatorBuilder: (BuildContext context, int index) => Divider(), | |
itemCount: articles.length | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment