Flutter: How to add space between two buttons in a row - flutter

I created two container in a row and want it in center of the screen so i used padding for it, but they more are close to each other, i want to add space between them. here is the snap of output.
Code:
Padding(
padding: EdgeInsets.fromLTRB(40, 250, 30, 0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
new Flexible(
flex: 2,
child: Container(
decoration: BoxDecoration(
color: Colors.pink,
borderRadius: BorderRadius.all(Radius.circular(3))),
height: 50,
width: 190,
child: Center(
child: Column(
children: <Widget>[
FlatButton(onPressed: (){}, child: Text("in",style:TextStyle(fontSize: 20))),
],
),
),
),
),
new Flexible(
flex: 2,
child: Container(
decoration: BoxDecoration(
color: Color(int.parse(presentcolor)),
borderRadius: BorderRadius.all(Radius.circular(3))),
height: 50,
width: 190,
child: Center(
child: Column(
children: <Widget>[
FlatButton(onPressed: (){}, child: Text("out",style:TextStyle(fontSize: 20))),
],
),
),
),
),

Use a Spacer or a SizedBox between 2 widgets.
Other option is to set mainAxisAlignment of Row to MainAxisAlignment.spaceBetween.

I started using the Gap package for adding spaces between widgets in rows or columns. Basically you just specify how big the space should be, so an 8px gap would be Gap(8).
Row(
children: [
widget1(),
Gap(8),
widget2(),
]);
The syntax is easy to use and there are special widgets like the MaxGap or SliverGap for specific use cases.

for centering this you can use Center() widget instead of padding and for putting space between the two you can add margin for the first one from the left. the code will be like this:
Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
new Flexible(
flex: 2,
child: Container(
margin: EdgeInsets.only(right: 20),
decoration: BoxDecoration(
color: Colors.pink,
borderRadius: BorderRadius.all(Radius.circular(3))),
height: 50,
width: 190,
child: Center(
child: Column(
children: <Widget>[
FlatButton(onPressed: (){}, child: Text("in",style:TextStyle(fontSize: 20))),
],
),
),
),
),
new Flexible(
flex: 2,
child: Container(
decoration: BoxDecoration(
color: Color(int.parse(presentcolor)),
borderRadius: BorderRadius.all(Radius.circular(3))),
height: 50,
width: 190,
child: Center(
child: Column(
children: <Widget>[
FlatButton(onPressed: (){}, child: Text("out",style:TextStyle(fontSize: 20))),
],
),
),
),
),
you can change righ margin to the suitable value for you.

This is the easiest way to add "Text" containing 4 spaces or or however many spaces you like:
Row(
children: [
Flexible(),
Text(" "),
Flexible(),
]);

Related

AutosizeText doesn't resize text

Im trying to create a custom listTile which can expand if it has multiple lines and maintain photo centered but my problem is AutosizeText isn't working and if I wrap AutosizeText with Expand an error apears.
I can use listTile class if there is a method to know if is tree line or not
Can you help me to solve this?
here is my code:
Card(
elevation: 8,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
SizedBox(
width: 70.0,
height: 70.0,
child: ClipRRect(
borderRadius: BorderRadius.circular(8),
child: Image.asset(
'assets/images/photo.jpg',
width: 70,
height: 70,
fit: BoxFit.cover,
),
),
),
SizedBox(
width: 24,
),
Column(
children: [
AutoSizeText(
'{otherUser.name! otherUser.lastName!}',
maxLines: 1,
style: Theme.of(context).textTheme.bodyText1!),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Container(
margin: const EdgeInsets.symmetric(horizontal: 2),
child: Image(
image: AssetImage(
'assets/icons/icon2.png'),
height: 20,
),
),
AutoSizeText(
otherUser.country![0],
maxLines: 2,
style: Theme.of(context)
.textTheme
.subtitle2!
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Container(
margin: const EdgeInsets.symmetric(horizontal: 2),
child: const FaIcon(
FontAwesomeIcons.locationDot,
size: 20,
color: Colors.black45,
),
),
AutoSizeText(
otherUser.location!,
maxLines: 2,
overflow: TextOverflow.ellipsis,
style: Theme.of(context)
.textTheme
.subtitle2!
),
]),
])
],
),
],
),
Row(
children: [Icon(Icons.star_half_outlined), Text("4.5")],
)
],
),
),
This may be due to the default minimum and maximum size in AutoSizeText widget. Change the minSize and maxSize parameter to some range that you need.
NB: If found helpful, do upvote and mark as right answer, thanks.

