How can I use marquee package conditionally in Futter? - flutter

In the image you see that some texts take more space than the width provided by their parent widget. I want to use marquee so that when the text is longer than the width, it scrolls. But, it should not always be scrollable, only when it is longer, not even when the text is fitted inside the parent widget. How can I use marquee only when the text is long?

bool _willTextOverflow({required String text, required TextStyle style}) {
final TextPainter textPainter = TextPainter(
text: TextSpan(text: text, style: style),
maxLines: 1,
textDirection: TextDirection.ltr,
)..layout(minWidth: 0, maxWidth: maxWidth);
return textPainter.didExceedMaxLines;
}
Read more about this here.
Get the entire code here.

Related

Flutter/Dart: RichText and TextField removes beginning spaces at display

I have a RichText Widget in Flutter where I want to print the attribute LUKETXT which is in a Modell.
The problem is, that LUKETXT is containing whitespaces at the beginning, like for example:
' *end of invoice* '
But when I want to display it, the whitespaces at the beginning where removed.
My function is:
RichText BuildText(List<OrderReceipt> OrderReceipt) {
int deflen = OrderReceipt.length;
return RichText(
text: TextSpan(
style: const TextStyle(color: Colors.black,
fontFamily: "PolarSys",fontSize: 10),
spellOut: false,
children: [
for (int i =0; i<deflen; i++)
TextSpan(
spellOut: false,
text: OrderReceipt[i].LUKETXT,
)
]
),
);
}
Just for information: PolarSys Font is a Monospace Font, that's the reason I gonna build it like that.
Does anyone know what is wrong? I already experimentet with spellOut true and false but there was no change.
Thanks..
The problem is likely with the leading whitespaces being removed by the TextSpan widget. In Flutter, the TextSpan widget is designed to remove leading whitespaces by default. To preserve the leading whitespaces, you can use a Text widget instead of TextSpan. You can wrap the Text widget inside a parent widget like a Padding or a SizedBox to add the necessary whitespaces before the text.

Colorize only a part of a TextForm text

I was wondering if there was a way to customize a TextFormField with more accuracy. I don't want to change the whole text color, but only part of it. For instance, below is the above mention TextFormField and I want to highlight "には" (ie what is between curly braces) by adding a red color. I would like to avoid creating multiple TextFormFields to do this because it will be a mess to assemble the text afterwards but I don't know if it is possible.
WARNING
I am not looking for the RichText widget since I want to customize a TextFormField Widget or any Widget with an editable text.
This Widget is used in a List so I would like not to use a "preview" widget with my input widget.
AND
I don't need a full RichTextEditor since the User should not be able to modify the color. Only parts between curly braces should automatically be colorised.
Looking forwards to see what kind of solutions you guys could come up with !
I've finally found a gist that match my request. For those who are searching an answer to my question, you seems to have to override EditableText (How to change color of particular text in a text field dynamically?). But, it's only a draft and it is not working correctly as for today. I'll try to follow this path and add my answer on this post.
EDIT:
The only thing you have to change to this answer is the following:
#override
TextSpan buildTextSpan() {
final String text = textEditingValue.text;
int textLength = text.length;
if (widget.annotations != null && textLength > 0) {
var items = getRanges();
var children = <TextSpan>[];
for (var item in items) {
if (item.range.end < textLength) {
children.add(
TextSpan(style: item.style, text: item.range.textInside(text)),
);
} else if (item.range.start <= textLength) {
children.add(
TextSpan(
style: item.style,
text: TextRange(start: item.range.start, end: text.length)
.textInside(text)),
);
}
}
return new TextSpan(style: widget.style, children: children);
}
return new TextSpan(style: widget.style, text: text);
}
}
Explanation:
You simply have to correct the buildTextSpan part. The error was raised when you delete a character because the Range could raise an exception when the range end was not meet.
This might not be exactly what you want, but may be this can help you get started in a way.
Use RichText widget.
var text = new RichText(
text: new TextSpan(
style: new TextStyle(
fontSize: 10.0,
),
children: <TextSpan>[
new TextSpan(text: 'Text1'),
new TextSpan(text: 'Text2', style: new TextStyle(),
],
),
);

How to get the width of the dynamic text widget in flutter?

The fonts of the text in my text widget are dynamic. The width and height of the font change according to different font types. How to get the width or height value of the text widget that varies according to the font.
you can use a GlobalKey into the Text Widget and then check its size somewhere else
GlobalKey myKey = GlobalKey();
...
Text('MyText, key: myKey)
Then use the currentContext of the GlobalKey to check its size (in a callback after it has been built)
print(myKey?.currentContext?.size);
But before doing that you need to build the Text Widget
The current context is null if there is no widget in the tree that
matches this global key
If you want to know the size it will use before creating the TextWidget you can try creating a TextSpan somewhere (in the initState for example)
TextSpan longestParagraphTest = TextSpan(
text: "This is all my Text",
style: myTextStyle, //define the style it willl use, fontSize, etc.
);
TextPainter _textPainter = TextPainter(text: longestParagraphTest, textDirection: TextDirection.ltr, maxLines: 1)..layout(minWidth: 0.0, maxWidth: double.infinity);
width = _textPainter.width; //This is the width it requires
height = _textPainter.height; //This is the height it requires

check if text Widget has style

I am trying to add a default style to a Text Widget, but I need to be able to overwrite it.
this is what I am trying right now.
var newTitle = title;
if (title is Text) {
Text titleText = title as Text;
newTitle = Text(titleText.data, style: TextStyle == null ? TextStyle() : TextStyle(fontWeight: FontWeight.bold));
}
So I want to check if it has style if not add the default style to it, otherwise use overwriten
Flutter provides a simple way to do this by providing a DefaultTextStyle widget, which can be used to specify a default texty style for the subtree. If a child Text widget already defines a style, the specific Text style will be used.
DefaultTextStyle(
child: title,
style: TextStyle(fontWeight: FontWeight.bold),
),

Flutter editable text on canvas

I am trying to add an editable textbox to the canvas in Flutter, that will bring up the keyboard once selected.
I am using a custom painter to print text to a canvas (shown in the code below). Is it possible to make this text editable, or add a text input element over the canvas at a particular offset?
import 'package:flutter/material.dart' as Material;
class CanvasPainter extends Material.ChangeNotifier implements Material.CustomPainter {
..
void paint(Canvas canvas, Size size) {
Material.TextSpan textSpan = new Material.TextSpan(style: new material.TextStyle(color: new Maaterial.Color.fromRGBO(r, g, b, 1.0), fontSize: font.fontSize.toDouble(), fontFamily: 'Roboto'),text: "Hello there");
Material.TextPainter tp = new Material.TextPainter( text: textSpan, textAlign: TextAlign.left, textDirection: TextDirection.ltr, textScaleFactor: ratio);
tp.layout();
tp.paint(canvas, new Offset(50.0,50.0));
}
}
Not the best solution, but I could solve similar issue with OverlayEntry.
Steps:
create an OverlayEntry, put a (hidden) TextField into it with a FocusNode and TextEditController.
after added the overlay entry, request focus on the focusNode, so the keyboard will open.
Add an onChanged to the TextField, and notify the painter somehow (e.g. valueNotifier) on text change.
In the TextPainer use the TextEditController.text value