diff --git a/lib/main.dart b/lib/main.dart index 29215d1..20f430e 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -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'; @@ -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((_) => 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'), @@ -50,14 +56,14 @@ class App extends StatelessWidget { } } -class HomePage extends StatefulWidget { +class HomePage extends ConsumerStatefulWidget { const HomePage({super.key}); @override - State createState() => _HomePageState(); + ConsumerState createState() => _HomePageState(); } -class _HomePageState extends State with SingleTickerProviderStateMixin { +class _HomePageState extends ConsumerState with SingleTickerProviderStateMixin { bool showMenu = false; final GlobalKey _scaffoldKey = GlobalKey(); @@ -130,6 +136,22 @@ class _HomePageState extends State 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")), + ], + ), ) ], ), diff --git a/pubspec.lock b/pubspec.lock index 52221d4..e4cd62d 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -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 @@ -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: @@ -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: diff --git a/pubspec.yaml b/pubspec.yaml index 5948c25..701ba49 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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 @@ -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.