startseite.dart 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'package:http/http.dart' as http;
  4. import 'package:flutter/material.dart';
  5. import 'package:my_first_app/router.dart';
  6. import 'package:my_first_app/utils/network.dart';
  7. import 'package:shared_preferences/shared_preferences.dart';
  8. import 'models/person.dart';
  9. class StartSeite extends StatefulWidget {
  10. _StartSeiteState createState() => _StartSeiteState();
  11. }
  12. class _StartSeiteState extends State<StartSeite> {
  13. List<Person> generatedPersons = List<Person>();
  14. Future<String> fetchPerson() async {
  15. final person = await http.get('https://randomname.de/?format=json&count=1&images=1');
  16. dynamic json = "";
  17. if (person != null) {
  18. if (person.statusCode == 200) {
  19. return Future.value(person.body);
  20. }
  21. }
  22. return Future.value(json);
  23. }
  24. @override
  25. void initState() {
  26. super.initState();
  27. printPreferences();
  28. Network.registerSubscription(updateUI);
  29. }
  30. void updateUI() {
  31. setState(() {});
  32. }
  33. void printPreferences() async {
  34. SharedPreferences prefs = await _prefs;
  35. setState(() {
  36. generatedPersons.clear();
  37. List<String> personenliste = prefs.getStringList("Personenliste") ?? List<String>();
  38. for (String pjson in personenliste) {
  39. print("Reading: $pjson");
  40. generatedPersons.add(Person.fromJson(pjson));
  41. }
  42. });
  43. }
  44. final listScrollController = ScrollController();
  45. GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
  46. Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
  47. @override
  48. Widget build(BuildContext context) {
  49. return Scaffold(
  50. key: scaffoldKey,
  51. body: Center(
  52. child: Column(
  53. mainAxisAlignment: MainAxisAlignment.center,
  54. crossAxisAlignment: CrossAxisAlignment.start,
  55. children: [
  56. SizedBox(height: 45),
  57. Expanded(
  58. child: Container(
  59. alignment: Alignment.center,
  60. child: ListView.separated(
  61. controller: listScrollController,
  62. separatorBuilder: (context, index) => Divider(height: 0.5, color: Colors.blue),
  63. itemCount: generatedPersons.length,
  64. itemBuilder: (BuildContext context, int index) {
  65. String vorname = generatedPersons[index].firstname;
  66. print(generatedPersons[index].toJson());
  67. return ListTile(
  68. leading: Network.isAvailable() ? Image.network(generatedPersons[index].image ?? '') : Image.asset("assets/images/default-avatar.png"),
  69. title: Text(vorname),
  70. onTap: () => Navigator.pushNamed(context, PersonDetailsRoute, arguments: generatedPersons[index]));
  71. },
  72. )),
  73. ),
  74. Center(child: Container(child: Image.asset("assets/images/drake.jpg"))),
  75. Center(
  76. child: RaisedButton(
  77. color: Colors.black,
  78. splashColor: Colors.blue,
  79. onPressed: !Network.isAvailable()
  80. ? null
  81. : () async {
  82. // var jsondata = '[{"firstname":"Annetraude","lastname":"Kreutzer","gender":"f","birthday":"26.11.2001","email":"Annetraude-Kreutzer@solar-impact.pro","age":18,"password":"ahX*u8Ahm)","username":"Moatmour01","location":{"zip":"67744","city":"Schweinschied","street":{"name":"Am Klingenberg","number":20}}}]';
  83. // var decoded = jsonDecode(jsondata);
  84. // Person p = Person(decoded[0]['firstname'], decoded[0]['lastname'], decoded[0]['age'], decoded[0]['gender']);
  85. // print(p);
  86. String firstname = "";
  87. await fetchPerson().then((value) async {
  88. if (value.isNotEmpty) {
  89. var json = jsonDecode(value)[0];
  90. Person p = Person(json['firstname'], json['lastname'], json['age'], json['gender'], json['image']);
  91. SharedPreferences prefs = await _prefs;
  92. firstname = json['firstname'];
  93. setState(() {
  94. generatedPersons.add(p);
  95. List<String> personenliste = List<String>();
  96. for (Person pobj in generatedPersons) {
  97. print("Writing: ${pobj.toJson()}");
  98. personenliste.add(pobj.toJson());
  99. }
  100. prefs.setStringList("Personenliste", personenliste);
  101. listScrollController.jumpTo(listScrollController.position.maxScrollExtent);
  102. });
  103. }
  104. });
  105. SnackBar sb = SnackBar(content: Text("$firstname wurde angelegt."));
  106. AlertDialog alert = AlertDialog(
  107. title: Text("Neue Person"),
  108. content: Text("Es wurde eine neue Person angelegt: $firstname"),
  109. actions: [
  110. Column(
  111. children: [
  112. FlatButton(
  113. child: Text("Ok"),
  114. onPressed: () {
  115. Navigator.of(context).pop();
  116. },
  117. ),
  118. FlatButton(
  119. child: Text("Cancel", style: TextStyle(color: Colors.red)),
  120. onPressed: () {
  121. Navigator.pop(context);
  122. },
  123. ),
  124. ],
  125. )
  126. ],
  127. );
  128. // showDialog(context: context, builder: (context){
  129. // return alert;
  130. // });
  131. // scaffoldKey.currentState.showSnackBar(sb);
  132. // var result = await Navigator.pushNamed(
  133. // context, ZweiteSeiteRoute,
  134. // arguments: 'Daten von der Startseite');
  135. // setState(() {
  136. // empfangeneDaten = result;
  137. // print(result);
  138. // });
  139. },
  140. child: Text("Gib mir einen Namen!", style: TextStyle(color: Colors.white)))),
  141. Center(
  142. child: RaisedButton(
  143. onPressed: () async {
  144. SharedPreferences prefs = await _prefs;
  145. setState(() {
  146. generatedPersons.clear();
  147. prefs.setStringList("Personenliste", List<String>());
  148. });
  149. },
  150. child: Text("Liste leeren")),
  151. ),
  152. Center(
  153. child: RaisedButton(
  154. onPressed: () async {
  155. bool result = await Navigator.pushNamed(context, PersonHinzufuegenRoute) ?? false;
  156. if (result) {
  157. setState(() {
  158. printPreferences();
  159. });
  160. }
  161. },
  162. child: Text("Person hinzufügen")),
  163. )
  164. ],
  165. )));
  166. }
  167. }