Flutter: Nested row in column, how to fill row without expanding parent column

This is what I have:
source code:
Container(
width: 150,
height: 150,
color: Colors.grey,
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(
height: 100,
width: 100,
color: Colors.green,
),
Row(
mainAxisSize: MainAxisSize.min,
children: [
Container(
child: Text('text'),
color: Colors.blue,
),
Container(
child: Text('text'),
color: Colors.red,
),
],
),
],
),
),
),
How can I make the blue and red container to match the green container's width without making them larger as the green container?
this is what I want to get without using a fixed with for the containers inside the row. Also I have more than one element in the column and I don't want to use fixed sizes.
If the size if fixed then you can add a width to the container which are wrap with the row widget.
The reason i gave width 50 is beacause the parent container is having width of 100 already so remaning width are given to containers
Container(
width: 150,
height: 150,
color: Colors.grey,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(
height: 100,
width: 100,
color: Colors.green,
),
Row(
mainAxisSize: MainAxisSize.min,
children: [
Container(
width: 50,
child: Text('text'),
color: Colors.blue,
),
Container(
width: 50,
child: Text('text'),
color: Colors.red,
),
],
),
],
),
),
You can use flexible widget to give row fixible width
Container(
width: 150,
height: 150,
color: Colors.grey,
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(
height: 100,
width: 100,
color: Colors.green,
),
Container(
width: 100,
child: Row(
children: [
Flexible(
flex: 1,
fit: FlexFit.tight,
child: Container(
child: Text('text'),
color: Colors.blue,
),
),
Flexible(
flex: 1,
fit: FlexFit.tight,
child: Container(
child: Text('text'),
color: Colors.red,
),
),
],
),
),
],
),
),
)
Container(
width: 150,
height: 150,
color: Colors.grey,
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(
height: 100,
width: 100,
color: Colors.green,
),
Container(
width: 100,
Row(
mainAxisSize: MainAxisSize.min,
children: [
Expanded(
child:
Container(
child: Text('text'),
color: Colors.blue,
),
),
Expanded(
child:
Container(
child: Text('text'),
color: Colors.red,
),
),
],
),
),
],
),
),
),
You can wrap only children of Row with Expanded widget.
Try this, you have to add Exapnded to the childeren of row widgets
Container(
padding: EdgeInsets.all(16),
color: Colors.grey,
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Container(
color: Colors.green,
child: Text('Hdddfhlsd', style: primaryTextStyle()),
),
Row(
mainAxisSize: MainAxisSize.max,
children: [
Expanded(
child: Container(
child: Text('text'),
color: Colors.blue,
),
),
Expanded(
child: Container(
child: Text('text'),
color: Colors.red,
),
),
],
)
],
),
)
**Replace your container by this **
Container(
width: 150,
height: 150,
color: Colors.grey,
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(
width: 100,
child: Column(
children: [
Container(
height: 100,
width: 100,
color: Colors.green,
),
Row(
mainAxisSize: MainAxisSize.min,
children: [
Expanded(
child: Container(
child: Text('text'),
color: Colors.blue,
),
),
Expanded(
child: Container(
child: Text('text'),
color: Colors.red,
),
),
],
)
],
),
),
],
),
),
)

show Image over image flutter

