How to fix top position in column in flutter? - flutter

If I add widgets in column, the top of column is going up.(picture 2)
Is there any way to fixed the top of column?(picture 3)
picture1
Column(
crossAxisAlignment: CrossAxisAlignment.center
mainAxisAlignment: MainAxisAlignment.center
children: <Widget>[
Text("Test1")
]
)
picture2
Column(
crossAxisAlignment: CrossAxisAlignment.center
mainAxisAlignment: MainAxisAlignment.center
children: <Widget>[
Text("Test1"),
Text("Test2")
]
)

Recreated what you want from the picture attached above:
Make two Expanded widgets and let the 2nd Expanded widget take a Column where you will put your Texts widgets:
return Scaffold(
body: Column(
children: [
Expanded(
child: SizedBox(),
),
Expanded(
child: SizedBox.expand(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
'Test 1',
style: TextStyle(
fontSize: 20,
color: Colors.red,
),
),
Text(
'Test 2',
style: TextStyle(
fontSize: 20,
color: Colors.red,
),
),
Text(
'Test 3',
style: TextStyle(
fontSize: 20,
color: Colors.red,
),
),
],
),
),
),
],
),
);
RESULT:

Use mainAxisAlignment: MainAxisAlignment.start

Related

how to format widgets so they are on opposite ends but also within the alignment of the other widgets

I have been trying to find a way to keep two text widgets apart but the problem was that if I used Spacer() or mainAxisAlignment: MainAxisAlignment.spaceBetween the text goes outside the alignment of the other widgets
desired layout vs current layout
child: Scaffold(
backgroundColor: Colors.transparent,
body: Container(
child: Column(children: [
Text(
'Notes Made',
textAlign: TextAlign.left,
style: const TextStyle(fontSize: 25, fontWeight: FontWeight.bold),
),
Accordion(
color: 0xffE69AF2,
title: receiptData.getVisitType(),
content: receiptData.getVisitDesc(),
),
Accordion(
color: 0xff9AF2AE,
title: 'Doctor\'s Notes',
content: receiptData.getDoctorNotes(),
),
Text(
'Receipt',
textAlign: TextAlign.left,
style: const TextStyle(fontSize: 25, fontWeight: FontWeight.bold),
),
Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Container(
child: Text(
'consultation fee:',
),
),
Container(
child: Text(
receiptData.getCFees().toString(),
),
),
],
),
RatingBar.builder(
initialRating: receiptData.getRating(),
ignoreGestures: true,
minRating: 1,
direction: Axis.horizontal,
allowHalfRating: true,
itemCount: 5,
itemPadding: EdgeInsets.symmetric(horizontal: 4.0),
itemBuilder: (context, _) => Icon(
Icons.star,
color: Colors.amber,
),
onRatingUpdate: (rating) {
print(receiptData.getRating());
},
),
]),
),
You could try wrapping each text widget in a container and setting the width of each container to a specific value.
Container(
width: 50.0, // container width
child: Text(
'Text 1',
),
),
Container(
width: 50.0,
child: Text(
'Text 2',
),
),
Additionally you could try wrapping both text widgets in a Row widget and set the mainAxisSize property to MainAxisSize.min. This will force the row to shrink-wrap the text widgets, allowing you to control the space between them by setting the mainAxisAlignment property to MainAxisAlignment.spaceBetween
Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text('Text 1'),
Text('Text 2'),
],
),

Centering an widget in a row by height in flutter

I'm sorry for my bad english
I just want to center the icon widget on the row. How can I do that?
here is the screenshot
what i want is here
here is my code
Container(
color: Colors.red,
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
CircleAvatar(
radius: 40.r,
//backgroundImage: AssetImage(_imagaPath),
backgroundColor: Colors.blue,
),
Padding(
padding: EdgeInsets.all(6.0.w),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
TextWidget(
"Good morning",
fontSize: 12.sp,
color: silver,
fontWeight: semiBold,
),
TextWidget(
"John",
fontSize: 20.sp,
fontWeight: bold,
),
],
),
),
const Spacer(),
Container(
color: Colors.yellow,
child: const Icon(Icons.add),
),
],
),
)
wrap your widget with Center like:
Center(
child: Container(
color: Colors.yellow,
child: const Icon(Icons.add),
),
),
the output would look like:
maybe you just make this crossAxisAlignment: CrossAxisAlignment.start,
to
crossAxisAlignment: CrossAxisAlignment.center, for first Row

Centring a Column inside another Column in Flutter

