Custom fontweigh otherthan given 9 constants by dart like (550) - Flutter - flutter

I want to use custom fontweight in my text style otherthan flutter given 100, 200 ----- 900 font weights, like Fontweight.w550 or (550 as int).
tried to achieve with this but it doesn't worked out
fontWeight: FontWeight.lerp(FontWeight.w500, FontWeight.w600, 0.5)
Your help is appreciated...

FontWeight is a const parameter which is : [w100, w200, w300, w400, w500, w600, w700, w800, w900]
FontWeight.w500 along with fontStyle:FontStyle.bold work in older version, in new version there is nothing which gives FontWeight 550.

Related

How to create a Text widget with exactly two lines of text in flutter?

I want to create a Text widget with exactly two lines of text even if text's length is too small or too large.
I can limit upper-bound of line count with maxLines like below:
Text(subtitle,
maxLines: 2,
style: Theme.of(context).textTheme.bodyText1?.copyWith(
color: Theme.of(context).colorScheme.onSurface,
fontWeight: FontWeight.w400,
)
)
I saw some answers where it is suggested to use a SizedBox to fix the height of content. I didn't feel right to hardcode this value.
I am looking for something like lines in Android's TextView. What else can I do to achieve this in Flutter?
Update:
From the help of comments & answers below, I am using following code now:
String withExtraNextLineCharacters(String text, int count) {
String nextLineCharacters = "";
for (int index = 0; index < (count - 1); index++) {
nextLineCharacters += "\n";
}
return text + nextLineCharacters;
}
Text(subtitle+'\n',
maxLines: 2,
style: Theme.of(context).textTheme.bodyText1?.copyWith(
color: Theme.of(context).colorScheme.onSurface,
fontWeight: FontWeight.w400,
)
)
the line break \n is a placeholder for 2 lines. The subtitle shift them down... and the maxLines condition is cutting them away :-)

Flutter how to guarante minimum left space

Let me provide an image:
I have a rectangle. I put here two elements, O1 and TEXT (with TextAlign.center).
For now I'm using stack, so in rectangle I have:
text in container, expanded on whole width with TextAlign.Center
Small rectangle on right (O1) on screen. (positioned widget with left: 0)
All works fine but, in some cases, If text is long, text overlap O1 - it's a problem.
How to assure a minimum left starting position for text which will work on long text (text which want to overlap O1)? i.e if text want to overlap O1, then move text to right. (O1 width = 16)
What I tried?
Add left margin / padding to container of text.
It works fine for long text which wants to overlap O1. But It does not work properly on smaller text. Because TextAlign.center applies after text to right. So this text does not looks center in the biggest rectangle, only in the smaller.
You can use the overflow in Text Widget.
new Text(
'Text largeeeeeeeeeeeeeeeeeeeeeee',
overflow: TextOverflow.ellipsis,
style: new TextStyle(
fontSize: 13.0,
fontFamily: 'Roboto',
color: new Color(0xFF212121),
fontWeight: FontWeight.bold,
),
),
Just try the methods here to see which one fits best for you. This should work if text trys to overflow over its container (into O1) it will not.

Integers of values 0 to show 2 digits or decimal places - flutter

Hi there I am having trouble displaying a variable with a value of zero ith more the one place except for in the terminal.
i have 2 simple variables that I initialize at the start of my app
int players = 00
double pot = 00.00
id like my variable to be displayed on the screen like that when the app fist starts but it always reverts player to 0 and pot to 0.0
I've had no luck with doing that with the text widget it will only show 1 zero
Text(
'Players : $players',
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25.0),
),
Text(
'Current Pot : $pot',
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25.0),
),
any ideas?
It isn't exactly clear what behavior is expected when the value of players and the value of pot changes. You are probably seeking something along the lines of players.toString().padLeft(2, '0') and pot.toStringAsFixed(2).padLeft(5, '0'). Behavior shown when these values change in DartPad.

Flutter: How can I resize text based on device's screen size

In flutter, how can I resize text in an app based on device screen size? The app is used for reading text, I need users to see bigger text as their device size increases and also set a maximum possible size. One way I know is doing this;
Text(
'Some text here',
style: TextStyle(
fontSize: 20 * MediaQuery.of(context).size.width * some_ratio
),
)
Is there a different way of doing this that will also take into consideration display height as well?
For font size based on screen, I would suggest using Height as reference.
for example:
double unitHeightValue = MediaQuery.of(context).size.height * 0.01;
double multiplier = 25;
return Text(
'Some Text',
style: TextStyle(
fontSize: multiplier * unitHeightValue,
),
);
By this approach, you will get Pixel perfect Text size with an additional advantage of a multiplier value of your choice.
This is similar to the dp or sp concept in Android.
Update:
After a few additional experiences, I have realised that choosing Height as references only works for mobile devices, especially in portrait mode/orientation.
In simple terms when device's main scrolling axis is vertical in device's portrait mode.
So, I would suggest selecting/changing the references as per the need of your application.
i.e. If you are working on an application which is definitely mobile app with only portrait orientation & no rotation support, the above code should work fine without any issues.
However, in case of flutter web support or just rotation support in mobile app, the above approach may not give desired results when user rotates the device.
In this scenario referring to the screen width makes sense for flutter web due to web being accessible to horizontal displays.
But in case of rotation support you may choose to stick with width as reference in all orientation or update the reference based on orientation.
This should be the best answer so far:
final textScale=MediaQuery.of(context).size.height * 0.01;
final screenHeight=MediaQuery.of(context).size.height;
double getHeight(double sysVar,double size){
double calc=size/1000;
return sysVar *calc;
}
double getTextSize(double sysVar,double size){
double calc=size/10;
return sysVar *calc;
}
Text('lovely family',style: TextStyle(fontSize: getTextSize(textScale,
20),),
SizedBox(height: getHeight(screenHeight, 30),),)
Not a direct answer, but the solution for me was different. Depends on what you would like to achieve. For me, the auto_size_text widget solved most of the problems, because row and other widgets define basic size.
AutoSizeText(
'Text will be resized',
style: TextStyle(fontSize: 20), //just info size
maxLines: 2, //if needed
)

How do set text line height in flutter?

So in Material Design Spec under Onboarding: here
It is specified that:
32sp line height
which is the height from the bottom of the headline text to the base line of the subhead text.
My question is how exactly can this be implemented in flutter. Are padding enough to mimic this spec? or are there other more accurate ways to do this?
Yes, there is also a height property in TextStyle which allows you to manually adjust the height of the line.
Code Snippet
Text('Hey There',
style: TextStyle(height: 5, fontSize: 10),
)
In addition to Ayush's answer. If we look to the documentation, we can see
When height is non-null, the line height of the span of text will be a multiple of fontSize and be exactly fontSize * height logical pixels tall.
For example, if want to have height 24.0, with font-size 20.0, we should have height property 1.2
Example:
TextStyle(
fontSize: 20.0,
height: 1.2,
);