Created
March 14, 2020 16:58
-
-
Save JaveedIshaq/eab6a10986dd81c699aede6f7b865c86 to your computer and use it in GitHub Desktop.
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 FamilyMember { | |
String name; | |
String userId; | |
String fatherId; | |
String fatherName; | |
String alive; | |
String sex; | |
int generationNumber; | |
FamilyMember( | |
{this.name, | |
this.userId, | |
this.fatherId, | |
this.fatherName, | |
this.alive, | |
this.sex, | |
this.generationNumber}); | |
FamilyMember.fromJson(Map<String, dynamic> json) { | |
name = json['name']; | |
userId = json['user_id']; | |
fatherId = json['father_id']; | |
fatherName = json['father_name']; | |
alive = json['alive']; | |
sex = json['sex']; | |
generationNumber = json['generation_number']; | |
} | |
Map<String, dynamic> toJson() { | |
final Map<String, dynamic> data = new Map<String, dynamic>(); | |
data['name'] = this.name; | |
data['user_id'] = this.userId; | |
data['father_id'] = this.fatherId; | |
data['father_name'] = this.fatherName; | |
data['alive'] = this.alive; | |
data['sex'] = this.sex; | |
data['generation_number'] = this.generationNumber; | |
return data; | |
} | |
} |
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 'dart:async'; | |
import 'dart:convert'; | |
import 'family-member-model.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.deepPurple, | |
), | |
home: MyHomePage(title: 'Json Read and Search'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
MyHomePage({Key key, this.title}) : super(key: key); | |
final String title; | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
List<FamilyMember> familyMembers = []; | |
//List<> myfamilyMembers = []; | |
void getFamilyMembersList() { | |
loadFamilyTreeJsonData(); | |
} | |
@override | |
void initState() { | |
getFamilyMembersList(); | |
super.initState(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: NestedScrollView( | |
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) { | |
return <Widget>[ | |
SliverAppBar( | |
expandedHeight: 150.0, | |
floating: false, | |
pinned: true, | |
flexibleSpace: FlexibleSpaceBar( | |
centerTitle: true, | |
title: Text("Collapsing Toolbar", | |
style: TextStyle( | |
color: Colors.white, | |
fontSize: 16.0, | |
)), | |
background: Image.network( | |
"https://images.pexels.com/photos/1616403/pexels-photo-1616403.jpeg?auto=compress&cs=tinysrgb&h=350", | |
fit: BoxFit.cover, | |
) | |
), | |
), | |
]; | |
}, | |
body: ListView.builder( | |
itemCount: familyMembers.length, | |
itemBuilder: (buildContext, index) => _buildItem(index) | |
), | |
), | |
); | |
} | |
Widget _buildItem (index) { | |
return Center( | |
child: Card( | |
color: Colors.lightBlue, | |
child: InkWell( | |
splashColor: Colors.red.withAlpha(30), | |
onTap: () {}, | |
child: Container( | |
width: 300, | |
height: 100, | |
child: Center(child: Text(familyMembers[index].name)), | |
), | |
), | |
), | |
); | |
} | |
Future loadFamilyTreeJsonData() async { | |
final jsonData = await DefaultAssetBundle.of(context).loadString('assets/sample.json'); | |
if (jsonData != null) { | |
final jsonResponse = jsonDecode(jsonData); | |
print(jsonResponse.length); | |
for (int i = 0; i < jsonResponse.length; i++) { | |
familyMembers.add(FamilyMember.fromJson(jsonResponse[i])); | |
} | |
print(familyMembers); | |
return familyMembers; | |
} else { | |
// If the server did not return a 200 OK response, then throw an exception. | |
throw Exception('Failed to load album'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment