Last active
March 8, 2020 18:26
-
-
Save JaveedIshaq/36e016e256d078c80bc99d878390aeb2 to your computer and use it in GitHub Desktop.
Passing Data with Route in Flutter
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: ListView.separated( | |
itemBuilder: (BuildContext context, int index){ | |
return ListTile( | |
title: Text(articleList[index].name), | |
subtitle: Text(articleList[index].detail), | |
onTap: (){ | |
Navigator.push(context, MaterialPageRoute( | |
builder: (context) => DetailScreen(articleList[index]) | |
)); | |
}, | |
); | |
}, | |
separatorBuilder: (BuildContext context, int index) => Divider(), | |
itemCount: articleList.length | |
), | |
), | |
); | |
} | |
} | |
class DetailScreen extends StatelessWidget { | |
final Article selectedArticle; | |
DetailScreen(this.selectedArticle); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(selectedArticle.name) | |
), | |
body: Center( | |
child: Text(selectedArticle.detail), | |
), | |
); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment