check if text Widget has style - flutter

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),
),

Related

Boolean controlled textstyle in Flutter?

I have a boolean that controls the design of a container, its color, decoration, etc. based on if it is True or not. Is there a possible way for it to control the style of any text that is put into it?
You can use two kind of style in extra file and switch:
style: isBoolean ? firstTextstyle : secondTextstyle,
In another file create two textstyle and use above:
final firstTextstyle = Textstyle();
You can use theme here like this question and answer
You can use it by creating a TextStyle object according to the situation.
bool b =true;
TextStyle t1 = TextStyle(fontSize: 10);//true TextStyle
TextStyle t2 = TextStyle(fontSize: 20);//false TextStyle
Text('text', style: b ? t1 : t2,);

Flutter: How to programmatically change the font weight

I am new to flutter and trying to build a text field with basic options just as in word (underlineing, changing fontsize and weight,...)
I was able to write a code for changing the font size and I tried the same for font weight, but i wasn´t able to make it work.
As I am also new to Stackoverflow I am having troubles with inserting my working code properly...
So my question is, if you could share your version of changing the fontweight of a text in a textfield via buttons just like in word.
Thank you!
Firstly, use stateful widget, then make variable:
bool isBold = false;
Then use a Text widget like this:
Text(‘hello’, style: TextStyle(fontWeight: isBold ? FontWeight.bold : FontWeight.normal))
Next you need a button for user to toggle fontweight option:
MaterialButton(onPressed: () => setState(() => isBold = !isBold))
You can change fontweight of the textfield by using style property of Textfield
TextField(
....
style: fontweight=Fontweight.bold),
...

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 change Font Family using setState in Flutter?

I want to change my Text Widget font family on button click.
Which means I created a Container and in this Container I added multiple text style widget with GestureDetector widget. So whenever I click on Particular font family, I want to change my Text widget Font-Family.
Any suggestion for doing this type of functionality.
Hope this works or modify accordingly:
code inside your Text Widget:
Text("Your Text",
style: tapped
?TextStyle(fontFamily: 'Your-Font-Family')
:TextStyle(fontFamily: 'Your-Second-Font-Family'),
),
code inside Gesture Detector:
GestureDetector(
child: your child widget,
onTap: () {
setState(() {
tapped = !tapped;
});
}),
and remember to define bool tapped; in your widget class.
Still, if you face any difficulty, add the code to comment so I can help you out.
Use a variable to store font name
reference it in your text Widget
then call setState to update the value of variable and UI
String fontName;
//your TextWidget
Text("some text for testing",
style: TextStyle( fontFamily: fontName )
)
//function to update the value
_updateFont(String name){
setState((){
fontName = name;
});
}
make sure you are importing the fonts to be used
add them in pubspec.yaml file under fonts
- family: font1
fonts:
- asset: assets/fonts/font1.ttf

How to animate TextSpans within Flutter TextFormField

I am currently working on a project that requires minor animations to text as it appears in a TextFormField.
I hav extended the TextEditingControler and overriden the buildTextSpan function to enable different TextSyles based on the types of words that were being typed in. For example if a word is ['the','a','me'].
For example I would add a TextSpan with a particular style for each matching word and for all other words I would just add the word in a TextSpan with normal style.
This is working fine, however, what I want to do is to animate the matched words for a period as they are entered, for example they would turn from blue to green.
Extended TextEditingController Class
class RichTextEditingController extends TextEditingController {
TextStyle matchedStyle = TextStyle(
color: Colors.green,
fontSize: 20,
fontWeight: FontWeight.w700,
);
#override
TextSpan buildTextSpan({TextStyle style, bool withComposing}) {
List<String> wordsToMatchList = ['the','a','me'];
List<String> splitText = text.split(' ');
List<TextSpan> textSpanList = [];
splitText.forEach((word) {
if (wordsToMatchList.contains(word)) {
textSpanList.add(TextSpan(style: boldStyle, text: '$word'));
} else {
textSpanList.add(TextSpan(style: style, text: '$word'));
}
});
return TextSpan(children: textSpanList);
}
}
I have tried various techniques like future.Delayed and others, but can't seem to find any approach that works and need some help.
Question: Is there a way to add a minor animation to TextSpans or other widgets that would show when new text is typed into a text field that matches a certain pattern?