From 6d6a9ee2baec045499434aa2733a8e494b19ac88 Mon Sep 17 00:00:00 2001 From: mskupin Date: Wed, 10 May 2023 17:56:51 +0200 Subject: [PATCH] added localization --- l10n.yaml | 3 + lib/localization/app_de.arb | 3 + lib/localization/app_en.arb | 6 ++ lib/main.dart | 116 ++++++---------------------------- lib/pages/create_page.dart | 0 lib/pages/dashboard_page.dart | 25 ++++++++ lib/pages/overview_page.dart | 0 pubspec.lock | 13 ++++ pubspec.yaml | 4 ++ 9 files changed, 73 insertions(+), 97 deletions(-) create mode 100644 l10n.yaml create mode 100644 lib/localization/app_de.arb create mode 100644 lib/localization/app_en.arb create mode 100644 lib/pages/create_page.dart create mode 100644 lib/pages/dashboard_page.dart create mode 100644 lib/pages/overview_page.dart diff --git a/l10n.yaml b/l10n.yaml new file mode 100644 index 0000000..a5b643a --- /dev/null +++ b/l10n.yaml @@ -0,0 +1,3 @@ +arb-dir: lib/localization +template-arb-file: app_en.arb +output-localization-file: app_localizations.dart \ No newline at end of file diff --git a/lib/localization/app_de.arb b/lib/localization/app_de.arb new file mode 100644 index 0000000..553d72b --- /dev/null +++ b/lib/localization/app_de.arb @@ -0,0 +1,3 @@ +{ + "helloWorld": "Hallo, Welt!" +} \ No newline at end of file diff --git a/lib/localization/app_en.arb b/lib/localization/app_en.arb new file mode 100644 index 0000000..46fffae --- /dev/null +++ b/lib/localization/app_en.arb @@ -0,0 +1,6 @@ +{ + "helloWorld": "Hello World!", + "@helloWorld": { + "description": "The conventional newborn programmer greeting" + } +} \ No newline at end of file diff --git a/lib/main.dart b/lib/main.dart index 008fa38..e83e29e 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,4 +1,7 @@ import 'package:flutter/material.dart'; +import 'package:kassenzettel/pages/dashboard_page.dart'; +import 'package:flutter_localizations/flutter_localizations.dart'; +import 'package:flutter_gen/gen_l10n/app_localizations.dart'; void main() { runApp(const MyApp()); @@ -7,109 +10,28 @@ void main() { class MyApp extends StatelessWidget { const MyApp({super.key}); - // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( - title: 'Flutter Demo', + title: 'Kassenzettel Demo', + localizationsDelegates: const [ + AppLocalizations.delegate, + GlobalMaterialLocalizations.delegate, + GlobalWidgetsLocalizations.delegate, + GlobalCupertinoLocalizations.delegate, + ], + supportedLocales: const [ + Locale('en'), + Locale('de'), + ], theme: ThemeData( - // This is the theme of your application. - // - // Try running your application with "flutter run". You'll see the - // application has a blue toolbar. Then, without quitting the app, try - // changing the primarySwatch below to Colors.green and then invoke - // "hot reload" (press "r" in the console where you ran "flutter run", - // or simply save your changes to "hot reload" in a Flutter IDE). - // Notice that the counter didn't reset back to zero; the application - // is not restarted. + useMaterial3: true, primarySwatch: Colors.blue, ), - home: const MyHomePage(title: 'Flutter Demo Home Page'), - ); - } -} - -class MyHomePage extends StatefulWidget { - const MyHomePage({super.key, required this.title}); - - // This widget is the home page of your application. It is stateful, meaning - // that it has a State object (defined below) that contains fields that affect - // how it looks. - - // This class is the configuration for the state. It holds the values (in this - // case the title) provided by the parent (in this case the App widget) and - // used by the build method of the State. Fields in a Widget subclass are - // always marked "final". - - final String title; - - @override - State createState() => _MyHomePageState(); -} - -class _MyHomePageState extends State { - int _counter = 0; - - void _incrementCounter() { - setState(() { - // This call to setState tells the Flutter framework that something has - // changed in this State, which causes it to rerun the build method below - // so that the display can reflect the updated values. If we changed - // _counter without calling setState(), then the build method would not be - // called again, and so nothing would appear to happen. - _counter++; - }); - } - - @override - Widget build(BuildContext context) { - // This method is rerun every time setState is called, for instance as done - // by the _incrementCounter method above. - // - // The Flutter framework has been optimized to make rerunning build methods - // fast, so that you can just rebuild anything that needs updating rather - // than having to individually change instances of widgets. - return Scaffold( - appBar: AppBar( - // Here we take the value from the MyHomePage object that was created by - // the App.build method, and use it to set our appbar title. - title: Text(widget.title), - ), - body: Center( - // Center is a layout widget. It takes a single child and positions it - // in the middle of the parent. - child: Column( - // Column is also a layout widget. It takes a list of children and - // arranges them vertically. By default, it sizes itself to fit its - // children horizontally, and tries to be as tall as its parent. - // - // Invoke "debug painting" (press "p" in the console, choose the - // "Toggle Debug Paint" action from the Flutter Inspector in Android - // Studio, or the "Toggle Debug Paint" command in Visual Studio Code) - // to see the wireframe for each widget. - // - // Column has various properties to control how it sizes itself and - // how it positions its children. Here we use mainAxisAlignment to - // center the children vertically; the main axis here is the vertical - // axis because Columns are vertical (the cross axis would be - // horizontal). - mainAxisAlignment: MainAxisAlignment.center, - children: [ - const Text( - 'You have pushed the button this many times:', - ), - Text( - '$_counter', - style: Theme.of(context).textTheme.headlineMedium, - ), - ], - ), - ), - floatingActionButton: FloatingActionButton( - onPressed: _incrementCounter, - tooltip: 'Increment', - child: const Icon(Icons.add), - ), // This trailing comma makes auto-formatting nicer for build methods. + routes: { + DashboardPage.routeName: (context) => const DashboardPage(), + }, + initialRoute: DashboardPage.routeName, ); } } diff --git a/lib/pages/create_page.dart b/lib/pages/create_page.dart new file mode 100644 index 0000000..e69de29 diff --git a/lib/pages/dashboard_page.dart b/lib/pages/dashboard_page.dart new file mode 100644 index 0000000..d7f8cee --- /dev/null +++ b/lib/pages/dashboard_page.dart @@ -0,0 +1,25 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_gen/gen_l10n/app_localizations.dart'; + +class DashboardPage extends StatelessWidget { + const DashboardPage({super.key}); + + static const routeName = '/dashboard'; + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: Text(AppLocalizations.of(context)?.helloWorld ?? ''), + ), + bottomNavigationBar: BottomAppBar( + child: Align( + alignment: Alignment.centerRight, + child: FloatingActionButton( + onPressed: () {}, + child: const Icon(Icons.add), + ), + )), + ); + } +} diff --git a/lib/pages/overview_page.dart b/lib/pages/overview_page.dart new file mode 100644 index 0000000..e69de29 diff --git a/pubspec.lock b/pubspec.lock index b2cb69d..056ae78 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -70,11 +70,24 @@ packages: url: "https://pub.dev" source: hosted version: "2.0.1" + flutter_localizations: + dependency: "direct main" + description: flutter + source: sdk + version: "0.0.0" flutter_test: dependency: "direct dev" description: flutter source: sdk version: "0.0.0" + intl: + dependency: "direct main" + description: + name: intl + sha256: "910f85bce16fb5c6f614e117efa303e85a1731bb0081edf3604a2ae6e9a3cc91" + url: "https://pub.dev" + source: hosted + version: "0.17.0" js: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index b980e13..cbf3198 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -35,6 +35,9 @@ dependencies: # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^1.0.2 + flutter_localizations: + sdk: flutter + intl: any dev_dependencies: flutter_test: @@ -57,6 +60,7 @@ flutter: # included with your application, so that you can use the icons in # the material Icons class. uses-material-design: true + generate: true # To add assets to your application, add an assets section, like this: # assets: