Browse Source

Seperated startseite, added zweiteseite + Navigation (push and pop)

Ray-works 9 months ago
parent
commit
4c299b9835
4 changed files with 132 additions and 88 deletions
  1. 3 88
      lib/main.dart
  2. 22 0
      lib/router.dart
  3. 83 0
      lib/startseite.dart
  4. 24 0
      lib/zweiteseite.dart

+ 3 - 88
lib/main.dart

@@ -1,8 +1,8 @@
1 1
 import 'dart:convert';
2 2
 import 'dart:math';
3 3
 import 'package:http/http.dart' as http;
4
-
5 4
 import 'package:flutter/material.dart';
5
+import 'router.dart';
6 6
 
7 7
 void main() {
8 8
   runApp(MyApp());
@@ -17,93 +17,8 @@ class MyApp extends StatelessWidget {
17 17
         primarySwatch: Colors.blue,
18 18
         visualDensity: VisualDensity.adaptivePlatformDensity,
19 19
       ),
20
-      home: StartSeite(),
20
+      initialRoute: StartSeiteRoute,
21
+      onGenerateRoute: MyRouter.routes,
21 22
     );
22 23
   }
23 24
 }
24
-
25
-class StartSeite extends StatefulWidget {
26
-  _StartSeiteState createState() => _StartSeiteState();
27
-}
28
-
29
-class _StartSeiteState extends State<StartSeite> {
30
-
31
-  Future<String> fetchName() async {
32
-    final person = await http.get('https://randomname.de/?format=json&count=1');
33
-    print(person.body);
34
-    
35
-  }
36
-
37
-  List<String> names = [
38
-    "John", 
39
-    "Peter", 
40
-    "Klaus", 
41
-    "Gabi", 
42
-    "Hans", 
43
-    "Dieter", 
44
-    "Ute", 
45
-    "Max",
46
-    "Jeff",
47
-    "Olaf",
48
-    "Anna",
49
-    "Rudolf",
50
-    "Theodor",
51
-    "Ralf",
52
-    "Sabine",
53
-    "Leo",
54
-    "Hubert",
55
-    "Willy",
56
-    "Werner",
57
-    "Ferdinand"
58
-  ];
59
-  String randomName = "Namen Generator";
60
-  List<String> generatedNames = List<String>();
61
-  final listScrollController = ScrollController();
62
-
63
-
64
-
65
-  @override
66
-  Widget build(BuildContext context) {
67
-    return Scaffold(
68
-      body: Center(
69
-        child: Column(
70
-          mainAxisAlignment: MainAxisAlignment.center,
71
-          crossAxisAlignment: CrossAxisAlignment.start,
72
-          children: [
73
-            Expanded(
74
-              child: Container(
75
-                alignment: Alignment.center,
76
-                child: ListView.separated(
77
-                  controller: listScrollController,
78
-                  separatorBuilder: (context, index) => Divider(height: 0.5, color: Colors.blue),
79
-                  itemCount: generatedNames.length,
80
-                  itemBuilder:(BuildContext context, int index) {
81
-                    return ListTile(
82
-                      title: Text(generatedNames[index]),
83
-                    );
84
-                  },
85
-                )
86
-              ),
87
-            ),
88
-            Center(
89
-              child: RaisedButton(
90
-                color: Colors.black,
91
-                splashColor: Colors.blue,
92
-                onPressed: () {
93
-                  this.fetchName();
94
-                  setState(() {
95
-                    randomName = names[Random().nextInt(names.length)];
96
-                    generatedNames.add(randomName);
97
-                    listScrollController.jumpTo(listScrollController.position.maxScrollExtent);
98
-                  });
99
-                },
100
-                child: Text("Gib mir einen Namen", style: TextStyle(color: Colors.white))
101
-              )
102
-            )
103
-          ],
104
-        )
105
-      )
106
-    );
107
-  }
108
-
109
-}

+ 22 - 0
lib/router.dart

@@ -0,0 +1,22 @@
1
+import 'package:flutter/material.dart';
2
+import 'startseite.dart';
3
+import 'zweiteseite.dart';
4
+
5
+const StartSeiteRoute = '/';
6
+const ZweiteSeiteRoute = '/zweiteseite';
7
+
8
+class MyRouter {
9
+  static Route<dynamic> routes(RouteSettings settings) {
10
+    switch (settings.name) {
11
+      case StartSeiteRoute:
12
+        return MaterialPageRoute(
13
+            builder: (BuildContext context) => StartSeite());
14
+      case ZweiteSeiteRoute:
15
+        {
16
+          String daten = settings.arguments;
17
+          return MaterialPageRoute(
18
+              builder: (BuildContext context) => ZweiteSeite(daten));
19
+        }
20
+    }
21
+  }
22
+}

