Browse Source

Sprint 2 // User Story

mas files y el main
carlos.perez25 3 years ago
parent
commit
491542ba18
2 changed files with 86 additions and 97 deletions
  1. 17
    0
      Carlos/language.dart
  2. 69
    97
      Carlos/main.dart

+ 17
- 0
Carlos/language.dart View File

@@ -0,0 +1,17 @@
1
+class Language {
2
+  final int id;
3
+  final String flag;
4
+  final String name;
5
+  final String languageCode;
6
+
7
+  Language(this.id, this.flag, this.name, this.languageCode);
8
+
9
+  static List<Language> languageList() {
10
+    return <Language>[
11
+      Language(1, "🇦🇫", "فارسی", "fa"),
12
+      Language(2, "🇺🇸", "English", "en"),
13
+      Language(3, "🇺🇸", "Español", "es"),
14
+      Language(4, "🇮🇳", "हिंदी", "hi"),
15
+    ];
16
+  }
17
+}

+ 69
- 97
Carlos/main.dart View File

@@ -1,109 +1,81 @@
1 1
 import 'package:flutter/material.dart';
2
-import 'dart:convert';
3
-import 'package:json_table/json_table.dart';
2
+import 'package:userstory2translate/localization/localization.dart';
3
+import 'package:userstory2translate/router/custom_router.dart';
4
+import 'package:userstory2translate/router/route_constants.dart';
5
+import 'package:flutter_localizations/flutter_localizations.dart';
4 6
 
5
-class SimpleTable extends StatefulWidget {
6
-  @override
7
-  _SimpleTableState createState() => _SimpleTableState();
8
-}
7
+import 'localization/language_constants.dart';
9 8
 
