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

Add some properties (cursorColor, onTextChange, focusNode, onTextSubmitted, ...) for TextField #83

Merged
merged 5 commits into from
Sep 18, 2024
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
87 changes: 63 additions & 24 deletions lib/src/presentation/language_tool_text_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ class LanguageToolTextField extends StatefulWidget {
/// Mistake popup window
final MistakePopup? mistakePopup;

/// Padding for the text field
final EdgeInsetsGeometry? padding;

/// The maximum number of lines to show at one time, wrapping if necessary.
final int? maxLines;

Expand All @@ -42,19 +39,47 @@ class LanguageToolTextField extends StatefulWidget {
/// Determine text Direction
final TextDirection? textDirection;

final ValueChanged<String>? onTextChange;
final ValueChanged<String>? onTextSubmitted;
final VoidCallback? onTap;
final TapRegionCallback? onTapOutside;
final TextInputAction? textInputAction;
final TextInputType? keyboardType;
final Color? cursorColor;
final bool autoFocus;
final FocusNode? focusNode;
final Brightness? keyboardAppearance;
final bool autocorrect;
final bool readOnly;
final MouseCursor? mouseCursor;
final bool alignCenter;

/// Creates a widget that checks grammar errors.
const LanguageToolTextField({
required this.controller,
this.style,
this.decoration = const InputDecoration(),
this.language = 'auto',
this.mistakePopup,
this.padding,
this.maxLines = 1,
this.minLines,
this.expands = false,
this.textAlign = TextAlign.start,
this.textDirection,
this.cursorColor,
this.autocorrect = true,
this.autoFocus = false,
this.readOnly = false,
this.textInputAction,
this.keyboardType,
this.focusNode,
this.keyboardAppearance,
this.mouseCursor,
this.onTap,
this.onTapOutside,
this.onTextChange,
this.onTextSubmitted,
this.alignCenter = true,
super.key,
});

Expand All @@ -63,16 +88,15 @@ class LanguageToolTextField extends StatefulWidget {
}

class _LanguageToolTextFieldState extends State<LanguageToolTextField> {
static const _padding = 24.0;

final _focusNode = FocusNode();
FocusNode? _focusNode;
final _scrollController = ScrollController();

@override
void initState() {
super.initState();
final controller = widget.controller;

_focusNode = widget.focusNode ?? FocusNode();
controller.focusNode = _focusNode;
controller.language = widget.language;
final defaultPopup = MistakePopup(popupRenderer: PopupOverlayRenderer());
Expand Down Expand Up @@ -101,23 +125,36 @@ class _LanguageToolTextFieldState extends State<LanguageToolTextField> {
suffix: fetchError != null ? httpErrorText : null,
);

return Padding(
padding: widget.padding ?? const EdgeInsets.all(_padding),
child: Center(
child: TextField(
textAlign: widget.textAlign,
textDirection: widget.textDirection,
focusNode: _focusNode,
controller: widget.controller,
scrollController: _scrollController,
decoration: inputDecoration,
minLines: widget.minLines,
maxLines: widget.maxLines,
expands: widget.expands,
style: widget.style,
),
),
Widget childWidget = TextField(
textAlign: widget.textAlign,
textDirection: widget.textDirection,
focusNode: _focusNode,
controller: widget.controller,
scrollController: _scrollController,
decoration: inputDecoration,
minLines: widget.minLines,
maxLines: widget.maxLines,
expands: widget.expands,
style: widget.style,
cursorColor: widget.cursorColor,
autocorrect: widget.autocorrect,
textInputAction: widget.textInputAction,
keyboardAppearance: widget.keyboardAppearance,
keyboardType: widget.keyboardType,
autofocus: widget.autoFocus,
readOnly: widget.readOnly,
mouseCursor: widget.mouseCursor,
onChanged: widget.onTextChange,
onSubmitted: widget.onTextSubmitted,
onTap: widget.onTap,
onTapOutside: widget.onTapOutside,
);

if (widget.alignCenter) {
childWidget = Center(child: childWidget);
}

return childWidget;
},
);
}
Expand All @@ -127,7 +164,9 @@ class _LanguageToolTextFieldState extends State<LanguageToolTextField> {

@override
void dispose() {
_focusNode.dispose();
if (widget.focusNode == null) {
_focusNode?.dispose();
}
_scrollController.dispose();
super.dispose();
}
Expand Down
225 changes: 114 additions & 111 deletions lib/src/utils/mistake_popup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:languagetool_textfield/src/domain/mistake.dart';
import 'package:languagetool_textfield/src/domain/typedefs.dart';
import 'package:languagetool_textfield/src/utils/extensions/string_extension.dart';
import 'package:languagetool_textfield/src/utils/popup_overlay_renderer.dart';
import 'package:pointer_interceptor/pointer_interceptor.dart';

/// Builder class that uses specified [popupRenderer] and [mistakeBuilder]
/// to create mistake popup
Expand Down Expand Up @@ -112,129 +113,131 @@ class LanguageToolMistakePopup extends StatelessWidget {

final availableSpace = _calculateAvailableSpace(context);

return ConstrainedBox(
constraints: BoxConstraints(
maxWidth: maxWidth,
maxHeight: availableSpace,
),
child: Container(
margin: EdgeInsets.symmetric(
horizontal: horizontalMargin,
vertical: verticalMargin,
),
decoration: BoxDecoration(
color: const Color.fromRGBO(241, 243, 248, 1.0),
borderRadius: BorderRadius.circular(_borderRadius),
boxShadow: const [BoxShadow(color: Colors.grey, blurRadius: 8)],
return PointerInterceptor(
child: ConstrainedBox(
constraints: BoxConstraints(
maxWidth: maxWidth,
maxHeight: availableSpace,
),
padding: const EdgeInsets.only(
top: 8,
bottom: 4,
left: 4,
right: 4,
),
child: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.only(left: 4),
child: Row(
children: [
Expanded(
child: Row(
children: [
Padding(
padding: const EdgeInsets.only(right: 5.0),
child: Image.asset(
LangToolImages.logo,
width: _iconSize,
height: _iconSize,
package: 'languagetool_textfield',
child: Container(
margin: EdgeInsets.symmetric(
horizontal: horizontalMargin,
vertical: verticalMargin,
),
decoration: BoxDecoration(
color: const Color.fromRGBO(241, 243, 248, 1.0),
borderRadius: BorderRadius.circular(_borderRadius),
boxShadow: const [BoxShadow(color: Colors.grey, blurRadius: 8)],
),
padding: const EdgeInsets.only(
top: 8,
bottom: 4,
left: 4,
right: 4,
),
child: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.only(left: 4),
child: Row(
children: [
Expanded(
child: Row(
children: [
Padding(
padding: const EdgeInsets.only(right: 5.0),
child: Image.asset(
LangToolImages.logo,
width: _iconSize,
height: _iconSize,
package: 'languagetool_textfield',
),
),
),
const Text('Correct'),
],
const Text('Correct'),
],
),
),
),
IconButton(
icon: const Icon(
Icons.close,
size: 12,
IconButton(
icon: const Icon(
Icons.close,
size: 12,
),
constraints: const BoxConstraints(),
padding: EdgeInsets.zero,
splashRadius: _dismissSplashRadius,
onPressed: () {
_dismissDialog();
controller.onClosePopup();
},
),
constraints: const BoxConstraints(),
padding: EdgeInsets.zero,
splashRadius: _dismissSplashRadius,
onPressed: () {
_dismissDialog();
controller.onClosePopup();
},
),
],
),
),
Container(
margin: const EdgeInsets.only(top: 8),
padding: const EdgeInsets.all(padding),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(_borderRadius),
],
),
),
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Padding(
padding: const EdgeInsets.only(
bottom: _paddingBetweenTitle,
),
child: Text(
mistake.type.name.capitalize(),
style: TextStyle(
color: Colors.grey.shade700,
fontSize: _mistakeNameFontSize,
fontWeight: FontWeight.w600,
letterSpacing: _titleLetterSpacing,
Container(
margin: const EdgeInsets.only(top: 8),
padding: const EdgeInsets.all(padding),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(_borderRadius),
),
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Padding(
padding: const EdgeInsets.only(
bottom: _paddingBetweenTitle,
),
child: Text(
mistake.type.name.capitalize(),
style: TextStyle(
color: Colors.grey.shade700,
fontSize: _mistakeNameFontSize,
fontWeight: FontWeight.w600,
letterSpacing: _titleLetterSpacing,
),
),
),
),
Padding(
padding: const EdgeInsets.only(bottom: padding),
child: Text(
mistake.message,
style: const TextStyle(
fontSize: _mistakeMessageFontSize,
Padding(
padding: const EdgeInsets.only(bottom: padding),
child: Text(
mistake.message,
style: const TextStyle(
fontSize: _mistakeMessageFontSize,
),
),
),
),
Wrap(
spacing: _replacementButtonsSpacing,
runSpacing: kIsWeb
? _replacementButtonsSpacing
: _replacementButtonsSpacingMobile,
children: mistake.replacements
.map(
(replacement) => ElevatedButton(
onPressed: () => _fixTheMistake(replacement),
style: mistakeStyle ??
ElevatedButton.styleFrom(
elevation: 0,
minimumSize: const Size(40, 36),
padding: const EdgeInsets.symmetric(
horizontal: 8,
Wrap(
spacing: _replacementButtonsSpacing,
runSpacing: kIsWeb
? _replacementButtonsSpacing
: _replacementButtonsSpacingMobile,
children: mistake.replacements
.map(
(replacement) => ElevatedButton(
onPressed: () => _fixTheMistake(replacement),
style: mistakeStyle ??
ElevatedButton.styleFrom(
elevation: 0,
minimumSize: const Size(40, 36),
padding: const EdgeInsets.symmetric(
horizontal: 8,
),
),
),
child: Text(replacement),
),
)
.toList(),
),
],
child: Text(replacement),
),
)
.toList(),
),
],
),
),
),
),
],
],
),
),
),
),
Expand Down
Loading
Loading