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

Clear button autosuggest #240

Merged
merged 3 commits into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 6 additions & 9 deletions example/lib/screens/forms.dart
Original file line number Diff line number Diff line change
Expand Up @@ -193,15 +193,12 @@ class _FormsState extends State<Forms> {
const SizedBox(height: 20),
InfoLabel(
label: 'Selectable Text',
child: Mica(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: SelectableText(
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.',
selectionControls: fluentTextSelectionControls,
showCursor: true,
cursorWidth: 1.5,
),
child: Card(
child: SelectableText(
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.',
selectionControls: fluentTextSelectionControls,
showCursor: true,
cursorWidth: 1.5,
),
),
),
Expand Down
11 changes: 6 additions & 5 deletions lib/src/controls/form/auto_suggest_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,10 @@ class _AutoSuggestBoxState<T> extends State<AutoSuggestBox> {
final GlobalKey _textBoxKey = GlobalKey();

late TextEditingController controller;

final FocusScopeNode overlayNode = FocusScopeNode();

final clearGlobalKey = GlobalKey();

@override
void initState() {
super.initState();
Expand Down Expand Up @@ -293,14 +294,14 @@ class _AutoSuggestBoxState<T> extends State<AutoSuggestBox> {
clipBehavior:
_entry != null ? Clip.none : Clip.antiAliasWithSaveLayer,
prefix: widget.leadingIcon,
suffix: Row(children: [
clearGlobalKey: clearGlobalKey,
suffix: Row(mainAxisSize: MainAxisSize.min, children: [
if (widget.trailingIcon != null) widget.trailingIcon!,
if (widget.clearButtonEnabled &&
controller.text.isNotEmpty &&
focusNode.hasFocus)
if (widget.clearButtonEnabled && controller.text.isNotEmpty)
Padding(
padding: const EdgeInsetsDirectional.only(start: 2.0),
child: IconButton(
key: clearGlobalKey,
icon: const Icon(FluentIcons.chrome_close),
onPressed: () {
controller.clear();
Expand Down
148 changes: 91 additions & 57 deletions lib/src/controls/form/text_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@ class _TextBoxSelectionGestureDetectorBuilder

@override
void onSingleTapUp(TapUpDetails details) {
editableText.hideToolbar();
// Because TextSelectionGestureDetector listens to taps that happen on
// widgets in front of it, tapping the clear button will also trigger
// this handler. If the clear button widget recognizes the up event,
// then do not handle it.
if (_state._clearGlobalKey.currentContext != null) {
final RenderBox renderBox = _state._clearGlobalKey.currentContext!
.findRenderObject() as RenderBox;
.findRenderObject()! as RenderBox;
final Offset localOffset =
renderBox.globalToLocal(details.globalPosition);
if (renderBox.hitTest(BoxHitTestResult(), position: localOffset)) {
Expand All @@ -38,7 +43,7 @@ class _TextBoxSelectionGestureDetectorBuilder
}
super.onSingleTapUp(details);
_state._requestKeyboard();
if (_state.widget.onTap != null) _state.widget.onTap!();
_state.widget.onTap?.call();
}

@override
Expand Down Expand Up @@ -125,6 +130,7 @@ class TextBox extends StatefulWidget {
this.decoration,
this.foregroundDecoration,
this.highlightColor,
this.clearGlobalKey,
}) : assert(obscuringCharacter.length == 1),
smartDashesType = smartDashesType ??
(obscureText ? SmartDashesType.disabled : SmartDashesType.enabled),
Expand Down Expand Up @@ -483,6 +489,8 @@ class TextBox extends StatefulWidget {

final ButtonThemeData? iconButtonThemeData;

final GlobalKey? clearGlobalKey;

@override
_TextBoxState createState() => _TextBoxState();

Expand Down Expand Up @@ -560,7 +568,9 @@ class TextBox extends StatefulWidget {
class _TextBoxState extends State<TextBox>
with RestorationMixin, AutomaticKeepAliveClientMixin
implements TextSelectionGestureDetectorBuilderDelegate {
final GlobalKey _clearGlobalKey = GlobalKey();
final _localClearGlobalKey = GlobalKey();
GlobalKey get _clearGlobalKey =>
widget.clearGlobalKey ?? _localClearGlobalKey;

RestorableTextEditingController? _controller;
TextEditingController get _effectiveController =>
Expand Down Expand Up @@ -824,12 +834,6 @@ class _TextBoxState extends State<TextBox>
widget.keyboardAppearance ?? theme.brightness;
final Color cursorColor = widget.cursorColor ?? theme.inactiveColor;
final Color disabledColor = theme.disabledColor;
final Color backgroundColor = _effectiveFocusNode.hasFocus
? theme.scaffoldBackgroundColor
: AccentColor('normal', const {
'normal': Colors.white,
'dark': Color(0xFF2d2d2d),
}).resolve(context);

final TextStyle placeholderStyle = textStyle
.copyWith(
Expand Down Expand Up @@ -950,55 +954,59 @@ class _TextBoxState extends State<TextBox>
},
child: IgnorePointer(
ignoring: !enabled,
child: AnimatedContainer(
duration: theme.fasterAnimationDuration,
curve: theme.animationCurve,
decoration: BoxDecoration(
borderRadius: radius,
border: Border.all(
style: _effectiveFocusNode.hasFocus
? BorderStyle.solid
: BorderStyle.none,
width: 1,
color: theme.brightness.isLight
? const Color.fromRGBO(0, 0, 0, 0.08)
: const Color.fromRGBO(255, 255, 255, 0.07),
),
color: enabled
? backgroundColor
: theme.brightness.isLight
? const Color.fromRGBO(249, 249, 249, 0.3)
: const Color.fromRGBO(255, 255, 255, 0.04),
).copyWith(
backgroundBlendMode: widget.decoration?.backgroundBlendMode,
border: widget.decoration?.border,

/// This border radius can't be applied, otherwise the error "A borderRadius
/// can only be given for a uniform Border." will be thrown. Instead,
/// [radius] is already set to get the value from [widget.decoration?.borderRadius],
/// if any.
// borderRadius: widget.decoration?.borderRadius,
boxShadow: widget.decoration?.boxShadow,
color: widget.decoration?.color,
gradient: widget.decoration?.gradient,
image: widget.decoration?.image,
shape: widget.decoration?.shape,
),
foregroundDecoration: foregroundDecoration,
constraints: BoxConstraints(minHeight: widget.minHeight ?? 0),
child: _selectionGestureDetectorBuilder.buildGestureDetector(
behavior: HitTestBehavior.translucent,
child: Align(
alignment: Alignment(-1.0, _textAlignVertical.y),
widthFactor: 1.0,
heightFactor: 1.0,
child: _addTextDependentAttachments(
paddedEditable,
textStyle,
placeholderStyle,
child: HoverButton(
actionsEnabled: false,
onPressed: enabled ? () {} : null,
builder: (context, states) {
return Container(
decoration: BoxDecoration(
borderRadius: radius,
border: Border.all(
style: _effectiveFocusNode.hasFocus
? BorderStyle.solid
: BorderStyle.none,
width: 1,
color: theme.brightness.isLight
? const Color.fromRGBO(0, 0, 0, 0.08)
: const Color.fromRGBO(255, 255, 255, 0.07),
),
color: _backgroundColor(states),
).copyWith(
backgroundBlendMode: widget.decoration?.backgroundBlendMode,
border: widget.decoration?.border,

/// This border radius can't be applied, otherwise the error "A borderRadius
/// can only be given for a uniform Border." will be thrown. Instead,
/// [radius] is already set to get the value from [widget.decoration?.borderRadius],
/// if any.
// borderRadius: widget.decoration?.borderRadius,
boxShadow: widget.decoration?.boxShadow,
color: widget.decoration?.color,
gradient: widget.decoration?.gradient,
image: widget.decoration?.image,
shape: widget.decoration?.shape,
),
),
),
constraints: BoxConstraints(minHeight: widget.minHeight ?? 0),
child: AnimatedContainer(
duration: theme.fasterAnimationDuration,
curve: theme.animationCurve,
foregroundDecoration: foregroundDecoration,
child: _selectionGestureDetectorBuilder.buildGestureDetector(
behavior: HitTestBehavior.translucent,
child: Align(
alignment: Alignment(-1.0, _textAlignVertical.y),
widthFactor: 1.0,
heightFactor: 1.0,
child: _addTextDependentAttachments(
paddedEditable,
textStyle,
placeholderStyle,
),
),
),
),
);
},
),
),
);
Expand Down Expand Up @@ -1050,4 +1058,30 @@ class _TextBoxState extends State<TextBox>
),
);
}

Color _backgroundColor(Set<ButtonStates> states) {
final brightness = FluentTheme.of(context).brightness;

if (brightness.isDark) {
if (!enabled) {
return const Color.fromRGBO(255, 255, 255, 0.04);
} else if (states.isPressing || states.isFocused) {
return const Color(0xFF1f1f1f);
} else if (states.isHovering) {
return const Color(0xFF323232);
} else {
return const Color(0xFF2d2d2d);
}
} else {
if (!enabled) {
return const Color.fromRGBO(249, 249, 249, 0.3);
} else if (states.isPressing || states.isFocused) {
return const Color(0xFFffffff);
} else if (states.isHovering) {
return const Color(0xFFfbfbfb);
} else {
return const Color(0xFFf6f6f6);
}
}
}
}
7 changes: 5 additions & 2 deletions lib/src/controls/utils/hover_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class HoverButton extends StatefulWidget {
this.onHorizontalDragEnd,
this.onFocusChange,
this.autofocus = false,
this.actionsEnabled = true,
}) : super(key: key);

/// {@template fluent_ui.controls.inputs.HoverButton.mouseCursor}
Expand Down Expand Up @@ -94,6 +95,8 @@ class HoverButton extends StatefulWidget {

final ValueChanged<bool>? onFocusChange;

final bool actionsEnabled;

@override
_HoverButtonState createState() => _HoverButtonState();
}
Expand Down Expand Up @@ -207,7 +210,7 @@ class _HoverButtonState extends State<HoverButton> {
focusNode: node,
autofocus: widget.autofocus,
enabled: enabled,
actions: _actionMap,
actions: widget.actionsEnabled ? _actionMap : {},
onFocusChange: widget.onFocusChange,
onShowFocusHighlight: (v) {
if (mounted) setState(() => _shouldShowFocus = v);
Expand All @@ -222,7 +225,7 @@ class _HoverButtonState extends State<HoverButton> {
label: widget.semanticLabel,
button: true,
enabled: enabled,
focusable: enabled,
focusable: enabled && node.canRequestFocus,
focused: node.hasFocus,
child: w,
),
Expand Down