diff --git a/CHANGELOG.md b/CHANGELOG.md index a757e71..41ebad0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,12 @@ +## [0.0.2] 26.04.2021 + +* Fixed wrong cursor position when last word has been replaced. + +## [0.0.1+1] 23.04.2021 + +* Improved documentation + ## [0.0.1] 23.04.2021 * Initial Release + diff --git a/example/lib/main.dart b/example/lib/main.dart index 8823c4c..bde1297 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -37,7 +37,6 @@ class _MyHomePageState extends State { @override Widget build(BuildContext context) { - var height = MediaQuery.of(context).size.height * 0.4; return Scaffold( appBar: AppBar( title: Text(widget.title), diff --git a/example/pubspec.lock b/example/pubspec.lock index 0cf7373..d41698f 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -68,7 +68,7 @@ packages: path: ".." relative: true source: path - version: "0.0.1" + version: "0.0.1+1" flutter_test: dependency: "direct dev" description: flutter diff --git a/lib/controller/social_text_editing_controller.dart b/lib/controller/social_text_editing_controller.dart index c678850..4a7dcd4 100644 --- a/lib/controller/social_text_editing_controller.dart +++ b/lib/controller/social_text_editing_controller.dart @@ -52,8 +52,8 @@ class SocialTextEditingController extends TextEditingController{ void replaceRange(String newValue, TextRange range){ print("text.length = ${text.length}"); - print("range.end: ${range.end}"); - var willAddSpaceAtEnd = (text.length-1) <= range.end; + print("range.end: ${range.start + newValue.length}"); + var willAddSpaceAtEnd = (text.length-1) <= (range.start + newValue.length); var replacingText = "$newValue${willAddSpaceAtEnd ? " " : ""}"; var replacedText = text.replaceRange(range.start, range.end+1, replacingText); var newCursorPosition = range.start+replacingText.length + (willAddSpaceAtEnd ? 0 : 1); @@ -61,6 +61,10 @@ class SocialTextEditingController extends TextEditingController{ // if(newCursorPosition == replacedText.length){ // newCursorPosition -= 1; // } + print("Length: ${replacedText.length}, ${newCursorPosition}"); + if(newCursorPosition >= replacedText.length){ + newCursorPosition = replacedText.length-1; + } value = value.copyWith(text: replacedText,selection: value.selection.copyWith(baseOffset: newCursorPosition,extentOffset: newCursorPosition),composing: value.composing); } diff --git a/lib/flutter_social_textfield.dart b/lib/flutter_social_textfield.dart index b2e6df8..1b1b0e0 100644 --- a/lib/flutter_social_textfield.dart +++ b/lib/flutter_social_textfield.dart @@ -1,6 +1,6 @@ library flutter_social_textfield; -export 'util/regular_expressions.dart'; export 'controller/social_text_editing_controller.dart'; -export 'widget/social_text_field_controller.dart'; -export 'util/social_text_span_builder.dart'; \ No newline at end of file +export 'util/regular_expressions.dart'; +export 'util/social_text_span_builder.dart'; +export 'widget/social_text_field_controller.dart'; \ No newline at end of file diff --git a/lib/util/social_text_span_builder.dart b/lib/util/social_text_span_builder.dart index ca64067..f27065f 100644 --- a/lib/util/social_text_span_builder.dart +++ b/lib/util/social_text_span_builder.dart @@ -1,6 +1,10 @@ import 'package:flutter/material.dart'; import 'package:flutter_social_textfield/flutter_social_textfield.dart'; +///Builds [TextSpan] with the provided regular expression, stles and text. +/// [defaultTextStyle] Optional default textstyle. used for detection types that has not been initialied +/// [detectionTextStyles] required, used for setting up text styles for types found in [DetectedType] enum +/// [regularExpressions] required, used for detecting [DetectedType] content. default regular expressions can be found in the plugin class SocialTextSpanBuilder{ final TextStyle? defaultTextStyle; @@ -12,6 +16,8 @@ class SocialTextSpanBuilder{ SocialTextSpanBuilder(this.regularExpressions,this.defaultTextStyle,{this.detectionTextStyles = const {}}); + ///Gets Text Style for [start,end] range. + ///return default text style if noting found. TextStyle getTextStyleForRange(int start, int end){ TextStyle? textStyle; allMatches.keys.forEach((type) { @@ -24,6 +30,7 @@ class SocialTextSpanBuilder{ return textStyle ?? defaultTextStyle ?? TextStyle(); } + ///returns TextSpan conaining all formatted content. TextSpan build(String text){ regularExpressions.keys.forEach((type) { allMatches[type] = regularExpressions[type]!.allMatches(text).toList(); @@ -40,7 +47,6 @@ class SocialTextSpanBuilder{ int cursorPosition = 0; for(int i = 0;i=2.12.0 <3.0.0"