-
-
Notifications
You must be signed in to change notification settings - Fork 287
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
feat: Auto detect dark theme #1276
feat: Auto detect dark theme #1276
Conversation
@AshAman999 adding 'feat: ' in the title makes your PR semantic, and thus Release Please will generate the Changelog automatically. I just edited the title accordingly. |
}, | ||
items: <DropdownMenuItem<String>>[ | ||
DropdownMenuItem<String>( | ||
// need suggestions on how to set my own appLoxalizations for the string System Default |
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.
// need suggestions on how to set my own appLoxalizations for the string System Default | |
// need suggestions on how to set my own appLocalizations for the string System Default |
}, | ||
items: <DropdownMenuItem<String>>[ | ||
DropdownMenuItem<String>( | ||
// need suggestions on how to set my own appLoxalizations for the string System Default |
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.
You can create new strings in lib/l10n/app_en.arb
they are probably not directly picked up by the analyzer but after restarting the app they should definitely be there
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.
I am still not able to figure this out, as it was throwing an error and I couldn't find it :(
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.
Then just add a todo comment like this:
//TODO(X): Localize
@@ -18,6 +18,7 @@ class UserPreferences extends ChangeNotifier { | |||
static const String _TAG_PREFIX_IMPORTANCE = 'IMPORTANCE_AS_STRING'; | |||
static const String _TAG_INIT = 'init'; | |||
static const String _TAG_THEME_DARK = 'themeDark'; |
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.
Can't this and the methods for getting and setting darkmode be removed
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.
Yeah, and then the whole places where the bool darkTheme
was used,
As the bool darkTheme will be removed the places where it was being needed to have a bool to show diff colors according to the theme, as with the provided can't check exactly what theme is being used when using ThemeMode.system
old way
if (themeProvider.darkTheme) {
_textFieldBackgroundColor = Colors.white10;
}
new proposed way to check if app uses dark theme for now
if (MediaQuery.platformBrightnessOf(context) == Brightness.dark) {
_textFieldBackgroundColor = Colors.white10;
}
As for now, it is working, should I do it?
@@ -18,6 +18,7 @@ class UserPreferences extends ChangeNotifier { | |||
static const String _TAG_PREFIX_IMPORTANCE = 'IMPORTANCE_AS_STRING'; | |||
static const String _TAG_INIT = 'init'; | |||
static const String _TAG_THEME_DARK = 'themeDark'; |
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.
static const String _TAG_THEME_DARK = 'themeDark'; |
@@ -47,8 +47,9 @@ class SmoothTheme { | |||
|
|||
static MaterialColor getMaterialColor( | |||
final ThemeProvider themeProvider, |
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.
You can remove the ThemeProvider here and get it inside getMaterialColorImpl
since you already pass the context
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.
did the same, but some unit tests are failing Idk why? rest the app was working real smooth
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.
Probably because of screenshot test. There the test check if the changes in UI are over a cartain percentage. The question is why the ui changed.
Maybe when we use MediaQuery.platformBrightnessOf(context) == Brightness.dark
instead of context.watch<ThemeProvider>().darkMode
it doesn't change when changing the theme.
This should defintely be checked
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.
yeah the screenshot test was failing I tried to look into this and still scratching my head,I am kinda stuck guess I need some help here ;(
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.
Heyy yeah sure, sorry for the late reply. You can regenerate them by running flutter test --update-goldens
.
Can it be possible that the test cases were written into taking consideration according to the previous code, so even if my code is doing the work it is failing the tests because it wasn't supposed to be for it? @M123-dev |
@@ -25,7 +23,7 @@ class SmoothLargeButtonWithIcon extends StatelessWidget { | |||
children: <Widget>[ | |||
Icon( | |||
icon, | |||
color: isDarkMode | |||
color: MediaQuery.platformBrightnessOf(context) == Brightness.dark |
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.
It would be better to create a single utility method or an extension for this kind of check
…9/smooth-app into auto-detect-dark-theme
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.
Heyyyy thanks @AshAman999, just some places where your updated isDarkMode
method should be used besided that the code looks good.
But please also remove the screenshots in test/pages/failures
final bool isDarkMode = | ||
MediaQuery.platformBrightnessOf(context) == Brightness.dark; |
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.
Please use the isDarkMode method
final bool isDarkMode = | ||
MediaQuery.platformBrightnessOf(context) == Brightness.dark; |
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.
isDarkMode
final bool isDarkMode = | ||
MediaQuery.platformBrightnessOf(context) == Brightness.dark; |
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.
isDarkMode
@M123-dev did the changes, you can have a look at the recent changes |
final EdgeInsets? padding; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
final ThemeData themeData = Theme.of(context); | ||
final UserPreferences userPreferences = | ||
Provider.of<UserPreferences>(context, listen: false); | ||
final bool isDarkMode = ThemeProvider(userPreferences).isDarkMode(context); |
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.
Just one thing I just noticed. The ThemeProvider is a provider, so you shouldn't create it all over again but directly access it through it
@M123-dev id the changes, please check again, |
No need to apologize, now after the small touch ups the code looks great.Only the screenshot tests for the dark preferences pages (left tab) have over 80% deviation. It seems there is some problem |
Though i may be wrong , is it possible that the tests were written so as to be for the previous code, since we are using a different method to chose dark theme so it may be possible that the code might be doing okay but the tests needs to be rewritten |
That's a likely answer, I just ran your PR and it worked fine for me. Merging is blocken as long as the tests are failing. I'll have a look at the screenshot test later |
Heyy @AshAman999 I just checked the implementation here: https://github.com/openfoodfacts/smooth-app/blob/develop/packages/smooth_app/test/pages/user_preferences_page_test.dart#L32 There we create mock SharedPreferences with the key |
Heyy @AshAman999 fixed the screenshot test + resolved conflicts |
I am looking at the changes you did, needed to see how exactly how it was done. |
Yeah the screenshot tests can be sometimes confusing and yes I think so, didn't do any major changes outside of the tests and we've been waiting for the automatic darkmode for way too long now |
But yeah thanks a lot for this great new feature |
Really happy to see this pr merged :) |
What
Also need help in the file
lib/pages/user_preferences_settings.dart
as a comment left on line 84demo.mp4
Fixes bug(s)