Need to show image above other image outside the widget margin.
I tried using stack, but it not coming outside.
Stack(children: <Widget>[
Container(child: Image.asset('assets/discover.png')),
Row(
children: <Widget>[
Expanded(child: Text(''), flex: 9),
Expanded(
child: ClipRRect(
borderRadius: new BorderRadius.circular(80.0),
child: Image.asset('assets/user.jpg')),
flex: 6,
),
Expanded(child: Text(''), flex: 9)
],
),
]);
Below source code works for your scenario. You have to give top padding for the image's container and make the entire stack's alignment as topCenter.
Additional things:
Consider giving height and width for the circled image.
Keep the big image widget in AspectRatio widget to occupy the full width of its parent widget and give fit property as well for showing entire image in the right fit.
.
Stack(
alignment: Alignment.topCenter,
children: <Widget>[
Container(
padding: EdgeInsets.only(top: 40),
child: AspectRatio(aspectRatio: 1, child: Image.asset('assets/discover.png', fit: BoxFit.cover),),
),
ClipRRect(
borderRadius: new BorderRadius.circular(40.0),
child: Image.asset('assets/user.jpg', height: 80, width: 80),
),
],
)
return Scaffold(
appBar: AppBar(
title: const Text('Flutter demo'),
),
body: Container(
padding: EdgeInsets.fromLTRB(5, 10, 5, 10),
alignment: Alignment.topCenter,
child: Stack(children: <Widget>[
Container(
margin: EdgeInsets.only(top: 30),
child: ClipRRect(
borderRadius: BorderRadius.circular(20.0),
child: Image.network(
'https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcQGLanNnFsLi3QnQFdh-k-mkwG6yrEEXhorSoElObizTnP0_8rR')),
),
Align(
alignment: Alignment.topCenter,
child: CircleAvatar(
radius: 30.0,
backgroundImage: NetworkImage(
'https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcSIekuJOwtOWtZl9QX3t46Yz_7RCZ4Kpebnugsst2OFfNl-SGjf'),
backgroundColor: Colors.grey,
),
)
]),
));
Stack(children: <Widget>[
Container(
margin: EdgeInsets.only(top: 50.0),
child: Image.network(
'https://cdn.pixabay.com/photo/2015/06/19/21/24/the-road-815297__340.jpg'),
),
Row(
children: <Widget>[
Expanded(child: Text(''), flex: 9),
Expanded(
child: ClipRRect(
borderRadius: new BorderRadius.circular(80.0),
child: Image.asset('assets/ic_profile.png'),
),
flex: 6,
),
Expanded(child: Text(''), flex: 9)
],
)
]),

How to make a bottom bar with border radius using Card and add elevation to it in Flutter?

In my Flutter project, I have created a bottom bar using Container.
Here's the code given for that-
Scaffold(
body: Column(
children: <Widget>[
Container(
color: Colors.blue,
height: 300,
),
Expanded(
child: ListView(
shrinkWrap: true,
children: <Widget>[
getCarousel(),
Column(
children: <Widget>[
Container(
height: 240,
color: Colors.yellow,
),
Container(
color: Colors.teal,
height: 300,
),
],
),
Container(
height: 350,
width: double.infinity,
color: Colors.red,
child: ListView.builder(
scrollDirection: Axis.horizontal,
padding: new EdgeInsets.only(bottom: 72.0),
itemCount: itemList.length,
itemBuilder: (BuildContext context, int position) {
return new Column(
children: <Widget>[
Expanded(
child: new Container(
width: 160,
height: 320,
color: Colors.yellow[100],
),
),
new Divider()
],
);
},
),
),
],
),
),
Container(
height: 60,
width: double.maxFinite,
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(color: Colors.grey),
borderRadius:
BorderRadius.vertical(top: Radius.circular(20.0))),
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
showBottomIcons(Icons.home, "Home", "/HomeScreen"),
showBottomIcons(
Icons.category, "Category", "/CategoryScreen"),
showBottomIcons(Icons.shopping_cart, "Cart", "/CartScreen"),
showBottomIcons(Icons.person, "Account", "/AccountScreen"),
],
),
)
],
),
)
The Output of the screen looks like that-
Now, I want to switch the Container to Card. But I got some problems-
Card is taking some by default padding and the border radius at two top corners are not working.
As the top two corners are rounded, the background is getting white(As you can see in image). I want that space red colored.
I need some elevation with border radius like below image using card-
& 3. You don't actually need card widget for this. You can add shadow on your container itself.
You have to use stack to achieve this.
As I am hoping you are using the same code as my previous answer of your another question. I have updated the answer with stack layout & your requirements
Container(
child: Stack(
fit: StackFit.expand,
children: <Widget>[
Positioned.fill(
child: Container(
color: Colors.blue,
alignment: Alignment.center,
child: Text("Hello"),
),
),
Align(
alignment: Alignment.bottomCenter,
child: Container(
height: 50,
width: double.maxFinite,
decoration: BoxDecoration(
boxShadow: [
new BoxShadow(
color: Colors.black,
blurRadius: 2.0,
),
],
color: Colors.yellow,
borderRadius:
BorderRadius.vertical(top: Radius.circular(20.0))),
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
IconButton(
icon: Icon(Icons.arrow_forward),
onPressed: () {},
),
IconButton(
icon: Icon(Icons.arrow_downward),
onPressed: () {},
),
IconButton(
icon: Icon(Icons.arrow_left),
onPressed: () {},
),
IconButton(
icon: Icon(Icons.arrow_upward),
onPressed: () {},
),
],
),
),
)
],
),
)
editing steps for your code
I will try to explain the changes.
You have multiple items in your Column. Move everything except Container which you used as bottom bar (the last child of column) to another Column widget.
Put this last container child inside Align widget like my code
Put the newly created Column in Positioned.fill widget.
Change column of scaffold to Stack
Set this stack's fit property as StackFit.expand
The body of you Scaffold should look like this.
Stack(
fit: StackFit.expand,
children: <Widget>[
Positioned.fill(
child: Column(
children: <Widget>[
.... // Your page body childs
]
),
),
Align(
alignment: Alignment.bottomCenter,
child: ... // Your bottom bar container
)
]
)
To know more about stack visit here

