Boolean controlled textstyle in Flutter? - 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,);

Related

Flutter define icon color by textstyle

I think I've hared that there is a Widget the gets a Textstyle and a list of children. The children can be Text-Widgets or Icon-Widgets and get the defining styles (and with that the color of the Icon) from that parent.
But I can't find that anywhere.
Do I remember that correctly, and if, what Widget was that?
Or am I just wrong about that?
Edit:
I thought it was this, but it don't seam to work the way I thought:
return RichText(
strutStyle: StrutStyle.fromTextStyle(TextStyle(color: Colors.white, fontWeight: FontWeight.w500)),
text: TextSpan(
children: [
TextSpan(text: "Add Object"),
WidgetSpan(child: Icon(Icons.add))
]
),
);
You can make use of the DefaultTextStyle widget which receives a TextStyle entity and will apply it to descendant Text widgets (as long as the descendant Text widgets do not have explicit styles applied: https://api.flutter.dev/flutter/widgets/DefaultTextStyle-class.html
When talking about the icons as well, there is no dedicated widget for that since you would usually define that as part of your overall theme as ThemeData (usually provided in MaterialApp)

Is it okay to extend the Flutter built-in TextStyle class with my own custom methods?

Is there a problem if I extend some of the framework's built-in classes, specifically TextStyle, with my own extension methods? docs here
I'll layout an example of what I mean so that it resembles the actual implementation I'm using in my project - but not fully, it's just to be as clear and clean as possible:
In this example I'm declaring some common-use text styles (for context, in my project I have many many more), and later I'm extending the built-in flutter class TextStyle to have a faster and smoother implementation of further customization:
// some text styles I'll use across the whole app
const TextStyle titleLarge600 = TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.w600,
);
const TextStyle titleSmall500 = TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.w500,
);
const TextStyle bodyRegular400 = TextStyle(
fontSize: 14.0,
fontWeight: FontWeight.w400,
);
// ...
//* Here it comes
extension CopyWithStyles on TextStyle {
/// Copy with [MyColors.primaryFontColor] color.
TextStyle get primaryColor => copyWith(color: MyColors.primaryFontColor);
/// Copy with [MyColors.accentColor] color.
TextStyle get accentColor => copyWith(color: MyColors.accentColor);
// ... several colors later
/// Copy with [TextDecoration.underline] decoration.
TextStyle get underline => copyWith(decoration: TextDecoration.underline);
/// Copy with [TextStyle.letterSpacing] of 0.4.
TextStyle get withLetterSpacing => copyWith(letterSpacing: 0.4);
// etc ...
}
This allows me to later just import these styles and methods and implement them in my Text widgets:
// ...
Text('Some title', style: titleSmall500.primaryColor),
... and even concatenate them:
// ...
Text(
'Some action here',
style: bodyRegular400.accentColor.underline.withLetterSpacing, // ← THIS WORKS! it has no limits really
),
I'm amazed as to how flexible extension methods are, but in this particular case (extending TextStyle) I'm wondering about performance issues, and compatibility with the framework, or if maybe I'm missing something and actually should not do this.
For the moment I'm not experiencing any issues. Still, I'm wondering if there could be any potential problems. Is there a problem if I implement something like this in my projects? Is there something I'm not considering and should take into account in the future?
TY

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

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?

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