TextWidthBasis.longestLine alternative for AutoSizeText? - flutter

For a single line of text I have
Center(
child: Container(
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
width: 1,
),
),
),
child: Text('Some text',
style: TextStyle(
fontSize: 60),
textAlign: TextAlign.center,
textWidthBasis: TextWidthBasis.longestLine,
),
),
),
Which works great. But when I use AutoSizeText for multiple lines of text I can't remove the padding with textWidthBasis: TextWidthBasis.longestLine so the underline goes fully across the screen.
Is there another way to remove the padding?

I've got two options for you, both of them are workarounds more than solutions to be honest, but sometimes in Flutter, that's the best you've got 😩
(2 is a lot better than 1, good luck! happy coding!)
1.
First is giving the original container a small padding as to not disrupt the text much:
Padding(
padding: const EdgeInsets.symmetric(horizontal: 8.0),
child: Container(
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
width: 1,
),
),
),
child: AutoSizeText('Some text\ndummy text\n some texttttttt',
style: TextStyle(
fontSize: 60),
textAlign: TextAlign.center,
),
),
),
Which gives this output: https://gyazo.com/0c9d343ba6843d61327f2a33893fc81d
2.
You can wrap the container with a Column and simply but a divider underneath it as so (keep in mind that "indent" and "endIndent" controlles the right/left "padding" of the divider!):
Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
child: AutoSizeText(
'Some text\ndummy text\n some texttttttt',
style: TextStyle(fontSize: 60),
textAlign: TextAlign.center,
),
),
Divider(height: 1, thickness: 1, color: Colors.black, indent: 40, endIndent: 40,)
],
),
Which gives this output: https://gyazo.com/b7cefe2b36442939e13ecf344a858f31

Related

How to set width after using row in Flutter?

In this code when I am not using row then proceed button is not expanding but when I am using Row for adding one more widget then that is expanding.
This is my code.
Stack(
alignment: Alignment.centerRight,
children: [
Container(
margin: EdgeInsets.all(5),
height: 150,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(
'assets/assets_allocation_image.png',
),
fit: BoxFit.fill)),
),
Column(
children: [
Text(
"Know your\nASSET ALLOCATION",
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontSize: tSize18,
fontWeight: FontWeight.w600),
overflow: TextOverflow.ellipsis,
maxLines: 2,
),
Padding(
padding: const EdgeInsets.only(
top: 20,
),
child: Container(
padding: const EdgeInsets.only(
left: 10, right: 10, top: 5, bottom: 5),
decoration: BoxDecoration(
color: orangeColor,
border: Border.all(
color: orangeColor, width: 2.0, style: BorderStyle.solid),
borderRadius: BorderRadius.all(
Radius.circular(3),
),
),
child: Row( // here
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Proceed',
style: TextStyle(
fontSize: tSize12,
fontWeight: FontWeight.w500,
color: whiteColor),
textAlign: TextAlign.center,
),
SizedBox(width: 3,),
Icon(
Icons.arrow_circle_right_outlined,
color: Colors.white,
size: 18,
)
],
),
),
),
],
),
],
);
This is my ui
I want UI like this
How to possible without wrapping in container and without giving width Becouse if I will give fix width then if user changed text size from mobile device then it will give me overflow error.
so how to fix it?
You can apply mainAxisSize and row will not expand anymore
Row(
mainAxisSize: MainAxisSize.min,
...
Container tries to expand to the available space. Since you are using Column as it's parent, there are no restrictions on width of the Container. Hence it expands to its full width.
One way you can achieve is by replacing the Container with DecoratedBox which has no size constraints and tries to fit to the width and height of its child widget. Use proper padding around the Text widget. That would be sufficient to achieve the above.
Also add mainAxisSize: MainAxisSize.min to Row widget.
Example code is as below. Please look at the code comment as well for the changes
Column(
children: [
Text(
"Know your\nASSET ALLOCATION",
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontSize: tSize18,
fontWeight: FontWeight.w600),
overflow: TextOverflow.ellipsis,
maxLines: 2,
),
child: DecoratedBox( // change Container to DecoratedBox
decoration: BoxDecoration(
color: orangeColor,
border: Border.all(
color: orangeColor, width: 2.0, style: BorderStyle.solid),
borderRadius: BorderRadius.all(
Radius.circular(3),
),
),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal:24,vertical:16),//here change the value as per your requirement
child :Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Proceed',
style: TextStyle(
fontSize: tSize12,
fontWeight: FontWeight.w500,
color: whiteColor),
textAlign: TextAlign.center,
),
SizedBox(width: 3,),
Icon(
Icons.arrow_circle_right_outlined,
color: Colors.white,
size: 18,
)
],
),
),
),
],
)