I'm currently writing my first Flutter App and I got a little stuck here. Basically what I'm trying to do is display an image at the top of the screen (with 100px top-margin) and display a Column which contains some Text and a Button on the centre of the screen. I tried moving mainAxisAlignment crossAxisAlignment into the outer Column Widget but that centres everything.
Here's my code:
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Center(
child: Column(
children: [
Opacity(
opacity: 0.25,
child: Container(
child: Image.asset('assets/images/logo.png'),
margin: EdgeInsets.only(top: 100),
width: 75,
),
),
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text('Please enter your first name:'),
TextButton(onPressed: () {}, child: Text('Submit'))
],
)
],
),
),
);
You actually only need to wrap the second column in an Expanded widget, "so that the child fills the available space", that's all:
Column(
children: [
Expanded(
child: Column(
children: [],
),
)],
),
try, wrap your second Column inside a Center widget.
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Center(
child: Column(
children: [
Opacity(
opacity: 0.25,
child: Container(
child: Image.asset('assets/images/logo.png'),
margin: EdgeInsets.only(top: 100),
width: 75,
),
),
Center(
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text('Please enter your first name:'),
TextButton(onPressed: () {}, child: Text('Submit'))
],
)
],
),
),
),
);
If this doesn't work you could also wrap it in an Align widget and add the alignment property to it.
Align(
alignment: Alignment.center,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text('Please enter your first name:'),
TextButton(onPressed: () {}, child: Text('Submit'))
],
),
),
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
home: Scaffold(
appBar: AppBar(),
body: Container(
height: double.infinity,
width: double.infinity,
color: Colors.blue,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
height: 200,
width: 200,
color: Colors.pink,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
),
),
],
),
),
),
),
);
}
you have to use mainAxisAlignment property to set MainAxisAlignment.center.
In order to center align your second column,
Make sure MainAxisAlignment of your Second Column is MainAxisAlignment.center
Wrap your second column inside a Align Widget
Now, wrap your Align widget with Expanded widget.
This should solve your issue.
Check this refrence.
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const SizedBox(
height: 18.0,
),
const Padding(
padding: EdgeInsets.only(left: 16.0),
child: Text(
'Recent Search(s)',
style: TextStyle(
color: Colors.black,
fontSize: 16.0,
fontWeight: FontWeight.bold),
),
),
Expanded(
child: Align(
alignment: Alignment.center,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
MdiIcons.magnify,
color: Colors.grey.shade600,
size: 72.0,
),
const SizedBox(
height: 8.0,
),
Text(
'Search for places',
style: TextStyle(
fontSize: 14.0,
color: Colors.grey.shade600,
fontWeight: FontWeight.w600),
),
Text(
'You haven\'t serached for any items yet...',
style: TextStyle(
fontSize: 12.0,
color: Colors.grey.shade700,
fontWeight: FontWeight.w500),
),
Text(
'Try now - we will help you!',
style: TextStyle(
fontSize: 12.0,
color: Colors.grey.shade700,
fontWeight: FontWeight.w500),
)
],
),
),
)
],

White space in text - Flutter

When using Unicode text in flutter there seems to be a lot of whitespace in each character which makes it hard to align. In the code and picture below, I'm trying to get the 35m sit on the cross arrows with no space in between. I'm also trying to get the three stars at the start to center align vertically with the word starburst and then center align the (35m and arrows), starburst, and thee stars all to center align with each other vertically.
Scaffold(
body: SafeArea(
child: Column(
children: [
Row(
children: [
Text(
'\u{2605}' * 3,
style: TextStyle(
fontSize: 18.0,
),
),
Text(
'Starburst',
style: TextStyle(
fontSize: 28.0,
),
),
Column(
children: [
Text(
'35m',
style: TextStyle(
fontSize: 18.0,
),
),
Text(
'\u{2194}',
style: TextStyle(fontSize: 35.0),
),
],
)
],
),
],
),
),
);
Make Row crossAxisAlignment property to CrossAxisAlignment.center
Row(
crossAxisAlignment: CrossAxisAlignment.center
children: [....
And
Make Column mainAxisAlignment propert to
MainAxisAlignment.center
Column(
mainAxisAlignment: MainAxisAlignment.center
children: [...
You can use Stack instead of Column and align the Text as per your UI specifications:
Output Using Column:
Output Using Stack:
Full code:
return Scaffold(
body: SafeArea(
child: Column(
children: [
Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
'\u{2605}' * 3,
style: TextStyle(
fontSize: 18.0,
),
),
Text(
'Starburst',
style: TextStyle(
fontSize: 28.0,
),
),
// SizedBox(width: 5.0),
Container(
width: 50.0,
child: Center(
child: Stack(
children: [
Positioned(
top: 4,
child: Text(
'35m',
style: TextStyle(
fontSize: 18.0,
),
),
),
Text(
'\u{2194}',
style: TextStyle(fontSize: 40.0),
),
],
),
),
),
],
),
],
),
),
);
You might have to tweak Stack widget's children a bit, but I hope this answer gives the idea.

Flutter: Place a particular widget in a row at center

How to place the "question mark" at center of row?
Expected result is "?" are all at center regardless of the values on left/right.
body: ListView.builder(
itemCount: testList.length,
itemBuilder: (BuildContext context, int index) {
return Card(
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
color: Colors.green,
child: Text(testList[index].n1.toString(), style: TextStyle(fontSize: 80,)),
),
Container(
color: Colors.red,
child: Text(testList[index].x, style: TextStyle(fontSize: 80,)),
),
Container(
color: Colors.green,
child: Text(testList[index].n2.toString(), style: TextStyle(fontSize: 80, )),
),
],
),
Text('>'),
Text('<'),
Text('='), //
],
),
);
}),
You can use row and Expanded with flex widget to achieve your desire output.
Following minimal code help you more.
Card(
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Expanded(
flex: 1,
child: Row(
children: [
Expanded(child: Container()),
Container(
color: Colors.green,
child: Text('10',
style: TextStyle(
fontSize: 80,
)),
),
],
),
),
Container(
color: Colors.red,
child: Text("?",
style: TextStyle(
fontSize: 80,
)),
),
Expanded(
flex: 1,
child: Row(
children: [
Container(
color: Colors.green,
child: Text('100',
style: TextStyle(
fontSize: 80,
)),
),
Expanded(child: Container()),
],
),
),
],
),
Text('>'),
Text('<'),
Text('='), //
],
),
)