0

I've created a dart file called theme.dart wherein I put there all my colors and also my font sizes and text styles.

my theme.dart looks like this :

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

//COLORS
const colorPrimary = Color(0xFF913f91);
const colorPrimaryLight = Color(0xFFed74ed);
const colorSecondary = Color(0xFF1fb4ac);
const colorSecondaryLight = Color(0xFF3ee6dc);
const colorTertiary = Color(0xFFff8519);
const colorTertiaryLight = Color(0xFFf7a963);
const colorPositive = Color(0xFF00d96f);
const colorNegative = Color(0xFFFF4464);
const colorNegativeDark = Color(0xFFff8095);
const colorDisabled = Color(0xFFC1C1C1);
const colorOffWhite = Color(0xFFf2f2f2);
const colorOffBlack = Color(0xFF222222);
const colorText = Color(0xFF222222);
const colorAqua = Color(0xFF19c4fc);
const colorAquaDark = Color(0xFF09a7db);
const colorDarkBlue = Color(0xFF00649e);
const colorBlue = Color(0xFF1fadff);
const colorYellow = Color(0xFFfff021);
const colorYellowLight = Color(0xFFfff354);
const gradientTop = Color(0xFFFFD560);
const gradientBottom = Color(0xFFFF8519);
const colorShadowDark = Color(0xFF090909);


//FONT SIZE
const fontSizeRegular = 18.0;
const fontSizeSmall = 12.0;
const fontSizeMedium = 15.0;
const fontSizeSmallest = 10.0;
const fontSizeLarge = 22.0;
const fontExtraLarge = 30.0;

//TEXT STYLES
textHeaderStyle(Color _color, FontStyle _fontStyle, FontWeight _fontWeight) {
  return TextStyle(
      fontSize: ScreenUtil().setSp(fontSizeLarge),
      color: _color,
      fontStyle: _fontStyle,
      fontWeight: _fontWeight);
}

for example, I have a switchlet's say in the "settings" menu to toggle between dark and light mode.

what would be the best approach?

If I add a condition on the colors like:

const colorPrimary = mode == "light" ? Color(0xFF913f91) : Color(0xFFed74ed);

wherein "mode" is a global variable and I add a setState() every time I toggle the switch in the "settings" menu, will it work?

I'll gladly try all of your suggestions.

Thanks for those who can help!

1 Answers1

0

The simplier solution I can think of; you can use a bool to choice theme something mode as you call and if it is true you can set your bright theme colors, if false dark theme colors and the key part you need to store it with shared preferences to apply whole app and make it permanent, I mean user setting can be set again even user close to app then restart it.

So you can use the bool in your theme dart:

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

    //COLORS
    colorPrimary = mode ? Color(0xFFed74ed) : Color(0xFF913f91);
    colorSecondary = mode ? Color(0xFF3ee6dc) : Color(0xFF1fb4ac);
    colorTertiary = mode ? Color(0xFFf7a963): Color(0xFFff8519);
    ...

Then you don't need to make a global variable just you said you can add a setState() every time you toggle the switch in the "settings" menu..

Lunedor
  • 1,410
  • 1
  • 14
  • 33
  • you mean I have to put conditions in my widgets? If I already have a large app, that would not be the best approach. but I like the idea. thanks for your suggestion. – Claude Dubouzet May 22 '20 at 11:47
  • No you don't have to put it in your all widgets, you can use it in your theme dart file to determine the primary color etc. – Lunedor May 22 '20 at 11:49
  • const colorPrimary = mode == "light" ? Color(0xFF913f91) : Color(0xFFed74ed); like this? can you show an example condition? – Claude Dubouzet May 22 '20 at 11:55
  • I am not sure you can use `const` with a condition so I would prefer like that: `colorPrimary = mode ? Color(0xFF913f91) : Color(0xFFed74ed);` – Lunedor May 22 '20 at 12:03
  • it didn't work. I used ` //COLORS var colorPrimary = Constants.mode ? Color(0xFF913f91) : Color(0xFFed74ed);` then I tried updating the mode value in other parts of the app. not updating the color. – Claude Dubouzet May 22 '20 at 14:28
  • I don't know whole structure of your app but firstly when you change any color in your theme.dart it apply to all your widgets? – Lunedor May 22 '20 at 14:31
  • If I change color manually, while app is running, it changes. – Claude Dubouzet May 22 '20 at 14:40
  • all of the widgets are affected by the color change. – Claude Dubouzet May 22 '20 at 14:41
  • Can you add get preferences in your initState for every screen then try again(or just one for test). – Lunedor May 22 '20 at 14:41
  • sorry but I don't think it's necessary since setting the state of `mode` doesn't update the color. It should update the color then I save it on shared pref. but it's not working. =( – Claude Dubouzet May 22 '20 at 14:48
  • I tried with a simple app in my own and you are right, that needs provider or another state method to apply this change for your another dart file, so as I can suggest after a little research using some library such as https://pub.dev/packages/theme_provider would be easier in my opinion. – Lunedor May 22 '20 at 15:15
  • I've actually found this package [link](https://pub.dev/packages/dynamic_theme#-example-tab-) – Claude Dubouzet May 22 '20 at 15:29