How properly center text in a container

I am trying to display some numbers inside some circular containers. Inside of the container I am using a center widget with the text as its child. However, as you can see in the image attached the text is very noticeably not centered. My code:
return SizedBox(
height: 21,
width: 21,
child: DecoratedBox(
decoration: BoxDecoration(
color: MyColors.gray2,
shape: BoxShape.circle,
),
child: Center(
child: Text(
location.toString(),
textScaleFactor: 1,
style: positionTextStyle,
),
),
),
);
Example image
use textAlign: TextAlign.center,
child: Text(
location.toString(),
textAlign: TextAlign.center,
textScaleFactor: 1,
style: positionTextStyle,
),
Better with container
Container(
height: 21,
width: 21,
alignment: Alignment.center,
child: DecoratedBox(
decoration: BoxDecoration(
color: MyColors.gray2,
shape: BoxShape.circle,
),
child: Text(
"12",
textAlign: TextAlign.center,
textScaleFactor: 1,
// style: positionTextStyle,
),
),
)
You can manage size with padding which will eliminate the issue of text being overflowed and will keep the text in the center. Also you can use a single container and add decoration to it like
return Container(
padding: EdgeInsets.all(5),
decoration: BoxDecoration(
color: MyColors.gray2,
shape: BoxShape.circle,
),
child: Text(
location.toString(),
textScaleFactor: 1,
style: positionTextStyle,
),
);

How to Place two Text Form Fields side by side in flutter

I am trying to create a form in flutter. In which I want to keep the two text fields side by side as shown below:
I want to place Department and Year of Study field in same row as shown in picture. I am unable to do it. I have tried this using Row widget as shown below:
Row(
children: <Widget>[
Column(
children: [
const Padding(
padding: EdgeInsets.fromLTRB(0, 0, 0, 5),
child: Text(
"Department",
style: TextStyle(
color: Colors.black,
fontSize: 16,
fontFamily: "Poppins",
fontWeight: FontWeight.w700,
),
),
),
TextFormField(
decoration: const InputDecoration(
border: OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(8))),
),
),
],
),
Column(
children: [
const Padding(
padding: EdgeInsets.fromLTRB(0, 0, 0, 5),
child: Text(
"Year of Study",
style: TextStyle(
color: Colors.black,
fontSize: 16,
fontFamily: "Poppins",
fontWeight: FontWeight.w700,
),
),
),
TextFormField(
decoration: const InputDecoration(
border: OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(8))),
),
),
],
),
However, its giving me error. On searching the google I have got some idea to wrap TextFormField in Flexible() widget but that also didn't worked for me and giving me a different type of error. I have also tried to wrap in Expanded() but the error is still coming.
Try below code as per your above image show.
Column(
children: [
ListTile(
title: Text('Full Name'),
subtitle: TextFormField(
decoration: const InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(8))),
hintText: 'Full Name',
),
),
),
SizedBox(
height: 5,
),
Row(
children: [
Expanded(
child: ListTile(
title: Text('Department'),
subtitle: TextFormField(
decoration: const InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(8))),
hintText: ' Department',
),
),),
),
SizedBox(
width: 5,
),
Expanded(
child:ListTile(
title: Text('Year Of Study'),
subtitle: TextFormField(
decoration: const InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(8))),
hintText: ' Year Of Study',
),
),),
),
],
)
],
),
Result Screen->
Wrap each of them in sized boxed and give them a fixed width. The problem is because Rows and TextFormFields expand infinitely. And when putting a textformfield inside a row, you'll get a renderflex error. A sized box would solve this by giving it a limited width.
You can wrap the childrens into Expanded Widget:
Row(
children: [
Expanded(
flex: 3,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Padding(
padding: EdgeInsets.fromLTRB(10, 0, 0, 5),
child: Text(
"Department",
style: TextStyle(
color: Colors.black,
fontSize: 16,
fontFamily: "Poppins",
fontWeight: FontWeight.w700,
),
),
),
TextFormField(
decoration: const InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(8))),
),
),
],
),
),
SizedBox(width: 8),
Expanded(
flex: 2,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Padding(
padding: EdgeInsets.fromLTRB(10, 0, 0, 5),
child: Text(
"Year of Study",
style: TextStyle(
color: Colors.black,
fontSize: 16,
fontFamily: "Poppins",
fontWeight: FontWeight.w700,
),
),
),
TextFormField(
decoration: const InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(8))),
),
),
],
),
),
],
),
I also added a SizedBox in between, the flex property to have different sizes for the text fields and the crossAxisAlignment: CrossAxisAlignment.start to have the text on the left side.

