Skip to content

Commit

Permalink
fixed wrong cursor position (issue #1)
Browse files Browse the repository at this point in the history
  • Loading branch information
dreampowder committed Apr 26, 2021
1 parent 5691512 commit 1d456fa
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 13 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

1 change: 0 additions & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class _MyHomePageState extends State<MyHomePage> {

@override
Widget build(BuildContext context) {
var height = MediaQuery.of(context).size.height * 0.4;
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions lib/controller/social_text_editing_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,19 @@ 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);
print("$willAddSpaceAtEnd new Position: $newCursorPosition, new Length: ${replacedText.length}");
// 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);
}

Expand Down
6 changes: 3 additions & 3 deletions lib/flutter_social_textfield.dart
Original file line number Diff line number Diff line change
@@ -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';
export 'util/regular_expressions.dart';
export 'util/social_text_span_builder.dart';
export 'widget/social_text_field_controller.dart';
11 changes: 10 additions & 1 deletion lib/util/social_text_span_builder.dart
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand All @@ -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();
Expand All @@ -40,7 +47,6 @@ class SocialTextSpanBuilder{
int cursorPosition = 0;
for(int i = 0;i<orderedMatches.length;i++){
var match = orderedMatches[i];
print("match:${match.start},${match.end}= ${text.substring(match.start, match.end)}");
var subString = text.substring(match.start, match.end);
bool willAddSpaceAtStart = subString.startsWith(" "); //Strangely, mention and hashtags start with an empty space, while web detections are correct
root = getTextSpan(root, text.substring(cursorPosition,match.start + (willAddSpaceAtStart ? 1 : 0)), getTextStyleForRange(cursorPosition, match.start));
Expand All @@ -53,6 +59,9 @@ class SocialTextSpanBuilder{
return root;
}

///Wraps text with style inside the root.
///[root] optional, return TextSpan(text, style:style) if null
///[style] TextStyle
TextSpan getTextSpan(TextSpan? root, String text, TextStyle style){
if(root == null){
return TextSpan(text: text,style: style);
Expand Down
7 changes: 6 additions & 1 deletion lib/widget/social_text_field_controller.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'dart:async';
import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -57,6 +56,7 @@ class _DefaultSocialTextFieldControllerState extends State<DefaultSocialTextFiel
print("HeightMap: $heightMap");
}

///Shows the widget that hes been set with the [widget.detectionBuilders]. return empty Container if noting found
void onDetectContent(SocialContentDetection detection){
if(detection.type != _detectedType){
setState(() {
Expand All @@ -75,17 +75,20 @@ class _DefaultSocialTextFieldControllerState extends State<DefaultSocialTextFiel
}
}

///Used for calculating size for text. scrolls the main content if it is positioned under the detected content widget.
Size getTextRectSize({required width,required String text,required TextStyle style}) {
final TextPainter textPainter = TextPainter(
text: TextSpan(text: text, style: style), textDirection: TextDirection.ltr)
..layout(minWidth: 0, maxWidth: width);
return textPainter.size;
}

///return true if content type set
bool doesHaveBuilderForCurrentType(){
return (widget.detectionBuilders?.containsKey(_detectedType) ?? false);
}

///return bottom value of main content
double getChildBottomPosition(){
if(!doesHaveBuilderForCurrentType() || (!widget.willResizeChild)){
return 0;
Expand All @@ -94,6 +97,7 @@ class _DefaultSocialTextFieldControllerState extends State<DefaultSocialTextFiel
return heightMap[_detectedType] ?? 0;
}

///return height for detected content, zero if not set.
double getBuilderContentHeight(){
if(!doesHaveBuilderForCurrentType() || (!widget.willResizeChild)){
return 0;
Expand All @@ -102,6 +106,7 @@ class _DefaultSocialTextFieldControllerState extends State<DefaultSocialTextFiel
return heightMap[_detectedType] ?? 0;
}

///returns detected content
PreferredSize getDetectionContent(){
if(!(widget.detectionBuilders?.containsKey(_detectedType) ?? false)){
return PreferredSize(child: Container(), preferredSize: Size.zero);
Expand Down
7 changes: 3 additions & 4 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
name: flutter_social_textfield
description: Yet another textfield with hashtag, url and @mention detection. also with additional actions.
version: 0.0.1
author: Serdar Coskun
homepage: https://www.github.com/dreampowder
description: TextEditingController with hashtag, url and mention detection and ability to show selection widgets according to detected type.
version: 0.0.2
homepage: https://github.com/dreampowder/flutter_social_textfield

environment:
sdk: ">=2.12.0 <3.0.0"
Expand Down

0 comments on commit 1d456fa

Please sign in to comment.