-
Notifications
You must be signed in to change notification settings - Fork 151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Theme change functionality #22
base: master
Are you sure you want to change the base?
Changes from 3 commits
c855872
a1888c9
50e6208
5d8e25d
6c415c1
6ff460a
8f271bb
9ad691f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_getx_template/app/data/local/preference/preference_manager.dart'; | ||
import 'package:get/get.dart' as getx; | ||
|
||
class ThemeService { | ||
static const themeModeKey = 'isDarkMode'; | ||
static const lightThemeText = 'Light Theme'; | ||
static const darkThemeText = 'Dark Theme'; | ||
|
||
final PreferenceManager _preferenceManager = | ||
getx.Get.find(tag: (PreferenceManager).toString()); | ||
|
||
// Get themeMode info from local storage and return current ThemeMode | ||
Future<ThemeMode> get themeMode async => | ||
await _loadThemeFromSharedPref() ? ThemeMode.dark : ThemeMode.light; | ||
|
||
// Load themeMode from local storage | ||
Future<bool> _loadThemeFromSharedPref() async { | ||
return await _preferenceManager.getBool(themeModeKey); | ||
} | ||
|
||
// Load isDarkMode status from local storage | ||
Future<bool> getCurrentThemeMode() async { | ||
return await _preferenceManager.getBool(themeModeKey); | ||
} | ||
|
||
// change isDarkMode status in local storage | ||
changeThemeMode(bool isDarkMode) { | ||
_preferenceManager.setBool(themeModeKey, isDarkMode); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import 'package:flutter/material.dart'; | ||
|
||
import 'app_colors.dart'; | ||
|
||
class AppThemes { | ||
// light theme configurations | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Our code should be self explanatory. don't need to add unnecessary comment. |
||
static ThemeData get lightTheme { | ||
return ThemeData( | ||
primarySwatch: AppColors.colorPrimarySwatch, | ||
visualDensity: VisualDensity.adaptivePlatformDensity, | ||
brightness: Brightness.light, | ||
primaryColor: AppColors.colorPrimary, | ||
canvasColor: AppColors.canvasColor, | ||
cardColor: AppColors.canvasColor, | ||
textTheme: const TextTheme( | ||
button: TextStyle( | ||
color: Colors.white, | ||
fontSize: 20.0, | ||
fontWeight: FontWeight.bold, | ||
), | ||
), | ||
fontFamily: 'Roboto', | ||
); | ||
} | ||
|
||
// dark theme configurations | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Our code should be self explanatory. don't need to add unnecessary comment. |
||
static ThemeData get darkTheme { | ||
return ThemeData( | ||
primarySwatch: AppColors.colorPrimarySwatch, | ||
visualDensity: VisualDensity.adaptivePlatformDensity, | ||
brightness: Brightness.dark, | ||
primaryColor: AppColors.colorPrimary, | ||
canvasColor: AppColors.canvasColorDark, | ||
cardColor: AppColors.canvasColorDark, | ||
textTheme: const TextTheme( | ||
button: TextStyle( | ||
color: Colors.white, | ||
fontSize: 20.0, | ||
fontWeight: FontWeight.bold, | ||
), | ||
), | ||
fontFamily: 'Roboto', | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,28 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_getx_template/app/core/utils/theme_service.dart'; | ||
import 'package:get/get.dart'; | ||
|
||
import '../../../my_app/controllers/my_app_controller.dart'; | ||
import '/app/core/base/base_controller.dart'; | ||
|
||
class SettingsController extends BaseController { | ||
final count = 0.obs; | ||
final ThemeService themeService = ThemeService(); | ||
final Rx<bool> isCurrentThemeDarkMode = Rx<bool>(false); | ||
final Rx<String> currentThemeText = Rx<String>(""); | ||
final MyAppController _myAppController = Get.find(); | ||
|
||
void increment() => count.value++; | ||
changeTheme() async { | ||
late ThemeMode themeMode; | ||
themeMode = isCurrentThemeDarkMode.value ? ThemeMode.light : ThemeMode.dark; | ||
_myAppController.setCurrentThemeMode(themeMode); | ||
await themeService.changeThemeMode(!isCurrentThemeDarkMode.value); | ||
await getCurrentThemeMode(); | ||
} | ||
|
||
getCurrentThemeMode() async { | ||
isCurrentThemeDarkMode.value = await themeService.getCurrentThemeMode(); | ||
currentThemeText.value = isCurrentThemeDarkMode.value | ||
? ThemeService.lightThemeText | ||
: ThemeService.darkThemeText; | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import '../controllers/my_app_controller.dart'; | ||
import 'package:get/get.dart'; | ||
|
||
class MyAppBindings extends Bindings { | ||
@override | ||
void dependencies() { | ||
Get.lazyPut<MyAppController>( | ||
() => MyAppController(), | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_getx_template/app/core/utils/theme_service.dart'; | ||
import 'package:get/get.dart'; | ||
|
||
class MyAppController extends GetxController { | ||
ThemeService themeService = ThemeService(); | ||
var currentThemeMode = Rx<ThemeMode>(ThemeMode.light); | ||
|
||
MyAppController() { | ||
getCurrentThemeMode(); | ||
} | ||
|
||
getCurrentThemeMode() async { | ||
currentThemeMode.value = await themeService.themeMode; | ||
} | ||
|
||
setCurrentThemeMode(ThemeMode themeMode) { | ||
currentThemeMode.value = themeMode; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add return type as |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_gen/gen_l10n/app_localizations.dart'; | ||
import 'package:flutter_getx_template/app/core/values/app_themes.dart'; | ||
import 'package:flutter_getx_template/app/my_app/controllers/my_app_controller.dart'; | ||
import 'package:get/get.dart'; | ||
|
||
import '/app/bindings/initial_binding.dart'; | ||
import '/app/routes/app_pages.dart'; | ||
import '/flavors/build_config.dart'; | ||
import '/flavors/env_config.dart'; | ||
import '../../bindings/local_source_bindings.dart'; | ||
import '../bindings/my_app_bindings.dart'; | ||
|
||
class MyApp extends StatefulWidget { | ||
const MyApp({Key? key}) : super(key: key); | ||
|
||
@override | ||
_MyAppState createState() => _MyAppState(); | ||
} | ||
|
||
class _MyAppState extends State<MyApp> { | ||
final EnvConfig _envConfig = BuildConfig.instance.config; | ||
late MyAppController controller; | ||
|
||
@override | ||
void initState() { | ||
// initialize [LocalResourceBinding] here because | ||
hasibul-hasan-shuvo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// we need to access SharedPreferences in [MyAppController] | ||
LocalSourceBindings().dependencies(); | ||
MyAppBindings().dependencies(); | ||
controller = Get.find(); | ||
super.initState(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Obx(() => GetMaterialApp( | ||
title: _envConfig.appName, | ||
initialRoute: AppPages.INITIAL, | ||
initialBinding: InitialBinding(), | ||
getPages: AppPages.routes, | ||
localizationsDelegates: AppLocalizations.localizationsDelegates, | ||
supportedLocales: _getSupportedLocal(), | ||
theme: AppThemes.lightTheme, | ||
darkTheme: AppThemes.darkTheme, | ||
themeMode: controller.currentThemeMode.value, | ||
debugShowCheckedModeBanner: false, | ||
)); | ||
} | ||
|
||
List<Locale> _getSupportedLocal() { | ||
return [ | ||
const Locale('en', ''), | ||
const Locale('bn', ''), | ||
]; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Our code should be self explanatory. don't need to add unnecessary comment.