How to make a multi line text in Flutter - flutter

Sometimes, the text widget does a line break, and sometimes don't. I tried to add overflow and maxline but it doesn't work.
This is the code:
Text(
ArticleData().articleBank[index].articleTitle,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20.0,
color: Colors.black,
),
),
This is what, I will try to build:

You can use FittedBox class.
Scales and positions its child within itself according to fit. The fit and alignment arguments must not be null.
FittedBox(
Key key,
fit: BoxFit.none,
alignment: Alignment.center,
child: Text(
ArticleData().articleBank[index].articleTitle,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20.0,
color: Colors.black,
),
),
)

Related

Text is shifted vertically in Flutter

I have the following code:
AutoSizeText.rich(
TextSpan(children: [
const TextSpan(
text: "\$",
style: TextStyle(
color: Colors.white,
fontSize: 45,
height: 1.0,
letterSpacing: .0,
fontWeight: FontWeight.w600,
),
),
TextSpan(
text: widget.budget.truncate().toString(),
style: const TextStyle(
color: Colors.white,
fontSize: 90,
),
),
TextSpan(
text: ".${widget.budget.toStringAsFixed(2).split(".")[1]}",
style: const TextStyle(
color: Colors.white,
fontSize: 45,
),
),
]),
style: GoogleFonts.manrope(
fontSize: 45,
letterSpacing: -4.5,
height: 1.0,
fontWeight: FontWeight.w600,
textStyle: const TextStyle(
leadingDistribution: TextLeadingDistribution.even,
)),
textAlign: TextAlign.center,
maxLines: 1,
);
The code results in this (I also tried with Text.rich instead of AutoSizeText but the result is the same):
In the image, I have wrapped the text in a red container to check its bounding box.
I am implementing this element following a design I made in Figma:
Similatly, in this image the red frame has an autolayout applied with all padding set to 0, in a way that simulates wrapping the text in a Container.
The text looks the same in both the design and the Flutter implementation, but the Flutter one is shifted upwards, which on big text sizes is really noticeable. I figured it might have something to do with the vertical alignment of the text but I could not find any way to fix it.
Is this an expected behaviour or am I doing something wrong?

How to align specific elements to the left inside a Column Widget

In the following code I'd like to align the name 'Jonny Bas' and '#coder' to leftmost
body: Column(
children: [
Center(
child:Text(
'Information',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.black,
),
),
),
SizedBox(height: 100),
Text(
'Praveen Kumar',
style: TextStyle(
fontSize: 30,
fontWeight: FontWeight.bold,
color:Colors.black,
),
),
Text(
'Coding star- 4',
style: TextStyle(
fontSize: 30,
fontWeight: FontWeight.bold,
color: Colors.black,
),
),
],
),
backgroundColor: Colors.grey,
);
}
You can use 'Align' widget or 'positioned' widget if you want to make change in only one or two widget but if want to give all of widgets the same alignment of parent widget column then you can use MainAxisAlignment and CrossAxisAlignment with specified attributes of left/right/etc...
Check the documentation links below for more info on how to use them,
https://api.flutter.dev/flutter/widgets/Align-class.html
https://api.flutter.dev/flutter/widgets/Positioned-class.html

How do I make two texts touch each other in a column in Flutter?

So if I want to seperate two texts I would use padding, but how do I bring them so close to each other? I don't want any Space between the bottom of the "Test" and the top of the "Admin".
There is no sizedboxes or padding in between them.
Code:
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
CircleAvatar(
radius: Sizes.appropriateWidth(55),
backgroundImage:
NetworkImage("https://via.placeholder.com/140x100"),
),
SizedBox(
height: Sizes.appropriateHeight(9),
),
Text(
"Test",
style: TextStyle(
fontFamily: 'SF Pro Display Bold',
fontSize: Sizes.appropriateWidth(37),
color: Color(0xFF000000),
),
),
Text(
"Admin",
style: TextStyle(
fontFamily: 'SF Pro Display Regular',
fontSize: Sizes.appropriateWidth(62),
color: Color(0xFF8D99AE),
),
),
],
),
How do I do this in flutter? Thank you!
UPDATE
You could also use the TextStyle.height parameter to achieve the same effect, Like this,
Text(
"Admin",
style: TextStyle(
fontFamily: 'SF Pro Display Regular',
fontSize: 62,
height: 0.7,
color: Color(0xFF8D99AE),
),
),
OR
Transform.translate widget to the rescue.
The Transform widget allows you to perform any kind of transformations while painting your child.
Transform.translate is the named constructor for performing a translation by an Offset. Just wrap your child and give it an Offset to get it working.
You can use it like this,
Transform.translate(
offset: Offset.fromDirection(-pi / 2, 20),
child: Text(
"Admin",
style: TextStyle(
fontFamily: 'SF Pro Display Regular',
fontSize: 62,
color: Color(0xFF8D99AE),
),
),
),
In offset: Offset.fromDirection(-pi / 2, 20), -pi / 2 means towards the top and 20 is the distance to move.
Play with the distance and see what works for you.
There is no padding from flutter itself, the padding come from the typo, imagine you have something like ÓÛÌmAm, you will need extra space for those ` ^ ' on uppercase letters. You cannot get rid off this space. For solutions you can look at Nisanth Reddy answer.

Flutter Text overflow

How to control TextOverFlow if don't know the const width of the box.
I know about
Text(
'some Text',
overflow: TextOverflow.ellipsis,
)
but is there any way to wrap widgets like expanded or flexible?
FittedBox(
fit: setToWidth ? BoxFit.contain : BoxFit.none,
child: Text(
displayText,
maxLines: 2,
textAlign: textAlign,
style: setToWidth
? TextStyle(
fontWeight: fontWeight,
color: textColor,
fontFamily: AppFonts.circularStd,
)
: TextStyle(
fontWeight: fontWeight,
color: textColor,
fontFamily: AppFonts.circularStd,
fontSize: textSize),
),
);
Try with this
You can try AutoSizeText instead.
https://pub.dev/packages/auto_size_text
Note: it'll increase your overall app size as it requires external dependencies. But it'll give you some extra features such as maxLines and minLines. I will suggest read more about it.

Default value for Text Field flutter?

How can I have a text as a place holder until onTap() happen? (onTap is on another page)
Align(
alignment: Alignment.center,
child: Text(//Default value goes here
widget.categoryTitle,//this string comes from another page it changes on every onTap()
style: TextStyle(
color: Colors.black,
fontSize: 17.0,
fontWeight: FontWeight.bold,
),
),
),
Right now if no onTap happens the Text is holding a white blank space, How to replace it with a default/initial value?
Or you can also use shorter version for conditional expressions
Align(
alignment: Alignment.center,
child: Text(
widget.categoryTitle ?? "Your Default Text Here",
style: TextStyle(
color: Colors.black,
fontSize: 17.0,
fontWeight: FontWeight.bold,
),
),
),
expr1 ?? expr2,
If expr1 (widget.categoryTitle) is non-null, returns its value; otherwise, evaluates and returns the value of expr2 ("Your Default Text Here")
Add Ternary operator and assign values as per text
Align(
alignment: Alignment.center,
child: Text(//Default value goes here
widget.categoryTitle.isEmpty? "Default Text": widget.categoryTitle,
style: TextStyle(
color: Colors.black,
fontSize: 17.0,
fontWeight: FontWeight.bold,
),
),
),