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

Expose focusNode, fallback if missing + fix English typos. #15

Merged
merged 2 commits into from Nov 17, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/lib/main.dart
Expand Up @@ -49,7 +49,7 @@ class _MyHomePageState extends State<MyHomePage> {
children: <Widget>[
TagEditor(
length: values.length,
delimeters: [',', ' '],
delimiters: [',', ' '],
hasAddButton: true,
resetTextOnSubmitted: true,
textStyle: TextStyle(color: Colors.grey),
Expand Down
64 changes: 36 additions & 28 deletions lib/tag_editor.dart
Expand Up @@ -3,20 +3,21 @@ import 'package:flutter/material.dart';
import './tag_editor_layout_delegate.dart';
import './tag_layout.dart';

/// A Wiget for editing tag similar to Google's Gmail
/// email address input widget in iOS app
/// A [Widget] for editing tag similar to Google's Gmail
/// email address input widget in the iOS app.
/// TODO: Support remove while typing
class TagEditor extends StatefulWidget {
const TagEditor({
Key key,
@required this.length,
@required this.tagBuilder,
this.hasAddButton = true,
@required this.onTagChanged,
this.delimeters = const [],
Key key,
this.focusNode,
this.hasAddButton = true,
this.delimiters = const [],
this.icon,
this.enabled = true,
// TextField's Props
// TextField's properties
this.textStyle,
this.inputDecoration = const InputDecoration(),
this.keyboardType,
Expand All @@ -32,42 +33,49 @@ class TagEditor extends StatefulWidget {
this.resetTextOnSubmitted = false,
this.onSubmitted,
this.keyboardAppearance,
}) : super(key: key);
}) : assert(length != null),
assert(tagBuilder != null),
assert(onTagChanged != null),
super(key: key);

/// The number of tags currently shown
/// The number of tags currently shown.
final int length;

/// Builder for building the tags, this usually use Flutter's Material `Chip`
/// Builder for building the tags, this usually use Flutter's Material `Chip`.
final Widget Function(BuildContext, int) tagBuilder;

/// Show the add button to the right
/// Show the add button to the right.
final bool hasAddButton;

/// The icon for the add button enabled with `hasAddButton`
/// The icon for the add button enabled with `hasAddButton`.
final IconData icon;

/// Callback for when the tag changed. Use this to get the new tag and add
/// it to the state
/// it to the state.
final ValueChanged<String> onTagChanged;

/// When the string value in this `delimeters` is founded, a new tag will be
/// created and `onTagChanged` called
final List<String> delimeters;
/// When the string value in this `delimiters` is found, a new tag will be
/// created and `onTagChanged` is called.
final List<String> delimiters;

/// Reset the TextField when `onSubmitted` is called
/// this is default to `false` because when the form is submitted
/// usually the outstanding value is just used, but this option is here
/// in case you want to reset it for any reasons (like convering the
/// outstanding value to tag)
/// in case you want to reset it for any reasons (like converting the
/// outstanding value to tag).
final bool resetTextOnSubmitted;

/// Called when the user are done editing the text in the [TextField]
/// Use this to get the outstanding text that aren't converted to tag yet
/// If no text is entered when this is called an empty string will be passed
/// If no text is entered when this is called an empty string will be passed.
final ValueChanged<String> onSubmitted;

/// [TextField]'s Props
/// Please refer to [TextField] documentation
/// Focus node for checking if the [TextField] is focused.
final FocusNode focusNode;

/// [TextField]'s properties.
///
/// Please refer to [TextField] documentation.
final bool enabled;
final TextStyle textStyle;
final InputDecoration inputDecoration;
Expand All @@ -88,22 +96,22 @@ class TagEditor extends StatefulWidget {
}

class _TagsEditorState extends State<TagEditor> {
/// A controller to keep value of the [TextField]
/// A controller to keep value of the [TextField].
final _textFieldController = TextEditingController();

/// A state variable for checking if new text is enter
/// A state variable for checking if new text is enter.
var _previousText = '';

/// A state for checking if the [TextFiled] has focus
/// A state for checking if the [TextFiled] has focus.
var _isFocused = false;

/// Focus node for checking if the [TextField] is focused
final _focusNode = FocusNode();
/// Focus node for checking if the [TextField] is focused.
FocusNode _focusNode;

@override
void initState() {
super.initState();

_focusNode = widget.focusNode ?? FocusNode();
_focusNode.addListener(_onFocusChanged);
}

Expand All @@ -124,14 +132,14 @@ class _TagsEditorState extends State<TagEditor> {
// This function looks ugly fix this
final previousText = _previousText;
_previousText = string;
if (string.isEmpty || widget.delimeters.isEmpty) {
if (string.isEmpty || widget.delimiters.isEmpty) {
return;
}

if (string.length > previousText.length) {
// Add case
final newChar = string[string.length - 1];
if (widget.delimeters.contains(newChar)) {
if (widget.delimiters.contains(newChar)) {
final targetString = string.substring(0, string.length - 1);
if (targetString.isNotEmpty) {
_onTagChanged(targetString);
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
@@ -1,6 +1,6 @@
name: material_tag_editor
description: A simple tag editor for inputing tags. This is design to act and feel similar the standard Material TextField as much as possible.
version: 0.0.5
version: 0.0.6
homepage: https://github.com/panuavakul/material_tag_editor.git

environment:
Expand Down