Skip to content

Commit

Permalink
Add setting to control the casing of material3 text
Browse files Browse the repository at this point in the history
  • Loading branch information
Lance Johnstone authored and Lance Johnstone committed Jan 31, 2024
1 parent 054f5b2 commit 95d864f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/src/platform_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,19 @@ class PlatformSettingsData {
/// Set to true (wrapped) by default which could be a breaking change. If it is then set this value to false
final bool wrapCupertinoAppBarMiddleWithMediaQuery;

/// With Material3 spec defining buttons text as being non ALL CAPS (see https://m3.material.io/components/buttons/overview#388f6096-c34d-4a22-8b1f-11ad69963f7c)
/// If this is set to true then using [PlatformText] with material3 style will default to sentence case and not ALL CAPS. Material 2 will continue to be ALL CAPS
/// If this is set to false then it will default to previous behavour of ALL CAPS.
/// Going forward [PlatformText] will likely be removed when the material3 property on ThemeData is removed and all material widgets will assume material3 style.
final bool matchMaterialCaseForPlatformText;

PlatformSettingsData({
this.iosUsesMaterialWidgets = false,
this.iosUseZeroPaddingForAppbarPlatformIcon = false,
this.legacyIosUsesMaterialWidgets = false,
this.platformStyle = const PlatformStyleData(),
this.wrapCupertinoAppBarMiddleWithMediaQuery = true,
this.matchMaterialCaseForPlatformText = true,
});
}

Expand Down
15 changes: 15 additions & 0 deletions lib/src/platform_text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,27 @@

import 'dart:ui' as ui show TextHeightBehavior;

import 'package:flutter/material.dart' show Theme;
import 'package:flutter/widgets.dart';

import 'platform.dart' show isMaterial;
import 'platform_provider.dart' show PlatformProvider;
import 'platform_theme.dart' show PlatformTheme;

String formatData(BuildContext context, String data) {
if (isMaterial(context)) {
final providerState = PlatformProvider.of(context);
final matchMaterialCaseForPlatformText =
providerState?.settings.matchMaterialCaseForPlatformText ?? true;

final m3 = PlatformTheme.of(context)?.isMaterial3 ??
Theme.of(context).useMaterial3;

// If it material3 and we want to match the casing as defined for material3 then do not return ALL CAPS
if (m3 && matchMaterialCaseForPlatformText) {
return data;
}

return data.toUpperCase();
}
return data;
Expand Down
5 changes: 5 additions & 0 deletions lib/src/platform_theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ class _PlatformThemeState extends State<PlatformTheme>
applyToBothDarkAndLightTheme: applyToBothDarkAndLightTheme);
}

bool get isMaterial3 =>
(isDark ? _materialDarkTheme : _materialLightTheme)?.useMaterial3 ??
false;

void _setMaterialThemeType({
required bool? useMaterial3,
bool applyToBothDarkAndLightTheme = false,
Expand Down Expand Up @@ -167,6 +171,7 @@ class PlatformThemeState {
set themeMode(ThemeMode? themeMode) => _parent.themeMode = themeMode;

bool get isDark => _parent.isDark;
bool get isMaterial3 => _parent.isMaterial3;

CupertinoThemeData? get cupertinoLightTheme => _parent._cupertinoLightTheme;
CupertinoThemeData? get cupertinoDarkTheme => _parent._cupertinoDarkTheme;
Expand Down

0 comments on commit 95d864f

Please sign in to comment.