Skip to content

Commit

Permalink
support textCapitalization customization (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
henryleunghk authored Dec 2, 2021
1 parent 93ef75a commit 2e756f0
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 37 deletions.
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,21 @@ Hope you find it useful and happy coding! 🎉🎉🎉
| Name | Type | Description | Default |
|:----------------|:--------------|:-------------------------------|:-------------------------|
| `controller` | TextEditingController | Controlling the text being edited (https://api.flutter.dev/flutter/material/TextField/controller.html) | null |
| `cursorColor` | Color | The color of the cursor (https://api.flutter.dev/flutter/material/TextField/cursorColor.html) | null | `decoration` | BoxDecoration | Controls the BoxDecoration of the box behind the text input. (https://api.flutter.dev/flutter/cupertino/CupertinoTextField/decoration.html) | null |
| `style` | TextStyle | The style to use for the text being edited [Only `fontSize`, `fontWeight`, `color` are supported] (https://api.flutter.dev/flutter/material/TextField/style.html) | null |
| `placeholderStyle`| TextStyle | The style to use for the placeholder text. [Only `fontSize`, `fontWeight`, `color` are supported] (https://api.flutter.dev/flutter/cupertino/CupertinoTextField/placeholderStyle.html) | null |
| `placeholder` | String | Placeholder text when text entry is empty (https://api.flutter.dev/flutter/cupertino/CupertinoTextField/placeholder.html) | null |
| `textContentType` | TextContentType | To identify the semantic meaning expected for a text-entry area (https://developer.apple.com/documentation/uikit/uitextcontenttype) | null |
| `cursorColor` | Color | The color of the cursor (https://api.flutter.dev/flutter/material/TextField/cursorColor.html) | null |
| `decoration` | BoxDecoration | Controls the BoxDecoration of the box behind the text input. (https://api.flutter.dev/flutter/cupertino/CupertinoTextField/decoration.html) | null |
| `focusNode` | FocusNode | Defines the keyboard focus for this widget (https://api.flutter.dev/flutter/material/TextField/focusNode.html) | null |
| `keyboardAppearance` | Brightness | The appearance of the keyboard (https://api.flutter.dev/flutter/material/TextField/keyboardAppearance.html) | null |
| `keyboardType` | KeyboardType | Type of keyboard to display for a given text-based view (https://developer.apple.com/documentation/uikit/uikeyboardtype) | KeyboardType.defaultType |
| `onChanged` | ValueChanged\<String> | Called when the user initiates a change to text entry (https://api.flutter.dev/flutter/material/TextField/onChanged.html) | null |
| `onSubmitted` | ValueChanged\<String> | Called when the user indicates that they are done editing the text in the field (https://api.flutter.dev/flutter/material/TextField/onSubmitted.html) | null |
| `focusNode` | FocusNode | Defines the keyboard focus for this widget (https://api.flutter.dev/flutter/material/TextField/focusNode.html) | null |
| `textAlign` | TextAlign | How the text should be aligned horizontally (https://api.flutter.dev/flutter/material/TextField/textAlign.html) | TextAlign.start |
| `maxLines` | int | The maximum number of lines to show at one time, wrapping if necessary (https://api.flutter.dev/flutter/material/TextField/maxLines.html) | 1 |
| `minLines` | int | Minimum number of lines of text input widget | 1 |
| `placeholder` | String | Placeholder text when text entry is empty (https://api.flutter.dev/flutter/cupertino/CupertinoTextField/placeholder.html) | null |
| `placeholderStyle`| TextStyle | The style to use for the placeholder text. [Only `fontSize`, `fontWeight`, `color` are supported] (https://api.flutter.dev/flutter/cupertino/CupertinoTextField/placeholderStyle.html) | null |
| `style` | TextStyle | The style to use for the text being edited [Only `fontSize`, `fontWeight`, `color` are supported] (https://api.flutter.dev/flutter/material/TextField/style.html) | null |
| `textAlign` | TextAlign | How the text should be aligned horizontally (https://api.flutter.dev/flutter/material/TextField/textAlign.html) | TextAlign.start |
| `textCapitalization` | TextCapitalization | Configures how the platform keyboard will select an uppercase or lowercase keyboard (https://api.flutter.dev/flutter/material/TextField/textCapitalization.html) | TextCapitalization.none |
| `textContentType` | TextContentType | To identify the semantic meaning expected for a text-entry area (https://developer.apple.com/documentation/uikit/uitextcontenttype) | null |
| `onChanged` | ValueChanged\<String> | Called when the user initiates a change to text entry (https://api.flutter.dev/flutter/material/TextField/onChanged.html) | null |
| `onSubmitted` | ValueChanged\<String> | Called when the user indicates that they are done editing the text in the field (https://api.flutter.dev/flutter/material/TextField/onSubmitted.html) | null |

## More examples

Expand Down
2 changes: 2 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class HomePage extends StatelessWidget {
fontSize: 16,
color: Colors.black54,
),
textCapitalization: TextCapitalization.sentences,
placeholderStyle: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
Expand All @@ -85,6 +86,7 @@ class HomePage extends StatelessWidget {
fontSize: 16,
color: Colors.black54,
),
textCapitalization: TextCapitalization.sentences,
placeholderStyle: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
Expand Down
21 changes: 20 additions & 1 deletion ios/Classes/NativeTextInput.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ - (instancetype)initWithFrame:(CGRect)frame
_textView.keyboardAppearance = [self keyboardAppearanceFromString:args[@"keyboardAppearance"]];
_textView.keyboardType = [self keyboardTypeFromString:args[@"keyboardType"]];
_textView.textAlignment = [self textAlignmentFromString:args[@"textAlign"]];
_textView.autocapitalizationType = [self textAutocapitalizationTypeFromString:args[@"textCapitalization"]];
_textView.textContainer.lineBreakMode = NSLineBreakByCharWrapping;

if ([args[@"maxLines"] intValue] == 1) {
Expand Down Expand Up @@ -161,7 +162,25 @@ - (UIKeyboardType)keyboardTypeFromString:(NSString*)keyboardType {
return UIKeyboardTypeDefault;
}

- (UITextContentType)textContentTypeFromString:(NSString*)contentType {
- (UITextAutocapitalizationType)textAutocapitalizationTypeFromString:(NSString *)textCapitalization {
if (!textCapitalization || [textCapitalization isKindOfClass:[NSNull class]]) {
return UITextAutocapitalizationTypeNone;
}

if ([textCapitalization isEqualToString:@"TextCapitalization.none"]) {
return UITextAutocapitalizationTypeNone;
} else if ([textCapitalization isEqualToString:@"TextCapitalization.characters"]) {
return UITextAutocapitalizationTypeAllCharacters;
} else if ([textCapitalization isEqualToString:@"TextCapitalization.sentences"]) {
return UITextAutocapitalizationTypeSentences;
} else if ([textCapitalization isEqualToString:@"TextCapitalization.words"]) {
return UITextAutocapitalizationTypeWords;
}

return UITextAutocapitalizationTypeNone;
}

- (UITextContentType)textContentTypeFromString:(NSString *)contentType {
if (!contentType || [contentType isKindOfClass:[NSNull class]]) {
return nil;
}
Expand Down
51 changes: 24 additions & 27 deletions lib/flutter_native_text_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,19 @@ class NativeTextInput extends StatefulWidget {
this.controller,
this.cursorColor,
this.decoration,
this.style,
this.placeholderStyle,
this.placeholder,
this.textContentType,
this.focusNode,
this.keyboardAppearance,
this.keyboardType = KeyboardType.defaultType,
this.onChanged,
this.onSubmitted,
this.focusNode,
this.textAlign = TextAlign.start,
this.maxLines = 1,
this.minLines = 1,
this.placeholder,
this.placeholderStyle,
this.style,
this.textAlign = TextAlign.start,
this.textCapitalization = TextCapitalization.none,
this.textContentType,
this.onChanged,
this.onSubmitted,
}) : super(key: key);

/// Controls the text being edited.
Expand All @@ -98,36 +99,31 @@ class NativeTextInput extends StatefulWidget {

final BoxDecoration? decoration;

final TextStyle? style;

final TextStyle? placeholderStyle;

/// A lighter colored placeholder hint that appears on the first line of the
/// text field when the text entry is empty.
///
/// Defaults to having no placeholder text.
///
/// The text style of the placeholder text matches that of the text field's
/// main text entry except a lighter font weight and a grey font color.
final String? placeholder;

final TextContentType? textContentType;
final FocusNode? focusNode;

final Brightness? keyboardAppearance;

final KeyboardType keyboardType;

final ValueChanged<String>? onChanged;
final int maxLines;

final ValueChanged<String?>? onSubmitted;
final int minLines;

final FocusNode? focusNode;
final String? placeholder;

final TextStyle? placeholderStyle;

final TextStyle? style;

final TextAlign textAlign;

final int maxLines;
final TextCapitalization textCapitalization;

final int minLines;
final TextContentType? textContentType;

final ValueChanged<String>? onChanged;

final ValueChanged<String?>? onSubmitted;

@override
State<StatefulWidget> createState() => _NativeTextInputState();
Expand Down Expand Up @@ -211,6 +207,7 @@ class _NativeTextInputState extends State<NativeTextInput> {
"width": constraints.maxWidth,
"text": _effectiveController.text,
"placeholder": widget.placeholder ?? "",
"textCapitalization": widget.textCapitalization.toString(),
"textContentType": widget.textContentType?.toString(),
"keyboardAppearance": widget.keyboardAppearance.toString(),
"keyboardType": widget.keyboardType.toString(),
Expand Down

0 comments on commit 2e756f0

Please sign in to comment.