How to use center widget for the child of a wrap widget without expanding the child to the full width?

When I use center in a text used in Wrap widget as shown in the code below:
Wrap(
children: [
Padding(
padding: const EdgeInsets.symmetric(
vertical: 6.0),
child: Container(
decoration: BoxDecoration(
color: Colors.transparent,
borderRadius: BorderRadius.circular(27),
border: Border.all(
color: Colors.grey.withOpacity(0.60)),
),
padding: EdgeInsets.fromLTRB(9,6,9,6),
child: Center(<--placing this center widget makes the child expand to full width
child: Text(
'NTU',
style: TextStyle(
color: Colors.white.withOpacity(0.87),
fontSize: 16.5,
),
),
),
),
)
]),
the container expands to the full width as shown below:
What I want is:
But placing a center widget in the child of the Wrap widget seems to make it expand to the full width. Any suggestion on how to make it shrink to the child's width even when the center widget is used? Thank you.
Set widthFactor of Center widget to set its width to the child's width multiplied by this factor.
return Wrap(
children: [
Padding(
padding: const EdgeInsets.symmetric(
vertical: 6.0),
child: Container(
decoration: BoxDecoration(
color: Colors.transparent,
borderRadius: BorderRadius.circular(27),
border: Border.all(
color: Colors.grey.withOpacity(0.60)),
),
padding: const EdgeInsets.fromLTRB(9,6,9,6),
child: Center(
widthFactor: 1.5,
child: Text(
'NTU',
style: TextStyle(
color: Colors.white.withOpacity(0.87),
fontSize: 16.5,
),
),
),
),
)
]),
Try giving width to your container,
Wrap(
children: [
Align(
alignment: Alignment.centerLeft,
child: Container(
width: 100,
decoration: BoxDecoration(
color: Colors.transparent,
borderRadius: BorderRadius.circular(27),
border: Border.all(color: Colors.grey.withOpacity(0.60)),
),
padding: EdgeInsets.fromLTRB(9, 6, 9, 6),
child: Center(
child: Text(
'NTU',
style: TextStyle(
color: Colors.black.withOpacity(0.87),
fontSize: 16.5,
),
),
),
),
)
],
),
try this code:
Wrap(
children: [
Row(
children: [
Flexible(
child: SizedBox(width: MediaQuery.of(context).size.width),
),
Container(
width: 100,
decoration: BoxDecoration(
color: Colors.transparent,
borderRadius: BorderRadius.circular(27),
border: Border.all(color: Colors.grey.withOpacity(0.60)),
),
padding: EdgeInsets.fromLTRB(9, 6, 9, 6),
child: Center(
child: Text(
'NTU',
style: TextStyle(
color: Colors.black.withOpacity(0.87),
fontSize: 16.5,
),
),
),
),
Flexible(
child: SizedBox(width: MediaQuery.of(context).size.width),
),
],
),
],
),

Text width based on text length in flutter

I have this ListView.generator that returns such Containers
Container(
child: Row(
children: <Widget>[
Container(
child: null,
width: 10,
height: 10,
decoration: BoxDecoration(
color: social[i].color,
shape: BoxShape.circle,
),
margin: EdgeInsets.only(right: 7, top: 1,),
),
Text(
social[i].name,
style: TextStyle(
color: Colors.white,
fontFamily: 'Muli',
fontSize: 24.0,
fontWeight: FontWeight.w700
),
)
],
),
width: social[i].name.length * 16.0,
)
Right know I'm making use of width to set it based on text length but it's giving me and inadequate result as spacing between twitter and instagram is bigger than it should be.
The result I want being this with an even space between them.
A Wrap widget with spacing set to even wouldn't help as I want this list to have a full width of the user resolution and to be aligned to the left.
you can use mainAxisAlignment: MainAxisAlignment.spaceEvenly for Row element like
Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Container(
child: null,
width: 10,
height: 10,
decoration: BoxDecoration(
color: social[i].color,
shape: BoxShape.circle,
),
margin: EdgeInsets.only(right: 7, top: 1,),
),
Text(
social[i].name,
style: TextStyle(
color: Colors.white,
fontFamily: 'Muli',
fontSize: 24.0,
fontWeight: FontWeight.w700
),
)
],
),
width: social[i].name.length * 16.0,
)
In case you want to know more about it. https://medium.com/jlouage/flutter-row-column-cheat-sheet-78c38d242041 you can check this article. I found it really helpful.