Skip to content
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

Merged
merged 32 commits into from
Mar 30, 2022

Conversation

AshAman999
Copy link
Member

What

  • Let's user change the theme according to the device theme
    Also need help in the file lib/pages/user_preferences_settings.dart as a comment left on line 84
demo.mp4

Fixes bug(s)

@AshAman999 AshAman999 requested a review from a team as a code owner March 20, 2022 14:14
@teolemon teolemon linked an issue Mar 20, 2022 that may be closed by this pull request
@teolemon teolemon added the darkmode Ensuring we look good in the dark label Mar 20, 2022
@teolemon teolemon changed the title Auto detect dark theme feat: Auto detect dark theme Mar 20, 2022
@teolemon
Copy link
Member

@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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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
Copy link
Member

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

Copy link
Member Author

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 :(

Copy link
Member

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';
Copy link
Member

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

Copy link
Member Author

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';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
static const String _TAG_THEME_DARK = 'themeDark';

@@ -47,8 +47,9 @@ class SmoothTheme {

static MaterialColor getMaterialColor(
final ThemeProvider themeProvider,
Copy link
Member

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

Copy link
Member Author

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

Copy link
Member

@M123-dev M123-dev Mar 20, 2022

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

Copy link
Member Author

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 ;(

Copy link
Member

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.

@AshAman999
Copy link
Member Author

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
Copy link
Collaborator

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

Copy link
Member

@M123-dev M123-dev left a 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

Comment on lines 20 to 21
final bool isDarkMode =
MediaQuery.platformBrightnessOf(context) == Brightness.dark;
Copy link
Member

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

Comment on lines 72 to 73
final bool isDarkMode =
MediaQuery.platformBrightnessOf(context) == Brightness.dark;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isDarkMode

Comment on lines 75 to 76
final bool isDarkMode =
MediaQuery.platformBrightnessOf(context) == Brightness.dark;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isDarkMode

@AshAman999
Copy link
Member Author

@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);
Copy link
Member

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

@AshAman999
Copy link
Member Author

@M123-dev id the changes, please check again,
sorry for the troubles, feels like there is a lot of ongoings for a simple dark theme implementation ;(

@M123-dev
Copy link
Member

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

@AshAman999
Copy link
Member Author

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

@M123-dev
Copy link
Member

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

@M123-dev
Copy link
Member

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 themeDark to either true or false.
This should be adopted for your New currentTheme String with the options Dark and Light. You can leave out System Default as it's always Light by default.

@M123-dev
Copy link
Member

Heyy @AshAman999 fixed the screenshot test + resolved conflicts

@AshAman999
Copy link
Member Author

AshAman999 commented Mar 30, 2022

I am looking at the changes you did, needed to see how exactly how it was done.
Thanks for the rescue here, had nearly given up here and is it now ready to be merged?

@AshAman999 AshAman999 requested a review from g123k March 30, 2022 16:36
@M123-dev
Copy link
Member

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

@M123-dev M123-dev merged commit 809e2ef into openfoodfacts:develop Mar 30, 2022
@M123-dev
Copy link
Member

But yeah thanks a lot for this great new feature

@AshAman999
Copy link
Member Author

Really happy to see this pr merged :)
thanks a lot for the guidance @M123-dev 💯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
darkmode Ensuring we look good in the dark
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Automatically identify the system dark mode status
4 participants