10
-class _SimpleTableState extends State<SimpleTable> {
11
-  final String jsonSample =
12
-      '[{"id":"0","name":"Dr.Collazo","Oficina":"A","Phone":"787-522-0123","Espe":"Dentist"},{"id":"1","name":"Dr.Lelolelo","Oficina":"B","Phone":"787-533-4567","Espe":"Dentist"},{"id":"2","name":"Dr.Pepo","Oficina":"C","Phone":"787-544-8910","Espe":"otorrinolaringologo"}]';
13
-  bool toggle = true;
9
+void main() => runApp(MyApp());
14 10
 
15
-  @override
16
-  Widget build(BuildContext context) {
17
-    var json = jsonDecode(jsonSample);
18
-    return Scaffold(
19
-      body: Container(
20
-        padding: EdgeInsets.all(16.0),
21
-        child: toggle
22
-            ? Column(
23
-          children: [
24
-            JsonTable(
25
-              json,
26
-              showColumnToggle: true,
27
-              tableHeaderBuilder: (String header) {
28
-                return Container(
29
-                  padding: EdgeInsets.symmetric(
30
-                      horizontal: 8.0, vertical: 4.0),
31
-                  decoration: BoxDecoration(
32
-                      border: Border.all(width: 0.5),
33
-                      color: Colors.grey[300]),
34
-                  child: Text(
35
-                    header,
36
-                    textAlign: TextAlign.center,
37
-                    style: Theme.of(context).textTheme.display1.copyWith(
38
-                        fontWeight: FontWeight.w700,
39
-                        fontSize: 14.0,
40
-                        color: Colors.black87),
41
-                  ),
42
-                );
43
-              },
44
-              tableCellBuilder: (value) {
45
-                return Container(
46
-                  padding: EdgeInsets.symmetric(
47
-                      horizontal: 4.0, vertical: 2.0),
48
-                  decoration: BoxDecoration(
49
-                      border: Border.all(
50
-                          width: 0.5,
51
-                          color: Colors.grey.withOpacity(0.5))),
52
-                  child: Text(
53
-                    value,
54
-                    textAlign: TextAlign.center,
55
-                    style: Theme.of(context).textTheme.display1.copyWith(
56
-                        fontSize: 14.0, color: Colors.grey[900]),
57
-                  ),
58
-                );
59
-              },
60
-              allowRowHighlight: true,
61
-              rowHighlightColor: Colors.yellow[500].withOpacity(0.7),
62
-              paginationRowCount: 20,
63
-            ),
64
-            SizedBox(
65
-              height: 20.0,
66
-            ),
67
-            Text("Simple table which creates table directly from json")
68
-          ],
69
-        )
70
-            : Center(
71
-          child: Text(getPrettyJSONString(jsonSample)),
72
-        ),
73
-      ),
74
-      floatingActionButton: FloatingActionButton(
75
-          child: Icon(Icons.grid_on),
76
-          onPressed: () {
77
-            setState(
78
-                  () {
79
-                toggle = !toggle;
80
-              },
81
-            );
82
-          }),
83
-    );
11
+class MyApp extends StatefulWidget {
12
+  const MyApp({Key key}) : super(key: key);
13
+  static void setLocale(BuildContext context, Locale newLocale) {
14
+    _MyAppState state = context.findAncestorStateOfType<_MyAppState>();
15
+    state.setLocale(newLocale);
84 16
   }
85 17
 
86
-  String getPrettyJSONString(jsonObject) {
87
-    JsonEncoder encoder = new JsonEncoder.withIndent('  ');
88
-    String jsonString = encoder.convert(json.decode(jsonObject));
89
-    return jsonString;
90
-  }
18
+  @override
19
+  _MyAppState createState() => _MyAppState();
91 20
 }
92 21
 
93
-void main() => runApp(MyApp());
22
+class _MyAppState extends State<MyApp> {
23
+  Locale _locale;
24
+  setLocale(Locale locale) {
25
+    setState(() {
26
+      _locale = locale;
27
+    });
28
+  }
94 29
 
95
-class MyApp extends StatelessWidget {
96 30
   @override
97
-  Widget build(BuildContext context) {
98
-    return MaterialApp(
99
-      title: 'Flutter Demo',
100
-      theme: ThemeData(
101
-        primarySwatch: Colors.blue,
102
-      ),
103
-      home: SimpleTable(),
104
-    );
31
+  void didChangeDependencies() {
32
+    getLocale().then((locale) {
33
+      setState(() {
34
+        this._locale = locale;
35
+      });
36
+    });
37
+    super.didChangeDependencies();
105 38
   }
106
-}
107
-
108
-
109 39
 
40
+  @override
41
+  Widget build(BuildContext context) {
42
+    if (this._locale == null) {
43
+      return Container(
44
+        child: Center(
45
+          child: CircularProgressIndicator(
46
+              valueColor: AlwaysStoppedAnimation<Color>(Colors.blue[800])),
47
+        ),
48
+      );
49
+    } else {
50
+      return MaterialApp(
51
+        debugShowCheckedModeBanner: false,
52
+        title: "Flutter Localization Demo",
53
+        theme: ThemeData(primarySwatch: Colors.blue),
54
+        locale: _locale,
55
+        supportedLocales: [
56
+          Locale("en", "US"),
57
+          Locale("fa", "IR"),
58
+          Locale("es", "SP"),
59
+          Locale("hi", "IN")
60
+        ],
61
+        localizationsDelegates: [
62
+          DemoLocalization.delegate,
63
+          GlobalMaterialLocalizations.delegate,
64
+          GlobalWidgetsLocalizations.delegate,
65
+          GlobalCupertinoLocalizations.delegate,
66
+        ],
67
+        localeResolutionCallback: (locale, supportedLocales) {
68
+          for (var supportedLocale in supportedLocales) {
69
+            if (supportedLocale.languageCode == locale.languageCode &&
70
+                supportedLocale.countryCode == locale.countryCode) {
71
+              return supportedLocale;
72
+            }
73
+          }
74
+          return supportedLocales.first;
75
+        },
76
+        onGenerateRoute: CustomRouter.generatedRoute,
77
+        initialRoute: homeRoute,
78
+      );
79
+    }
80
+  }
81
+}