I'm just stuck here. I've tried everything but nothing works for me. All I want is to position the Icon on the right of my Row.
Note: I've tried setting all widget inside the row into a Align Widget and handle the position of that Align widget, but nothing worked. The code below is retrieving this:
This arrow, should go to the end of the Row widget.
Here is my Code:
Row(
mainAxisAlignment: MainAxisAlignment.start, //change here don't //worked
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
margin:
EdgeInsets.only(left: 8.0, top: 8.0, bottom: 8.0, right: 12.0),
width: 15.0,
height: 15.0,
decoration: BoxDecoration(
color: task.color, borderRadius: BorderRadius.circular(40.0)),
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
task.title,
style: TextStyle(
color: Colors.black,
fontSize: 19.0,
fontWeight: FontWeight.bold),
),
Text(
'Duration: ${task.date}',
style: TextStyle(color: Colors.black, fontSize: 14.0),
)
],
),
Icon(Icons.navigate_next, color: Colors.black) // This Icon
],
),
One solution is to use the Spacer widget to fill up the space
https://docs.flutter.io/flutter/widgets/Spacer-class.html
Row(
mainAxisAlignment: MainAxisAlignment.start, //change here don't //worked
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
margin:
EdgeInsets.only(left: 8.0, top: 8.0, bottom: 8.0, right: 12.0),
width: 15.0,
height: 15.0,
decoration: BoxDecoration(
color: Colors.red, borderRadius: BorderRadius.circular(40.0)),
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"task.title",
style: TextStyle(
color: Colors.black,
fontSize: 19.0,
fontWeight: FontWeight.bold),
),
Text(
'Duration: ${somenum}',
style: TextStyle(color: Colors.black, fontSize: 14.0),
)
],
),
new Spacer(), // I just added one line
Icon(Icons.navigate_next, color: Colors.black) // This Icon
],
),
Here is what happens if you add it to the beginning of the Row.
you can add Spacer(), above the widget you want to put at the end.
Solutions:
Use Spacer:
Row(
children: <Widget>[
Text('1'),
Spacer(), // <-- Use 'Spacer'
Text('2'),
],
)
Use mainAxisAlignment:
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween, // <-- spaceBetween
children: <Widget>[
Text('1'),
Text('2'),
],
)
Output (same for both):
Why don’t you use ListTile widget? Or wrapping Column with Expanded? I think that would be working.
Related
How do I add a new column to a Card in flutter?
Currently, I have 2 Columns and can't figure out how to add a third between these since they are on the left and right sides. When I have tried adding a "new Column", it only creates a new row of text under the first Column.
This is my code so far:
Widget _buildListItem(BuildContext context, Record record) {
return Card(
key: ValueKey(record.activityName),
elevation: 8.0,
margin: new EdgeInsets.symmetric(horizontal: 10.0, vertical: 6.0),
child: Container(
decoration: BoxDecoration(color: Color.fromRGBO(64, 75, 96, .9)),
child: ListTile(
contentPadding:
EdgeInsets.symmetric(horizontal: 20.0, vertical: 10.0),
title: Text(
record.activityName,
style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: 23),
),
subtitle: Row(
children: <Widget>[
new Flexible(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
RichText(
text: TextSpan(
text: "Activations: "+record.activations+
"\n"+record.dateCompleted,
style: TextStyle(color: Colors.white),
),
maxLines: 2,
softWrap: true,
)
],
)
)
],
),
trailing: Container(
child: Hero(
tag: "avatar_" + record.activityName,
child: CircleAvatar(
radius: 32,
backgroundImage: NetworkImage(record.icon),
backgroundColor: Colors.white,
)
)
),
onTap: () {
Navigator.push(
context, MaterialPageRoute(builder: (context) => new DetailPage(record: record)));
}
),
)
);
}
I am wanting 3 columns so that I can add either an image in the middle of this card.
Current output
Expected output
Based on your screenshot, here is the solution. You can modify it according to your needs.
Widget _buildListItem(BuildContext context) {
return Card(
margin: EdgeInsets.all(12),
elevation: 4,
color: Color.fromRGBO(64, 75, 96, .9),
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0, horizontal: 16),
child: Row(
children: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text("Jumping", style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold)),
SizedBox(height: 4),
Text("Activations 9", style: TextStyle(color: Colors.white70)),
Text("03-08-19", style: TextStyle(color: Colors.white70)),
],
),
Spacer(),
CircleAvatar(backgroundColor: Colors.white),
],
),
),
);
}
Output:
Am building a navigation Drawer in flutter, with some items on top and some items on the bottom of the drawer. But in ListView, am unable to add Column widget on the bottom of the Drawer. I had even tried expanded, but that didn't resolved the problem.
Drawer(
child: ListView(
children: <Widget>[
Container(
margin: const EdgeInsets.only(top: 10, right: 10.0, left: 10.0),
padding: const EdgeInsets.only(bottom: 10),
child: Stack(
alignment: Alignment.topRight,
children: <Widget>[
Center(
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
SizedBox(height: 30),
CircleAvatar(
radius: 45,
child: Text(
'AC',
style: TextStyle(fontSize: 40),
),
),
],
),
),
],
),
),
Divider(),
Column(children: _drawerOptions),
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Text(
AppStrings.termsAndConditions,
style: TextStyle(
fontSize: 16.0,
color: Color.fromARGB(255, 39, 39, 39),
),
)
],
),
)
],
),
);
That last widget Column wrapped with Expanded was to put that Column at bottom of the Drawer, but that's not happening.
Can anyone suggest any solution?
You should do it the other way, put ListView inside Column using Expanded , here is the full working code.
Drawer(
child: Column(
children: <Widget>[
Container(
margin: const EdgeInsets.only(top: 10, right: 10.0, left: 10.0),
padding: const EdgeInsets.only(bottom: 10),
child: Stack(
alignment: Alignment.topRight,
children: <Widget>[
Center(
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
SizedBox(height: 30),
CircleAvatar(
radius: 45,
child: Text(
'AC',
style: TextStyle(fontSize: 40),
),
),
],
),
),
],
),
),
Divider(),
Expanded(child: ListView(children: _drawerOptions)),
Spacer(),
Text("Terms and conditions"),
],
),
),
Below are my code I always got some pixel overflowed on right size. I was playing with Expanded and Fixable widgets as well as mainAxisSize of row and column too.
Look at the code:
Widget productDisplayListView(List<Products> listOfProduct, int index) {
return Container(
margin: EdgeInsets.all(7),
child: Container(
child: Card(
elevation: 4,
child: Container(
padding: EdgeInsets.all(6),
child: Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Container(
height: 110,
width: 90,
child: Image.network(
listOfProduct[index].thumbnail ?? '',
fit: BoxFit.cover,
),
),
Container(
padding: EdgeInsets.all(5),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
padding: EdgeInsets.zero,
margin: EdgeInsets.zero,
height: 20,
width: 20,
child: Icon(
Icons.crop_square,
color: Colors.red,
size: 18,
),
),
Flexible(
child: Text(
'The overflowing RenderFlex has an orientation of Axis.horizontal.',
overflow: TextOverflow.ellipsis,
softWrap: true,
style: TextStyle(
fontSize: 13,
fontWeight: FontWeight.w600,
color: Theme.of(context).primaryColor),
),
)
],
),
SizedBox(height: 5),
Text(
'The overflowing RenderFlex has an orientation of Axis.horizontal.',
maxLines: 1,
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w500,
color: Colors.grey),
),
SizedBox(height: 5),
Text(
'₹${listOfProduct[index].price ?? ''}',
style: TextStyle(
fontSize: 13,
fontWeight: FontWeight.w700,
color: Colors.black),
)
],
),
)
],
),
),
),
),
);
}
I tried with many Que./Ans. on stackOverFlow but But didn't get success.
Updated Code with Fixed Overflow Error. Have to made quite a change in your Widget Tree.
Widget productDisplayListView() {
return Card(
elevation: 4,
child: Container(
padding: EdgeInsets.all(6),
child: Row(
children: <Widget>[
Container(
height: 110,
width: 90,
child: Image.network(
'https://placeimg.com/250/250/any',
fit: BoxFit.cover,
),
),
SizedBox(
width: 5.0,
),
Flexible(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
MergeSemantics(
child: Row(
children: <Widget>[
Icon(
Icons.crop_square,
color: Colors.red,
size: 18,
),
Flexible(
child: Text(
'The overflowing RenderFlex has an orientation of Axis.horizontal.',
overflow: TextOverflow.ellipsis,
softWrap: true,
style: TextStyle(
fontSize: 13,
fontWeight: FontWeight.w600,
color: Theme.of(context).primaryColor),
),
)
],
),
),
SizedBox(height: 5),
Text(
'The overflowing RenderFlex has an orientation of Axis.horizontal.',
maxLines: 1,
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w500,
color: Colors.grey),
),
SizedBox(height: 5),
Text(
'₹ 10,000',
style: TextStyle(
fontSize: 13,
fontWeight: FontWeight.w700,
color: Colors.black),
)
],
),
)
],
),
),
);
}
OutPut:
Check Your Rows Add Column Widgets. If any row or column widget has a single flexible child an error will occur
Doing like this should fix your problem:-
..... Container(
padding: EdgeInsets.all(5),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Flexible(
child: Container(
padding: EdgeInsets.zero,
margin: EdgeInsets.zero,
height: 20,
width: 20,
child: Icon(
Icons.crop_square,
color: Colors.red,
size: 18,
),
),
),
Flexible(
child: Text(
'The overflowing RenderFlex has an orientation of Axis.horizontal.',
overflow: TextOverflow.ellipsis,
softWrap: true,
style: TextStyle(
fontSize: 13,
fontWeight: FontWeight.w600,
color: Theme.of(context).primaryColor),
),
)
],
),
SizedBox(height: 5),
Flexible(
child: Text(
'The overflowing RenderFlex has an orientation of Axis.horizontal.',
maxLines: 1,
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w500,
color: Colors.grey),
),
),
SizedBox(height: 5),
Text(
'₹${listOfProduct[index].price ?? ''}',
style: TextStyle(
fontSize: 13,
fontWeight: FontWeight.w700,
color: Colors.black),
)
],
Trying to accomplish the below effect:
This is the code I have:
new Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
const Divider(
color: const Color(0xFF000000),
height: 20
),
new Text(
"OR",
style: new TextStyle(
fontSize: 20.0,
color: const Color(0xFF000000),
fontWeight: FontWeight.w200,
),
),
const Divider(
color: const Color(0xFF000000),
height: 20
),
]),
The word OR appears in the middle but both Dividers are hidden? (However, if I play with the height you can see the height of the row increase/decrease but still no divider. How can I get this effect?
use this widget, it will do exactly what you want
class OrComponent extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Row(
children: <Widget>[
Expanded(
child: Container(
height: 2.0,
color: AppColor.lighterGrey,
),
),
Container(
margin: EdgeInsets.symmetric(horizontal: 3.0),
child: Text("OR",style:TextStyle(color:Colors.black)),
),
Expanded(
child: Container(
height: 2.0,
color: Colors.grey,
),
),
],
);
}
}
You can use the Expanded widget on the two Dividers (and on the Text for coherence purposes)
Expanded(
flex: 1,
child: Divider(
color: const Color(0xFF000000),
height: 2,
)),
Expanded(
flex: 0,
child: Container(
margin: EdgeInsets.symmetric(horizontal: 15.0),
child: Text(
"OR",
style: new TextStyle(
fontSize: 20.0,
color: const Color(0xFF000000),
fontWeight: FontWeight.w200,
),
))),
Expanded(
flex: 1,
child: Divider(
color: const Color(0xFF000000),
height: 2,
))
I'm trying to leave the $ text in the top of the container, but even with FractionalOffset(1.0, 0.0) the $ text continues in the middle of the container.
What could be done to leave the $ in the top of the container?
body: new Card(
child: new Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
new Container(
padding: new EdgeInsets.only(top: 16.0),
child: new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Container(
alignment: new FractionalOffset(1.0, 0.0),
child: new Text(
'\$ ',
style: new TextStyle(
fontSize: 20.0,
fontFamily: 'Roboto',
color: new Color(0xFF26C6DA),
)
)
),
new Text(
'3,435.23',
style: new TextStyle(
fontSize: 35.0,
fontFamily: 'Roboto',
color: new Color(0xFF26C6DA)
),
)
],
),
),
new Text('general balance'),
],
),
),
Try giving your Row a CrossAxisAlignment of CrossAxisAlignment.start. Is this what you had in mind?
body: new Card(
child: new Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
new Container(
padding: new EdgeInsets.only(top: 16.0),
child: new Row(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text(
'\$ ',
style: new TextStyle(
fontSize: 20.0,
fontFamily: 'Roboto',
color: new Color(0xFF26C6DA),
)
),
new Text(
'3,435.23',
style: new TextStyle(
fontSize: 35.0,
fontFamily: 'Roboto',
color: new Color(0xFF26C6DA)
),
)
],
),
),
new Text('general balance'),
],
),
),
To align RIGHT
Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Container()
],)