How to make a widget take as much width as the screen in Flutter?

In my flutter project, I haved used a Row where I aligned two text horizontally, the first Text size is fixed but the I want the second one to have as much width as the screen size.
Here's is my code-
Container(
child: Padding(
padding: const EdgeInsets.all(10.0),
child: (
Row(
children: <Widget>[
Container(
width: 150,
color: Colors.blue,
child: Text("City: "),
),
Container(
color: Colors.red,
child: Text("London"),
)
],
)
),
),
),
Container(
child: Padding(
padding: const EdgeInsets.all(10.0),
child: (
Row(
children: <Widget>[
Container(
width: 150,
color: Colors.blue,
child: Text("Country: "),
),
Container(
color: Colors.red,
child: Text("England "),
)
],
)
),
),
),
Container(
child: Padding(
padding: const EdgeInsets.all(10.0),
child: (
Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
width: 150,
color: Colors.blue,
child: Text("Address: "),
),
Container(
color: Colors.red,
child: Text("aaaaa zzzzz wwwww qqqqqq rrrrr"),
)
],
)
),
),
),
With the above code I got the following output-
The problem is the black marked section in the image.
So, I need help to know how the text will take as much width as the screen size & rest of the text will be shown in the next line.
The text widget has a property called overflow you can use that to either clip the extra string or put ellipsis. You could try different types of TextOverflow like a clip, ellipsis, fade, etc. As for the putting it to the next line you could set maxLines property to any number you like.
example:
Container(
width: MediaQuery.of(context).size.width / 2
child: Text(
"Hello world!!!!",
maxLines: 2,
overflow: TextOverflow.ellipsis
),
);
This problem can be solved by a widget called Flexible. I had to write few more lines for solving these but it works perfectly for the problem mentioned in the question.
So, here's the code-
Container(
child: Padding(
padding: const EdgeInsets.all(10.0),
child: (
Row(
children: <Widget>[
Container(
width: 150,
color: Colors.blue,
child: Text("City: "),
),
Container(
color: Colors.red,
child: Text("London"),
)
],
)
),
),
),
Container(
child: Padding(
padding: const EdgeInsets.all(10.0),
child: (
Row(
children: <Widget>[
Container(
width: 150,
color: Colors.blue,
child: Text("Country: "),
),
Container(
color: Colors.red,
child: Text("England "),
)
],
)
),
),
),
Container(
child: Padding(
padding: const EdgeInsets.all(10.0),
child: (
Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
width: 150,
color: Colors.blue,
child: Text("Address: "),
),
new Flexible(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
color: Colors.red,
child: Text("aaaaa zzzzz qqqqqq eeeeeeeeeeeeee "),
)
],
),
),
],
)
),
),
),