Skip to content

Commit

Permalink
fix: [DX-988] Fix text controller styles (#515)
Browse files Browse the repository at this point in the history
  • Loading branch information
witwash authored Jan 18, 2024
1 parent ba73c9b commit 2ff7279
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
17 changes: 17 additions & 0 deletions optimus/lib/src/form/date_input_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ class _OptimusDateInputFieldState extends State<OptimusDateInputField>
@override
void didUpdateWidget(covariant OptimusDateInputField oldWidget) {
super.didUpdateWidget(oldWidget);

if (widget.isEnabled != oldWidget.isEnabled) {
_updateControllerStyle();
}

if (oldWidget.value != widget.value) {
_updateControllerValue(widget.value);
} else if (oldWidget.format.pattern != widget.format.pattern ||
Expand All @@ -95,6 +100,18 @@ class _OptimusDateInputFieldState extends State<OptimusDateInputField>
}
}

@override
void didChangeDependencies() {
_updateControllerStyle();
super.didChangeDependencies();
}

void _updateControllerStyle() {
_controller
..inputStyle = _inputStyle
..placeholderStyle = _placeholderStyle;
}

void _updateControllerValue(DateTime? newValue) {
final formattedValue = _formatValue(newValue);
_controller
Expand Down
34 changes: 24 additions & 10 deletions optimus/lib/src/form/styled_input_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,34 @@ import 'package:flutter/widgets.dart';
class StyledInputController extends TextEditingController {
StyledInputController({
required String text,
required this.inputStyle,
required this.placeholderStyle,
}) : super.fromValue(
required TextStyle inputStyle,
required TextStyle placeholderStyle,
}) : _inputStyle = inputStyle,
_placeholderStyle = placeholderStyle,
super.fromValue(
// workaround for the issue with the cursor position on Android
TextEditingValue(
text: text,
selection: TextSelection.collapsed(offset: text.length),
),
);

/// The style to use for the user entered part.
final TextStyle inputStyle;
TextStyle _inputStyle;
TextStyle _placeholderStyle;

/// The style to use for the rest.
final TextStyle placeholderStyle;
set inputStyle(TextStyle value) {
if (value == _inputStyle) return;

_inputStyle = value;
notifyListeners();
}

set placeholderStyle(TextStyle value) {
if (value == _placeholderStyle) return;

_placeholderStyle = value;
notifyListeners();
}

@override
TextSpan buildTextSpan({
Expand All @@ -32,11 +45,12 @@ class StyledInputController extends TextEditingController {

for (int i = textParts.length - 1; i >= 0; i--) {
if (_allowedDigits.hasMatch(textParts[i])) {
children.add(TextSpan(style: inputStyle, text: textParts[i]));
children.add(TextSpan(style: _inputStyle, text: textParts[i]));
} else if (_maskRegExp.hasMatch(textParts[i])) {
children.add(TextSpan(style: placeholderStyle, text: textParts[i]));
children.add(TextSpan(style: _placeholderStyle, text: textParts[i]));
} else {
final style = children.isEmpty ? placeholderStyle : children.last.style;
final style =
children.isEmpty ? _placeholderStyle : children.last.style;
children.add(TextSpan(style: style, text: textParts[i]));
}
}
Expand Down

0 comments on commit 2ff7279

Please sign in to comment.