Last active
June 9, 2019 07:31
-
-
Save benznest/3163364c15d3d7da09f12a5d77bc2697 to your computer and use it in GitHub Desktop.
Flutter Fundamental 2 Workshop
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'; | |
import 'package:flutter_app_2/email.dart'; | |
class DetailPage extends StatelessWidget { | |
final Email email; | |
DetailPage({this.email}); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
backgroundColor: Colors.red[300], | |
title: Text("Detail"), | |
actions: <Widget>[ | |
IconButton(icon: Icon(Icons.share), onPressed: () {}), | |
IconButton(icon: Icon(Icons.delete), onPressed: () {}), | |
IconButton(icon: Icon(Icons.file_download), onPressed: () {}), | |
],), | |
body: Container(color: Colors.grey[400], | |
child: ListView(children: <Widget>[ | |
Container( | |
decoration: BoxDecoration( | |
color: Colors.white, | |
borderRadius: BorderRadius.circular(8)), | |
margin: EdgeInsets.all(16), | |
padding: EdgeInsets.all(16), | |
child: Column( | |
crossAxisAlignment: CrossAxisAlignment.start, | |
children: <Widget>[ | |
Text(email.title, style: TextStyle(fontSize: 24),), | |
Text(email.description), | |
Image.network("https://images.unsplash.com/photo-1556912172-45b7abe8b7e1?auto=format&fit=crop&w=1000&q=80") | |
], | |
)) | |
]), | |
)); | |
} | |
} | |
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
class Email { | |
String title; | |
String description; | |
String time; | |
bool favorite; | |
Email({this.title, this.description, this.time, this.favorite = false}); | |
toggleFavorite(){ | |
favorite = !favorite; | |
} | |
} |
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'; | |
import 'package:flutter_app_2/detail_page.dart'; | |
import 'package:flutter_app_2/email.dart'; | |
void main() => | |
runApp( | |
MaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: MyHomePage(), | |
)); | |
class MyHomePage extends StatefulWidget { | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
List<Email> listEmail; | |
TextEditingController textEditingController = TextEditingController(); | |
@override | |
void initState() { | |
listEmail = List() | |
..add(Email(title: "Hello 1", description: "looooo", time: "12:56"))..add( | |
Email(title: "Hello 2", description: "looooo", time: "12:56", favorite: true))..add( | |
Email(title: "Hello 3", description: "looooo", time: "12:56"))..add(Email(title: "Hello 4", description: "looooo", time: "12:56"))..add( | |
Email(title: "Hello 5", description: "looooo", time: "12:56"))..add(Email(title: "Hello 6", description: "looooo", time: "12:56")); | |
super.initState(); | |
} | |
@override | |
void dispose() { | |
textEditingController.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
elevation: 0, | |
title: Text('MyApp'), | |
backgroundColor: Colors.red[300], | |
actions: <Widget>[ | |
IconButton(icon: Icon(Icons.filter_list), onPressed: () {}), | |
], | |
), | |
drawer: Drawer(child: ListView( | |
children: <Widget>[ | |
Container(constraints: BoxConstraints.expand(height: 200), | |
decoration: BoxDecoration( | |
gradient: LinearGradient(colors: [Colors.red[200], Colors.purple[300]], | |
begin: Alignment.topRight, end: Alignment.bottomLeft)), | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
CircleAvatar(child: Icon(Icons.movie)), | |
Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: Text("Flutter App", | |
style: TextStyle(color: Colors.white, fontSize: 24, | |
fontWeight: FontWeight.bold)), | |
) | |
],), | |
), | |
buildMenu(title: "Profile", subtitle: "View your profile", icon: Icons.person) | |
])), | |
body: Column( | |
children: <Widget>[ | |
Container(color: Colors.red[300], | |
child: Container( | |
margin: EdgeInsets.only(bottom: 12,left: 16,right: 16), | |
padding: EdgeInsets.all(6), | |
decoration: BoxDecoration(color: Colors.white, | |
borderRadius: BorderRadius.circular(6)), | |
child: Row( | |
children: <Widget>[ | |
Icon(Icons.search,color: Colors.grey,), | |
Expanded(child: | |
TextField(controller: textEditingController, | |
onChanged: (text){ | |
// filter. | |
})), | |
], | |
))), | |
Expanded( | |
child: Container(color: Colors.grey[400], | |
child: buildListView)), | |
], | |
), | |
); | |
} | |
Widget get buildListView { | |
return ListView.builder(padding: EdgeInsets.only(top: 16), | |
itemCount: listEmail.length, | |
itemBuilder: (BuildContext context, int index) { | |
return GestureDetector(child: buildRowEmail(listEmail[index]), | |
onTap: () => navigateToDetailPage(listEmail[index])); | |
}); | |
} | |
Container buildRowEmail(Email email) { | |
return Container( | |
decoration: BoxDecoration(color: Colors.white, | |
borderRadius: BorderRadius.circular(8)), | |
margin: EdgeInsets.symmetric(vertical: 4, horizontal: 16), | |
padding: EdgeInsets.all(16), | |
child: Row(children: <Widget>[ | |
CircleAvatar(child: Icon(Icons.email), | |
backgroundColor: Colors.red[300],), | |
Expanded(child: | |
Padding( | |
padding: EdgeInsets.only(left: 16), | |
child: Column(crossAxisAlignment: CrossAxisAlignment.start, | |
children: <Widget>[ | |
Text(email.title, style: TextStyle(fontSize: 18),), | |
Text(email.description), | |
],), | |
)), | |
Padding( | |
padding: EdgeInsets.only(left: 16), | |
child: Column(crossAxisAlignment: CrossAxisAlignment.center, | |
children: <Widget>[ | |
Text(email.time, style: TextStyle(fontSize: 18),), | |
GestureDetector( | |
child: buildIconFavorite(email.favorite), | |
onTap: () { | |
toggleFavorite(email); | |
}), | |
]), | |
) | |
])); | |
} | |
Icon buildIconFavorite(bool isFavorite) { | |
if (isFavorite) { | |
return Icon(Icons.star, color: Colors.yellow); | |
} else { | |
return Icon(Icons.star_border); | |
} | |
} | |
ListTile buildMenu({String title, String subtitle, IconData icon}) { | |
return ListTile( | |
title: Text(title), | |
subtitle: Text(subtitle), | |
leading: Icon(icon)); | |
} | |
void toggleFavorite(Email email) { | |
setState(() { | |
email.toggleFavorite(); | |
}); | |
} | |
navigateToDetailPage(Email email) { | |
Navigator.push(context, | |
MaterialPageRoute(builder: (context) => DetailPage(email: email))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment