How properly center text in a container - flutter

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

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

TextWidthBasis.longestLine alternative for AutoSizeText?

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

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

flutter, how to wrap barcode and qr code in the same container?

I'm trying to make the barcode above the qrcode and I will like to make them in the same container, how should I edit it?
Thanks Alot!
body: SafeArea(
child: Center(
child: Column(
children: [
SizedBox(
height: 50,
child: SfBarcodeGenerator(
value: 'www.syncfusion.com',
),
),
Container(
decoration: BoxDecoration(
color: mFillColor,
borderRadius: BorderRadius.circular(12),
border: Border.all(color: mBorderColor, width: 1),
),
margin: const EdgeInsets.only(left: 30, right: 30),
width: double.infinity,
height: 350,
child: Padding(
padding: const EdgeInsets.all(90.0),
child: QrImage(
data: 'This is a simple QR code',
version: QrVersions.auto,
gapless: false,
),
),
),
],
),
),
),
What this code will show:
The effect that I want:
This is how you do this task. First you design a parent Container and then use a Column widget which contains two sizedBox as a children one is for barcode and second is for QrCode. Just like this
Scaffold(
body: SafeArea(
child: Container(
height: 250,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(12),
border: Border.all(color: Colors.black, width: 1),
),
margin: const EdgeInsets.all(30),
width: double.infinity,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
SizedBox(
height: 50,
child: SfBarcodeGenerator(
value: 'www.syncfusion.com',
),
),
const Text(
"2810 1102 0604 3155 4434 2047",
style: TextStyle(
color: Colors.grey,
fontWeight: FontWeight.w500,
),
),
SizedBox(
height: 150,
child: QrImage(
data: 'This is a simple QR code',
version: QrVersions.auto,
gapless: false,
),
),
const Text(
"Any text here",
style: TextStyle(
color: Colors.grey,
fontWeight: FontWeight.w500,
),
),
],
),
),
),
)

How to make text align to centre of a Sizedbox in Flutter

i use text inside a sizedbox in flutter and the text is sticked to the top of the box how can i put the text in the middle of the box.
child: Container(
width: 240.0,
height: 42.0,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(24.0),
color: const Color(0xff2c2c2c),
),
child: SizedBox(
child: Text(
'SIGN UP',
style: TextStyle(
fontFamily: 'Arial',
fontSize: 18,
color: Colors.white,
height: 1,
),
textAlign: TextAlign.center,
),
),
),
Wrap the Text in a Center widget
child: Container(
width: 240.0,
height: 42.0,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(24.0),
color: const Color(0xff2c2c2c),
),
child: Center(
child: Text(
'SIGN UP',
style: TextStyle(
fontFamily: 'Arial',
fontSize: 18,
color: Colors.white,
height: 1,
),
textAlign: TextAlign.center,
),
),
),
Also, as far I can tell, you can remove the SizedBox widget, This is the result