Skip to content

Commit

Permalink
feat: Adding riverpod to toggle between languages. #4
Browse files Browse the repository at this point in the history
  • Loading branch information
LuchoTurtle committed May 10, 2023
1 parent 4c08512 commit c40d9dd
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
36 changes: 29 additions & 7 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import 'app_localization.dart';
import 'menu.dart';
Expand All @@ -10,22 +11,27 @@ const homePageKey = Key("home_page");

// coverage:ignore-start
void main() {
runApp(const App());
runApp(const ProviderScope(child: App()));
}
// coverage:ignore-end

class App extends StatelessWidget {
/// Provider that tracks the current locale of the app
final currentLocaleProvider = StateProvider<Locale>((_) => const Locale('en', 'US'));

class App extends ConsumerWidget {
const App({super.key});

@override
Widget build(BuildContext context) {
Widget build(BuildContext context, WidgetRef ref) {
final currentLocale = ref.watch(currentLocaleProvider);

return MaterialApp(
title: 'Navigation Flutter Menu App',
theme: ThemeData(
primarySwatch: Colors.blue,
),
debugShowCheckedModeBanner: false,
locale: const Locale('pt', 'PT'),
locale: currentLocale,
supportedLocales: const [
Locale('en', 'US'),
Locale('pt', 'PT'),
Expand All @@ -50,14 +56,14 @@ class App extends StatelessWidget {
}
}

class HomePage extends StatefulWidget {
class HomePage extends ConsumerStatefulWidget {
const HomePage({super.key});

@override
State<HomePage> createState() => _HomePageState();
ConsumerState<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> with SingleTickerProviderStateMixin {
class _HomePageState extends ConsumerState<HomePage> with SingleTickerProviderStateMixin {
bool showMenu = false;
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();

Expand Down Expand Up @@ -130,6 +136,22 @@ class _HomePageState extends State<HomePage> with SingleTickerProviderStateMixin
showMenu = true;
});
},
),
Padding(
padding: const EdgeInsets.only(top: 24.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
ElevatedButton(
onPressed: () {ref.read(currentLocaleProvider.notifier).state = const Locale('pt', 'PT');},
style: ElevatedButton.styleFrom(backgroundColor: const Color.fromARGB(255, 161, 30, 30)),
child: const Text("PT")),
ElevatedButton(
onPressed: () {ref.read(currentLocaleProvider.notifier).state = const Locale('en', 'US');},
style: ElevatedButton.styleFrom(backgroundColor: const Color.fromARGB(255, 18, 50, 110)),
child: const Text("EN")),
],
),
)
],
),
Expand Down
24 changes: 24 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,14 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_riverpod:
dependency: "direct main"
description:
name: flutter_riverpod
sha256: b83ac5827baadefd331ea1d85110f34645827ea234ccabf53a655f41901a9bf4
url: "https://pub.dev"
source: hosted
version: "2.3.6"
flutter_test:
dependency: "direct dev"
description: flutter
Expand Down Expand Up @@ -341,6 +349,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.4"
riverpod:
dependency: transitive
description:
name: riverpod
sha256: "80e48bebc83010d5e67a11c9514af6b44bbac1ec77b4333c8ea65dbc79e2d8ef"
url: "https://pub.dev"
source: hosted
version: "2.3.6"
shared_preferences:
dependency: "direct main"
description:
Expand Down Expand Up @@ -426,6 +442,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.11.0"
state_notifier:
dependency: transitive
description:
name: state_notifier
sha256: "8fe42610f179b843b12371e40db58c9444f8757f8b69d181c97e50787caed289"
url: "https://pub.dev"
source: hosted
version: "0.7.2+1"
stream_channel:
dependency: transitive
description:
Expand Down
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ version: 1.0.0+1

environment:
sdk: '>=2.18.4 <3.0.0'
flutter: ">=3.0.0"

# Dependencies specify other packages that your package needs in order to work.
# To automatically upgrade your package dependencies to the latest versions
Expand All @@ -33,6 +34,7 @@ dependencies:
sdk: flutter
flutter_localizations:
sdk: flutter
flutter_riverpod: ^2.3.6

# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
Expand Down

0 comments on commit c40d9dd

Please sign in to comment.