Files
hash-wallet/lib/src/screens/dev/shared_preferences_page.dart
T
df88914628 New themes (#2239)
* Add theme base v2

* Initial new theme base files

* Typos

* Fixes

* Update theme files

* feat: Migrate to Material 3 Theming

Foundation, Theme Data Refactor, and First Extension Cleanup Batch.

This commit completes the first major phase of migrating to Material 3 theming by setting up the foundations for material 3 integration and begins the initial migration, removing custom theme extensions, updating theme data, and refactoring all relevant widget and page theming to use Material 3’s built-in color and typography tokens.

These changes:
- Lays the groundwork for Material 3 theming by restructuring the app’s theme configuration to use Material 3’s ColorScheme and TextTheme as the primary sources of color and typography throughout the app.
- Refactors the core theme config files by removing legacy custom color roles ensuring all color definitions now map directly to Material 3’s role.
- Begins the first batch migration of custom theme extensions (InfoTheme, PlaceholderTheme, KeyboardTheme, PinCodeTheme) and updates all affected widgets and pages to use Material 3 color and typography tokens instead of the custom properties.
- Cleans up the codebase by deleting the files of the initial set of migrated extensions and eliminating all related imports and usages.

* feat: Migrate to Material 3 Theming.

This change:
- Updates the themes README.md file to reflect the current structure and give more information based on the first major phase that was completed.

* feat: Migrate to Material 3 Theming

Deleting previous theme extensions

* feat: Migrate to Material 3 Theming

Another batch of migrations from existing extensions

* feat: Migration to Material 3 Theming

Third Migration batch for theme extensions

* fwat: Migration to Material 3 Theming

Final Migration batch for previous theme extensions

* Update onboarding hero

* Update button radius

* Add surfaceContainer to light theme

* feat(themes): Migrate to Material 3 Theming

This change:
- Adds new set of hero images
- Modifies the core structure for the themes
- Add missing color tokens to the theme classes
- Adds a CustomThemeColor class for custom color tokens
- Modifies the themelist to have a fall back for previous theme implementation
- Adds localization for some texts
- Modifies the flow for loading the theme on app start
- Add a WidgetsBindingObserver that listens for changes in the device theme and updates the app theme when there is a change
- Registers the themeStore as a Singleton for codebase wide use

* feat(themes): Migrate to Material 3 theming

This change:
- Migrates UI flows across the app to the new themes
- Confirms styling and typography of components across the app uses the new themes
- Remove instances of Palette use
- Switch TextStyles across the app to use theme text styles

* feat(themes): Migrate to Material 3 Theming.

This change:
- Adjusts bottomsheets styling and removes duplicate close button
- Removes more themedata extensions from the previous implementation

* - Remove outlines from cards and dock
- Update menu colors
- Update padding/divider size for cards

* - Update PIN screen
- Fix navigation dock shadow
- Update wallet screen colors

* Update border radius --skip-ci

* feat(themes): Migrate to Material 3 Theming.

This change:
- Adds gradient backgrounds to the dashbaord and balance cards.
- Migrates the input fields across the app to BaseTextFormFields.
- Removes dependence of input fields on individual styling, focusing instead on using theme defined InputDecoration styling with adjustments on individual components where needed.
- Applies new theme styling to BaseTextFormField, AddressTextField and CurrencyAmountTextField.

* - Switch some hero images to PNG
- Fix nagivation_dock shadow
- Minor fixes

* feat: Add fallback to previous underline styling in central widgets

This change:
- Adds a fallback to CurrencyAmountTextField, AddressTextField, and BaseTextFormField, allowing them use the previous theme styling.
- Adds localization for new texts

* feat(themes): Update warning box colors for dark and light themes

* feat(themes): Relaod themes when user restores from backup, ensuring the user previous theme preference is used.

* feat(themes): Handle themes logic during restore from backup

This change:
- Refactors theme loading logic to handle backup restore edgecase
- Refreshes the theme based on the user saved preference during restore from backup flow

* Fix card gradients and spacing

* Fix even more radiuses
Test new icons for navigation_dock.dart

* Update onboarding flow backgrounds
Fix swap icon clipping
Fix some text colors
Add more hero images

* Fix incorrect color for light theme

* Fix more hero images and cleanup

* Update text field icons
Fix info box CTA colors
Fix sync indicator colors

* Update toggle colors
Update dark theme colors (minor)
Update crypto_balance_widget.dart icon

* Update page transitions in router.dart
Fix some colors

* feat(themes): Display label by default for filled textfields

* feat(themes): Refactor theme handling across various components

This change:
- Fixes issue with themeMode resetting to system mode when app is restarted causing a UI glitch
- Updates theme checks from `currentTheme.type == ThemeType.dark` to `currentTheme.isDark` for consistency
- Adjusts UI components to use the theme directly from the themeStore

* feat(themes): Add animating tagline to the create pin welcome screen

* Revert text fields label temporarily, fix a couple colors, and cleanup some images

---------

Co-authored-by: OmarHatem <omarh.ismail1@gmail.com>
Co-authored-by: Blazebrain <davidadegoke16@gmail.com>
2025-05-25 23:11:45 +03:00

406 lines
14 KiB
Dart

import 'package:cake_wallet/src/screens/base_page.dart';
import 'package:cake_wallet/src/widgets/base_text_form_field.dart';
import 'package:cake_wallet/view_model/dev/shared_preferences.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
class DevSharedPreferencesPage extends BasePage {
final DevSharedPreferences viewModel;
DevSharedPreferencesPage(this.viewModel);
@override
String? get title => "[dev] shared preferences";
@override
Widget? trailing(BuildContext context) {
return IconButton(
icon: Icon(Icons.add),
onPressed: () => _showCreateDialog(context),
);
}
@override
Widget body(BuildContext context) {
return Observer(
builder: (_) {
if (viewModel.sharedPreferences == null) {
return Center(child: Text("No shared preferences found"));
}
final keys = viewModel.keys;
Map<String, dynamic> values = {};
for (final key in keys) {
values[key] = viewModel.get(key);
}
Map<String, PreferenceType> types = {};
for (final key in keys) {
types[key] = viewModel.getPreferenceType(key);
}
return ListView.builder(
itemCount: keys.length,
itemBuilder: (context, index) {
final key = keys[index];
final type = types[key]!;
return ListTile(
onTap: () {
Clipboard.setData(ClipboardData(text: key + ": " + values[key].toString()));
},
onLongPress: () {
_showEditDialog(context, key, type, values[key]);
},
title: switch (type) {
PreferenceType.bool => Text(key, style: Theme.of(context).textTheme.bodyMedium!.copyWith(color: Colors.blue)),
PreferenceType.int => Text(key, style: Theme.of(context).textTheme.bodyMedium!.copyWith(color: Colors.green)),
PreferenceType.double => Text(key, style: Theme.of(context).textTheme.bodyMedium!.copyWith(color: Colors.yellow)),
PreferenceType.listString => Text(key, style: Theme.of(context).textTheme.bodyMedium!.copyWith(color: Colors.purple)),
PreferenceType.string => Text(key),
PreferenceType.unknown => Text(key),
},
subtitle: switch (type) {
PreferenceType.bool => Text("bool: ${values[key]}"),
PreferenceType.int => Text("int: ${values[key]}"),
PreferenceType.double => Text("double: ${values[key]}"),
PreferenceType.listString => values[key].isEmpty as bool ? Text("listString: []") : Text("listString:\n- ${values[key].join("\n- ")}"),
PreferenceType.string => Text("string: ${values[key]}"),
PreferenceType.unknown => Text("UNKNOWN(${values[key].runtimeType}): ${values[key]}"),
},
);
},
);
},
);
}
void _showEditDialog(BuildContext context, String key, PreferenceType type, dynamic currentValue) {
dynamic newValue = currentValue;
bool isListString = type == PreferenceType.listString;
List<String> listItems = isListString ? List<String>.from(currentValue as Iterable<dynamic>) : [];
TextEditingController textController = TextEditingController(
text: isListString ? '' : currentValue?.toString() ?? '');
showDialog(
context: context,
builder: (BuildContext context) {
return StatefulBuilder(
builder: (context, setState) {
return AlertDialog(
title: Text('Edit $key'),
content: SizedBox(
width: double.maxFinite,
height: double.maxFinite,
child: SingleChildScrollView(
child: _buildDialogContent(
type,
newValue,
listItems,
textController,
(value) => setState(() => newValue = value),
(items) => setState(() => listItems = items),
),
),
),
actions: <Widget>[
TextButton(
child: Text('Delete'),
style: TextButton.styleFrom(foregroundColor: Colors.red),
onPressed: () {
_showDeleteConfirmation(context, key);
},
),
TextButton(
child: Text('Cancel'),
onPressed: () => Navigator.of(context).pop(),
),
TextButton(
child: Text('Save'),
onPressed: () async {
if (_validateAndUpdateValue(
context,
type,
textController,
listItems,
(value) => newValue = value
)) {
await viewModel.set(key, type, newValue);
Navigator.of(context).pop();
}
},
),
],
);
},
);
},
);
}
void _showDeleteConfirmation(BuildContext context, String key) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Delete Preference'),
content: Text('Are you sure you want to delete "$key"?'),
actions: <Widget>[
TextButton(
child: Text('Cancel'),
onPressed: () => Navigator.of(context).pop(),
),
TextButton(
child: Text('Delete'),
style: TextButton.styleFrom(foregroundColor: Colors.red),
onPressed: () {
viewModel.delete(key);
Navigator.of(context).pop();
Navigator.of(context).pop();
},
),
],
);
},
);
}
Widget _buildDialogContent(
PreferenceType type,
dynamic value,
List<String> listItems,
TextEditingController textController,
Function(dynamic) onValueChanged,
Function(List<String>) onListChanged,
) {
return switch (type) {
PreferenceType.bool => _buildBoolEditor(value as bool, onValueChanged),
PreferenceType.int => _buildNumberEditor(textController, 'Integer value', true),
PreferenceType.double => _buildNumberEditor(textController, 'Double value', false),
PreferenceType.string => _buildTextEditor(textController),
PreferenceType.listString => _buildListEditor(listItems, textController, onListChanged),
PreferenceType.unknown => Text('Cannot edit unknown type'),
};
}
Widget _buildBoolEditor(bool value, Function(bool) onChanged) {
return CheckboxListTile(
title: Text('Value'),
value: value,
onChanged: (newValue) {
if (newValue != null) onChanged(newValue);
},
);
}
Widget _buildTextEditor(TextEditingController controller) {
return Column(
mainAxisSize: MainAxisSize.min,
children: [
BaseTextFormField(
controller: controller,
hintText: 'String value',
maxLines: null,
),
],
);
}
Widget _buildNumberEditor(TextEditingController controller, String label, bool isInteger) {
return Column(
mainAxisSize: MainAxisSize.min,
children: [
BaseTextFormField(
controller: controller,
hintText: label,
keyboardType: isInteger
? TextInputType.number
: TextInputType.numberWithOptions(decimal: true),
inputFormatters: isInteger
? [FilteringTextInputFormatter.digitsOnly]
: [FilteringTextInputFormatter.allow(RegExp(r'^\d*\.?\d*$'))],
),
],
);
}
Widget _buildListEditor(
List<String> items,
TextEditingController controller,
Function(List<String>) onListChanged,
) {
return Column(
mainAxisSize: MainAxisSize.min,
children: [
SizedBox(
height: 200,
child: ReorderableListView(
shrinkWrap: true,
children: [
for (int i = 0; i < items.length; i++)
ListTile(
key: Key('$i'),
title: Text(items[i]),
trailing: IconButton(
icon: Icon(Icons.delete),
onPressed: () {
final newList = List<String>.from(items);
newList.removeAt(i);
onListChanged(newList);
},
),
)
],
onReorder: (int oldIndex, int newIndex) {
final newList = List<String>.from(items);
if (oldIndex < newIndex) {
newIndex -= 1;
}
final item = newList.removeAt(oldIndex);
newList.insert(newIndex, item);
onListChanged(newList);
},
),
),
Row(
children: [
Expanded(
child: BaseTextFormField(
controller: controller,
hintText: 'New item',
),
),
IconButton(
icon: Icon(Icons.add),
onPressed: () {
if (controller.text.isNotEmpty) {
final newList = List<String>.from(items);
newList.add(controller.text);
onListChanged(newList);
controller.clear();
}
},
),
],
),
],
);
}
bool _validateAndUpdateValue(
BuildContext context,
PreferenceType type,
TextEditingController controller,
List<String> listItems,
Function(dynamic) setNewValue,
) {
switch (type) {
case PreferenceType.int:
if (controller.text.isNotEmpty) {
try {
setNewValue(int.parse(controller.text));
} catch (e) {
_showErrorMessage(context, 'Invalid integer value');
return false;
}
}
break;
case PreferenceType.double:
if (controller.text.isNotEmpty) {
try {
setNewValue(double.parse(controller.text));
} catch (e) {
_showErrorMessage(context, 'Invalid double value');
return false;
}
}
break;
case PreferenceType.string:
setNewValue(controller.text);
break;
case PreferenceType.listString:
setNewValue(listItems);
break;
default:
break;
}
return true;
}
void _showErrorMessage(BuildContext context, String message) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(message)),
);
}
void _showCreateDialog(BuildContext context) {
PreferenceType selectedType = PreferenceType.string;
TextEditingController keyController = TextEditingController();
showDialog(
context: context,
builder: (BuildContext context) {
return StatefulBuilder(
builder: (context, setState) {
return AlertDialog(
title: Text('Create Preference'),
content: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
BaseTextFormField(
controller: keyController,
hintText: 'Preference Key',
),
SizedBox(height: 16),
DropdownButtonFormField<PreferenceType>(
value: selectedType,
decoration: InputDecoration(labelText: 'Type'),
items: [
DropdownMenuItem(value: PreferenceType.string, child: Text('String')),
DropdownMenuItem(value: PreferenceType.bool, child: Text('Boolean')),
DropdownMenuItem(value: PreferenceType.int, child: Text('Integer')),
DropdownMenuItem(value: PreferenceType.double, child: Text('Double')),
DropdownMenuItem(value: PreferenceType.listString, child: Text('List of Strings')),
],
onChanged: (value) {
if (value != null) {
setState(() {
selectedType = value;
});
}
},
),
],
),
),
actions: <Widget>[
TextButton(
child: Text('Cancel'),
onPressed: () => Navigator.of(context).pop(),
),
TextButton(
child: Text('Create'),
onPressed: () {
if (keyController.text.isEmpty) {
_showErrorMessage(context, 'Key cannot be empty');
return;
}
viewModel.set(keyController.text, selectedType, switch (selectedType) {
PreferenceType.bool => false,
PreferenceType.int => 0,
PreferenceType.double => 0.0,
PreferenceType.string => '',
PreferenceType.listString => [],
PreferenceType.unknown => null,
});
Navigator.of(context).pop();
},
),
],
);
},
);
},
);
}
}