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(cat-voices): theme mode switch #927

Merged
merged 4 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .config/dictionaries/project.dic
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,4 @@ xctestrun
xcworkspace
xvfb
yoroi
appbar
35 changes: 26 additions & 9 deletions catalyst_voices/lib/app/view/app_content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,30 @@ import 'package:flutter_localized_locales/flutter_localized_locales.dart';

const _restorationScopeId = 'rootVoices';

final class AppContent extends StatelessWidget {
final class AppContent extends StatefulWidget {
final RouterConfig<Object> routerConfig;

const AppContent({
super.key,
required this.routerConfig,
});

List<LocalizationsDelegate<dynamic>> get _localizationsDelegates {
return const [
...VoicesLocalizations.localizationsDelegates,
LocaleNamesLocalizationsDelegate(),
];
@override
State<AppContent> createState() => AppContentState();

/// Returns the state associated with the [AppContent].
static AppContentState of(BuildContext context) {
return context.findAncestorStateOfType<AppContentState>()!;
}
}

class AppContentState extends State<AppContent> {
ThemeMode _themeMode = ThemeMode.light;

void updateThemeMode(ThemeMode themeMode) {
setState(() {
_themeMode = themeMode;
});
}

@override
Expand All @@ -28,9 +39,8 @@ final class AppContent extends StatelessWidget {
localizationsDelegates: _localizationsDelegates,
supportedLocales: VoicesLocalizations.supportedLocales,
localeListResolutionCallback: basicLocaleListResolution,
routerConfig: routerConfig,
// Light mode is "go to" for now.
themeMode: ThemeMode.light,
routerConfig: widget.routerConfig,
themeMode: _themeMode,
theme: ThemeBuilder.buildTheme(
brand: Brand.catalyst,
brightness: Brightness.light,
Expand All @@ -46,4 +56,11 @@ final class AppContent extends StatelessWidget {
},
);
}

List<LocalizationsDelegate<dynamic>> get _localizationsDelegates {
return const [
...VoicesLocalizations.localizationsDelegates,
LocaleNamesLocalizationsDelegate(),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:catalyst_voices/app/app.dart';
import 'package:catalyst_voices/widgets/widgets.dart';
import 'package:flutter/material.dart';

/// A switch that updates the app theme mode.
class SpacesThemeModeSwitch extends StatelessWidget {
const SpacesThemeModeSwitch({super.key});

@override
Widget build(BuildContext context) {
return VoicesThemeModeSwitch(
onChanged: AppContent.of(context).updateThemeMode,
);
}
}
2 changes: 2 additions & 0 deletions catalyst_voices/lib/pages/spaces/spaces_shell_page.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:catalyst_voices/common/ext/ext.dart';
import 'package:catalyst_voices/pages/registration/registration_dialog.dart';
import 'package:catalyst_voices/pages/spaces/appbar/spaces_theme_mode_switch.dart';
import 'package:catalyst_voices/pages/spaces/drawer/spaces_drawer.dart';
import 'package:catalyst_voices/widgets/widgets.dart';
import 'package:catalyst_voices_blocs/catalyst_voices_blocs.dart';
Expand Down Expand Up @@ -62,6 +63,7 @@ class _SpacesShellPageState extends State<SpacesShellPage> {
leading: isVisitor ? null : const DrawerToggleButton(),
automaticallyImplyLeading: false,
actions: [
const SpacesThemeModeSwitch(),
SessionActionHeader(
onGetStartedTap: _showAccountSetup,
),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'package:catalyst_voices_localization/catalyst_voices_localization.dart';
import 'package:flutter/material.dart';

/// A switch that toggles between light & dark theme mode.
class VoicesThemeModeSwitch extends StatelessWidget {
final ValueChanged<ThemeMode> onChanged;

const VoicesThemeModeSwitch({
super.key,
required this.onChanged,
});

@override
Widget build(BuildContext context) {
return Row(
mainAxisSize: MainAxisSize.min,
children: [
Text('${context.l10n.themeLight} / ${context.l10n.themeDark}'),
const SizedBox(width: 8),
Switch(
dtscalac marked this conversation as resolved.
Show resolved Hide resolved
value: Theme.of(context).brightness == Brightness.dark,
onChanged: (value) {
onChanged(
value ? ThemeMode.dark : ThemeMode.light,
);
},
),
],
);
}
}
1 change: 1 addition & 0 deletions catalyst_voices/lib/widgets/widgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,6 @@ export 'toggles/voices_checkbox.dart';
export 'toggles/voices_checkbox_group.dart';
export 'toggles/voices_radio.dart';
export 'toggles/voices_switch.dart';
export 'toggles/voices_theme_mode_switch.dart';
export 'tooltips/voices_plain_tooltip.dart';
export 'tooltips/voices_rich_tooltip.dart';
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,18 @@ abstract class VoicesLocalizations {
/// **'Total'**
String get total;

/// Refers to a light theme mode.
///
/// In en, this message translates to:
/// **'Light'**
String get themeLight;

/// Refers to a dark theme mode.
///
/// In en, this message translates to:
/// **'Dark'**
String get themeDark;

/// A title on keychain deleted dialog
///
/// In en, this message translates to:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,12 @@ class VoicesLocalizationsEn extends VoicesLocalizations {
@override
String get total => 'Total';

@override
String get themeLight => 'Light';

@override
String get themeDark => 'Dark';

@override
String get keychainDeletedDialogTitle => 'Catalyst keychain removed';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,12 @@ class VoicesLocalizationsEs extends VoicesLocalizations {
@override
String get total => 'Total';

@override
String get themeLight => 'Light';

@override
String get themeDark => 'Dark';

@override
String get keychainDeletedDialogTitle => 'Catalyst keychain removed';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,14 @@
"yes": "Yes",
"no": "No",
"total": "Total",
"themeLight": "Light",
"@themeLight": {
"description": "Refers to a light theme mode."
},
"themeDark": "Dark",
"@themeDark": {
"description": "Refers to a dark theme mode."
},
"keychainDeletedDialogTitle": "Catalyst keychain removed",
"@keychainDeletedDialogTitle": {
"description": "A title on keychain deleted dialog"
Expand Down
22 changes: 6 additions & 16 deletions catalyst_voices/uikit_example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:catalyst_voices/widgets/toggles/voices_theme_mode_switch.dart';
import 'package:catalyst_voices_blocs/catalyst_voices_blocs.dart';
import 'package:catalyst_voices_brands/catalyst_voices_brands.dart';
import 'package:catalyst_voices_localization/generated/catalyst_voices_localizations.dart';
Expand Down Expand Up @@ -92,22 +93,11 @@ class _ThemeModeSwitcherWrapper extends StatelessWidget {
color: Theme.of(context).colorScheme.surface,
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 32, vertical: 16),
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
const Text('Light / Dark'),
Padding(
padding: const EdgeInsets.only(left: 8),
child: Switch(
value: Theme.of(context).brightness == Brightness.dark,
onChanged: (value) {
onChanged(
value ? ThemeMode.dark : ThemeMode.light,
);
},
),
),
],
child: Align(
alignment: Alignment.centerRight,
child: VoicesThemeModeSwitch(
onChanged: onChanged,
),
),
),
),
Expand Down
Loading