+ 83 - 0
lib/startseite.dart

@@ -0,0 +1,83 @@
1
+import 'dart:convert';
2
+import 'package:http/http.dart' as http;
3
+import 'package:flutter/material.dart';
4
+import 'package:my_first_app/router.dart';
5
+
6
+class StartSeite extends StatefulWidget {
7
+  _StartSeiteState createState() => _StartSeiteState();
8
+}
9
+
10
+class _StartSeiteState extends State<StartSeite> {
11
+  Future<String> fetchName() async {
12
+    final person = await http.get('https://randomname.de/?format=json&count=1');
13
+    String vorname = "";
14
+    if (person != null) {
15
+      if (person.statusCode == 200) {
16
+        // print(person.body);
17
+        final personArray = jsonDecode(person.body);
18
+        final konkretePerson = personArray[0];
19
+        vorname = konkretePerson['firstname'];
20
+        // print('Vorname: $vorname');
21
+        // print('Mein StatusCode: ${person.statusCode}');
22
+      }
23
+    }
24
+    return Future.value(vorname);
25
+  }
26
+
27
+  List<String> generatedNames = List<String>();
28
+  final listScrollController = ScrollController();
29
+  String empfangeneDaten;
30
+
31
+  @override
32
+  Widget build(BuildContext context) {
33
+    return Scaffold(
34
+        body: Center(
35
+            child: Column(
36
+      mainAxisAlignment: MainAxisAlignment.center,
37
+      crossAxisAlignment: CrossAxisAlignment.start,
38
+      children: [
39
+        SizedBox(height: 45),
40
+        Text('Empfangene Daten:' + (empfangeneDaten ?? "Keine Daten")),
41
+        Expanded(
42
+          child: Container(
43
+              alignment: Alignment.center,
44
+              child: ListView.separated(
45
+                controller: listScrollController,
46
+                separatorBuilder: (context, index) =>
47
+                    Divider(height: 0.5, color: Colors.blue),
48
+                itemCount: generatedNames.length,
49
+                itemBuilder: (BuildContext context, int index) {
50
+                  return ListTile(
51
+                    title: Text(generatedNames[index]),
52
+                  );
53
+                },
54
+              )),
55
+        ),
56
+        Center(
57
+            child: RaisedButton(
58
+                color: Colors.black,
59
+                splashColor: Colors.blue,
60
+                onPressed: () async {
61
+                  // await fetchName().then((value) {
62
+                  //   if (value.isNotEmpty) {
63
+                  //     setState(() {
64
+                  //       generatedNames.add(value);
65
+                  //       listScrollController.jumpTo(
66
+                  //           listScrollController.position.maxScrollExtent);
67
+                  //     });
68
+                  //   }
69
+                  // });
70
+                  var result = await Navigator.pushNamed(
71
+                      context, ZweiteSeiteRoute,
72
+                      arguments: 'Daten von der Startseite');
73
+                  setState(() {
74
+                    empfangeneDaten = result;
75
+                    print(result);
76
+                  });
77
+                },
78
+                child: Text("Gib mir einen Namen",
79
+                    style: TextStyle(color: Colors.white))))
80
+      ],
81
+    )));
82
+  }
83
+}

+ 24 - 0
lib/zweiteseite.dart

@@ -0,0 +1,24 @@
1
+import 'package:flutter/material.dart';
2
+
3
+class ZweiteSeite extends StatelessWidget {
4
+  ZweiteSeite(this.daten);
5
+  String daten;
6
+
7
+  @override
8
+  Widget build(BuildContext context) {
9
+    return Scaffold(
10
+        body: Center(
11
+            child: Column(
12
+      mainAxisAlignment: MainAxisAlignment.center,
13
+      children: [
14
+        Text("Ich bin die zweite Seite."),
15
+        Text(daten),
16
+        FlatButton(
17
+            onPressed: () {
18
+              Navigator.pop(context, 'Daten von der zweiten Seite');
19
+            },
20
+            child: Text("Zurück"))
21
+      ],
22
+    )));
23
+  }
24
+}