personhinzufuegen.dart 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import 'package:flutter/material.dart';
  2. import 'package:shared_preferences/shared_preferences.dart';
  3. import 'models/person.dart';
  4. class PersonHinzufuegen extends StatefulWidget {
  5. @override
  6. PersonHinzufuegenState createState() => PersonHinzufuegenState();
  7. }
  8. class PersonHinzufuegenState extends State<PersonHinzufuegen> {
  9. @override
  10. Widget build(BuildContext context) {
  11. Person p = Person('', '', 0, '', '');
  12. return Scaffold(
  13. body: SafeArea(
  14. child: Padding(
  15. padding: EdgeInsets.only(left: 25, top: 10),
  16. child: Column(
  17. children: [
  18. TextField(
  19. decoration: InputDecoration(labelText: "Vorname"),
  20. onChanged: (value) {
  21. p.firstname = value;
  22. },
  23. ),
  24. TextField(
  25. decoration: InputDecoration(labelText: "Nachname"),
  26. onChanged: (value) {
  27. p.lastname = value;
  28. },
  29. ),
  30. TextField(
  31. decoration: InputDecoration(labelText: "Alter"),
  32. keyboardType: TextInputType.number,
  33. onChanged: (value) {
  34. p.age = int.parse(value);
  35. },
  36. ),
  37. TextField(
  38. decoration: InputDecoration(labelText: "Geschlecht"),
  39. onChanged: (value) {
  40. p.gender = value;
  41. },
  42. ),
  43. RaisedButton(
  44. onPressed: () async {
  45. SharedPreferences prefs = await SharedPreferences.getInstance();
  46. List<String> personenliste = prefs.getStringList("Personenliste") ?? List<String>();
  47. personenliste.add(p.toJson());
  48. prefs.setStringList("Personenliste", personenliste);
  49. Navigator.pop(context, true);
  50. },
  51. child: Text("Hinzufügen"))
  52. ],
  53. ))));
  54. }
  55. }