Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
✨ Add central TextStyle defaults to ThemeService
Browse files Browse the repository at this point in the history
  • Loading branch information
999eagle committed Aug 16, 2019
1 parent 98dbf0d commit b0f8bd7
Show file tree
Hide file tree
Showing 16 changed files with 98 additions and 58 deletions.
Binary file added assets/fonts/DejaVuSans-Bold.ttf
Binary file not shown.
Binary file added assets/fonts/DejaVuSans-ExtraLight.ttf
Binary file not shown.
Binary file added assets/fonts/DejaVuSans.ttf
Binary file not shown.
7 changes: 5 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,13 @@ TextTheme _defaultTextTheme() {
// This text theme is merged with the default theme in the `TextData`
// constructor. This makes sure that the emoji font is used as fallback for
// every text that uses the default theme.
var style;

// Make sure this is the same as in lib/services/theme.dart ThemeService()
var fontFamilyFallback = ['NunitoSans', 'DejavuSans'];
if (isOnDesktop) {
style = new TextStyle(fontFamilyFallback: ['Emoji']);
fontFamilyFallback = ['NunitoSans', 'Emoji', 'DejavuSans'];
}
var style = new TextStyle(fontFamilyFallback: fontFamilyFallback);
return new TextTheme(
body1: style,
body2: style,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,15 @@ class OBCreatePostText extends StatelessWidget {
textCapitalization: TextCapitalization.sentences,
keyboardType: TextInputType.multiline,
maxLines: null,
style: TextStyle(
color: themeValueParserService.parseColor(theme.primaryTextColor),
fontSize: 18.0),
style: themeService.getThemedTextStyle(theme).merge(TextStyle(
fontSize: 18.0)),
decoration: InputDecoration(
border: InputBorder.none,
hintText: this.hintText != null ? this.hintText : 'What\'s going on?',
hintStyle: TextStyle(
hintStyle: themeService.getDefaultTextStyle().merge(TextStyle(
color: themeValueParserService
.parseColor(theme.secondaryTextColor),
fontSize: 18.0)),
fontSize: 18.0))),
autocorrect: true,
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ class OBRemainingPostCharacters extends StatelessWidget {

return Text(
remainingCharacters.toString(),
style: TextStyle(
style: themeService.getDefaultTextStyle().merge(TextStyle(
fontSize: 12.0,
color: exceededMaxCharacters
? themeValueParserService.parseColor(theme.dangerColor)
: themeValueParserService
.parseColor(theme.primaryTextColor),
fontWeight: exceededMaxCharacters
? FontWeight.bold
: FontWeight.normal),
: FontWeight.normal)),
);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:Okuna/services/theme_value_parser.dart';
import 'package:Okuna/widgets/alerts/alert.dart';
import 'package:Okuna/widgets/avatars/avatar.dart';
import 'package:Okuna/widgets/tabs/image_tab.dart';
import 'package:Okuna/widgets/theming/text.dart';
import 'package:flutter/material.dart';

class OBUserAvatarTab extends StatelessWidget {
Expand Down Expand Up @@ -53,7 +54,7 @@ class OBUserAvatarTab extends StatelessWidget {
Radius.circular(OBImageTab.borderRadius),
bottomRight:
Radius.circular(OBImageTab.borderRadius))),
child: Text(
child: OBText(
'You',
textAlign: TextAlign.center,
style: TextStyle(color: Colors.white, fontSize: 16),
Expand Down
4 changes: 2 additions & 2 deletions lib/pages/home/widgets/bottom-tab-bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,13 @@ class OBCupertinoTabBar extends StatelessWidget implements PreferredSizeWidget {
),
child: DefaultTextStyle(
// Default with the inactive state.
style: TextStyle(
style: themeService.getDefaultTextStyle().merge(TextStyle(
fontFamily: '.SF UI Text',
fontSize: 10.0,
letterSpacing: 0.1,
fontWeight: FontWeight.w400,
color: inactiveColor,
),
)),
child: Padding(
padding: EdgeInsets.only(bottom: bottomPadding),
child: Row(
Expand Down
1 change: 1 addition & 0 deletions lib/provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ class OpenbookProviderState extends State<OpenbookProvider> {
validationService
.setConnectionsCirclesApiService(connectionsCirclesApiService);
themeService.setStorageService(storageService);
themeService.setThemeValueParserService(themeValueParserService);
pushNotificationsService.setUserService(userService);
intercomService.setUserService(userService);
dialogService.setThemeService(themeService);
Expand Down
34 changes: 34 additions & 0 deletions lib/services/theme.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import 'package:Okuna/main.dart';
import 'package:Okuna/models/theme.dart';
import 'package:Okuna/services/storage.dart';
import 'package:Okuna/services/theme_value_parser.dart';
import 'package:flutter/widgets.dart';
import 'package:rxdart/rxdart.dart';
import 'dart:math';

Expand All @@ -12,6 +15,9 @@ class ThemeService {
OBTheme _activeTheme;

OBStorage _storage;
ThemeValueParserService _themeValueParserService;

TextStyle _defaultTextStyle;

List<OBTheme> _themes = [
OBTheme(
Expand Down Expand Up @@ -172,13 +178,28 @@ class ThemeService {

ThemeService() {
_setActiveTheme(_themes[2]);
// Make sure this is the same as in lib/main.dart _getDefaultTextTheme()
var fontFamilyFallback = ['NunitoSans', 'DejaVuSans'];
if (isOnDesktop) {
fontFamilyFallback = ['NunitoSans', 'Emoji', 'DejaVuSans'];
}
_defaultTextStyle = TextStyle(
fontFamily: 'NunitoSans',
fontFamilyFallback: fontFamilyFallback,
fontStyle: FontStyle.normal,
fontWeight: FontWeight.normal);
}

void setStorageService(StorageService storageService) {
_storage = storageService.getSystemPreferencesStorage(namespace: 'theme');
this._bootstrap();
}

void setThemeValueParserService(
ThemeValueParserService themeValueParserService) {
_themeValueParserService = themeValueParserService;
}

void setActiveTheme(OBTheme theme) {
_setActiveTheme(theme);
_storeActiveThemeId(theme.id);
Expand Down Expand Up @@ -231,4 +252,17 @@ class ThemeService {
while (length-- > 0) hex += chars[(random.nextInt(16)) | 0];
return hex;
}

TextStyle getDefaultTextStyle() {
return _defaultTextStyle;
}

TextStyle getTextStyle(TextStyle custom) {
return _defaultTextStyle.merge(custom);
}

TextStyle getThemedTextStyle(OBTheme theme) {
return getTextStyle(TextStyle(
color: _themeValueParserService.parseColor(theme.primaryTextColor)));
}
}
18 changes: 10 additions & 8 deletions lib/widgets/badges/badge.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ class OBBadge extends StatelessWidget {
gradient: primaryAccentColor,
borderRadius: BorderRadius.circular(50)),
child: Center(
child: count != null ? Text(
count.toString(),
style: TextStyle(
color: Colors.white,
fontSize: count < 10 ? 12 : 10,
fontWeight: FontWeight.bold),
) : const SizedBox()
),
child: count != null
? Text(
count.toString(),
style: themeService.getDefaultTextStyle().merge(
TextStyle(
color: Colors.white,
fontSize: count < 10 ? 12 : 10,
fontWeight: FontWeight.bold)),
)
: const SizedBox()),
);
});
}
Expand Down
17 changes: 8 additions & 9 deletions lib/widgets/theming/primary_accent_text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,15 @@ class OBPrimaryAccentText extends StatelessWidget {
builder: (BuildContext context, AsyncSnapshot<OBTheme> snapshot) {
var theme = snapshot.data;

TextStyle finalStyle = style;
TextStyle themedTextStyle = TextStyle(
foreground: Paint()
..shader = themeValueParserService
.parseGradient(theme.primaryAccentColor).createShader(Rect.fromLTWH(0.0, 0.0, 200.0, 70.0)));
TextStyle finalStyle = themeService.getDefaultTextStyle().merge(
TextStyle(
foreground: Paint()
..shader = themeValueParserService
.parseGradient(theme.primaryAccentColor)
.createShader(Rect.fromLTWH(0.0, 0.0, 200.0, 70.0))));

if (finalStyle != null) {
finalStyle = finalStyle.merge(themedTextStyle);
} else {
finalStyle = themedTextStyle;
if (style != null) {
finalStyle = style.merge(finalStyle);
}

return OBText(
Expand Down
14 changes: 6 additions & 8 deletions lib/widgets/theming/secondary_text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@ class OBSecondaryText extends StatelessWidget {
builder: (BuildContext context, AsyncSnapshot<OBTheme> snapshot) {
var theme = snapshot.data;

TextStyle finalStyle = style;
TextStyle themedTextStyle = TextStyle(
color:
themeValueParserService.parseColor(theme.secondaryTextColor));
TextStyle finalStyle = themeService.getDefaultTextStyle().merge(
TextStyle(
color: themeValueParserService
.parseColor(theme.secondaryTextColor)));

if (finalStyle != null) {
finalStyle = finalStyle.merge(themedTextStyle);
} else {
finalStyle = themedTextStyle;
if (style != null) {
finalStyle = style.merge(finalStyle);
}

return OBText(
Expand Down
21 changes: 7 additions & 14 deletions lib/widgets/theming/smart_text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -380,13 +380,9 @@ class OBSmartText extends StatelessWidget {
builder: (BuildContext context, AsyncSnapshot<OBTheme> snapshot) {
OBTheme theme = snapshot.data;

Color primaryTextColor =
themeValueParserService.parseColor(theme.primaryTextColor);

TextStyle textStyle = TextStyle(
color: primaryTextColor,
fontSize: fontSize,
fontFamilyFallback: ['NunitoSans']);
TextStyle textStyle = themeService
.getThemedTextStyle(theme)
.merge(TextStyle(fontSize: fontSize));

TextStyle secondaryTextStyle;

Expand All @@ -395,21 +391,18 @@ class OBSmartText extends StatelessWidget {
Color secondaryTextColor =
themeValueParserService.parseColor(theme.secondaryTextColor);
secondaryTextColor = TinyColor(secondaryTextColor).lighten(10).color;
secondaryTextStyle = TextStyle(
color: secondaryTextColor,
fontSize: fontSize * 0.8,
fontFamilyFallback: ['NunitoSans']);
secondaryTextStyle = textStyle.merge(
TextStyle(color: secondaryTextColor, fontSize: fontSize * 0.8));
}

Color actionsForegroundColor = themeValueParserService
.parseGradient(theme.primaryAccentColor)
.colors[1];

TextStyle smartItemsStyle = TextStyle(
TextStyle smartItemsStyle = textStyle.merge(TextStyle(
color: actionsForegroundColor,
fontSize: fontSize,
fontWeight: FontWeight.bold,
);
));

return RichText(
overflow: overflow,
Expand Down
13 changes: 6 additions & 7 deletions lib/widgets/theming/text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ class OBText extends StatelessWidget {
Widget build(BuildContext context) {
var openbookProvider = OpenbookProvider.of(context);
var themeService = openbookProvider.themeService;
var themeValueParserService = openbookProvider.themeValueParserService;

double fontSize = getTextSize(size);

Expand All @@ -57,12 +56,12 @@ class OBText extends StatelessWidget {
builder: (BuildContext context, AsyncSnapshot<OBTheme> snapshot) {
var theme = snapshot.data;

TextStyle themedTextStyle = TextStyle(
color: themeValueParserService.parseColor(theme.primaryTextColor),
fontFamilyFallback: ['NunitoSans'],
fontSize: (style != null && style.fontSize != null)
? style.fontSize
: fontSize);
TextStyle themedTextStyle = themeService
.getThemedTextStyle(theme)
.merge(TextStyle(
fontSize: (style != null && style.fontSize != null)
? style.fontSize
: fontSize));

if (style != null) {
themedTextStyle = themedTextStyle.merge(style);
Expand Down
11 changes: 11 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,17 @@ flutter:
style: normal
- asset: assets/fonts/NunitoSans-Bold.ttf
style: italic
- family: DejaVuSans
fonts:
- asset: assets/fonts/DejaVuSans.ttf
style: normal
weight: 400
- asset: assets/fonts/DejaVuSans-Bold.ttf
style: normal
weight: 700
- asset: assets/fonts/DejaVuSans-ExtraLight.ttf
style: normal
weight: 200
- family: VisbyRoundCF
fonts:
- asset: assets/fonts/VisbyRoundCF-Regular.otf
Expand Down

0 comments on commit b0f8bd7

Please sign in to comment.