Widget build(BuildContext context) {
return Card(
child: ListTile(
title: new Text(prod_name),
leading: Image.asset(prod_img, width: 50.0, height: 50.0,),
subtitle: new Column(
children: <Widget>[
new Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(0.0),
child: new Text("ice-cream: "),
),
Padding(
padding: const EdgeInsets.all(4.0),
child: new Text(prod_ice_cream, style: TextStyle(color: Colors.red),),
),
),
new Container(
alignment: Alignment.topLeft,
child: new Text("\Rs${prod_price}", style: TextStyle(fontSize: 16.0, fontWeight: FontWeight.bold),),
),
trailing: new Column(
children: <Widget>[
new IconButton(icon: Icon(Icons.arrow_drop_up), onPressed: (){}),
new IconButton(icon: Icon(Icons.arrow_drop_down), onPressed: (){})
],
),
),
);
I am getting "A RenderFlex overflowed by 40 pixels on the bottom" after adding those two icons
I have tried stackoverflow solution gave but after that I am getting "A RenderFlex overflowed by 21 pixels on the bottom". Here are some screenshots screenshot1 screenshot2 screenshot3. Here is my full code code
If Text widget is throwing overflow error then just set set the overflow property to a Text widget.
Example:
Flexible(
child: new Container(
padding: new EdgeInsets.only(right: 13.0),
child: new Text(
'Your Text Value Here',
overflow: TextOverflow.ellipsis,
),
),
),
Here is the solution using Container instead of ListTile:
class Single_cart_prod extends StatelessWidget {
final prod_name;
final prod_img;
final prod_price;
final prod_ice_cream;
final prod_sugar;
Single_cart_prod(
{this.prod_name,
this.prod_img,
this.prod_price,
this.prod_ice_cream,
this.prod_sugar});
#override
Widget build(BuildContext context) {
return Card(
child: Container(
width: double.infinity,
height: 80,
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Expanded(
flex: 2,
child: Image.asset(prod_img, width: 50.0, height: 50.0),
),
Expanded(
flex: 3,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
prod_name,
style: TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold,
),
),
Text(
"Rs:" + prod_price.toString(),
style: TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold,
color: Colors.black38),
),
],
),
),
Expanded(
flex: 3,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"Ice Cream",
),
Text(
prod_ice_cream,
style: TextStyle(color: Colors.red),
),
],
),
),
Expanded(
flex: 3,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"Sugar",
),
Text(
prod_sugar,
style: TextStyle(color: Colors.red),
),
],
),
),
Expanded(
flex: 1,
child: Column(
children: <Widget>[
Expanded(
child: IconButton(
icon: Icon(Icons.arrow_drop_up), onPressed: () {})),
Expanded(
child: IconButton(
icon: Icon(Icons.arrow_drop_down),
onPressed: () {}))
],
),
)
]),
),
);
}
}
Output:
check out this example
import 'package:flutter/material.dart';
void main() => runApp(Cartproduct());
class Cartproduct extends StatefulWidget {
#override
_CartproductState createState() => _CartproductState();
}
class _CartproductState extends State<Cartproduct> {
var cart_list = [
{
"name": "Avocado",
"img": "images/avocado.jpg",
"price": 180,
"ice-cream": "2 scoops",
"sugar": "3 cube"
},
{
"name": "Mango",
"img": "images/mango.jpg",
"price": 180,
"ice-cream": "1 scoops",
"sugar": "2 cube"
},
];
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: new ListView.builder(
shrinkWrap: true,
itemCount: 2,
itemBuilder: (context, index) {
return new Single_cart_prod(
prod_name: cart_list[index]['name'],
prod_img: cart_list[index]['img'],
prod_price: cart_list[index]['price'],
prod_ice_cream: cart_list[index]['ice-cream'],
prod_sugar: cart_list[index]['sugar'],
);
},
),
),
);
}
}
class Single_cart_prod extends StatelessWidget {
final prod_name;
final prod_img;
final prod_price;
final prod_ice_cream;
final prod_sugar;
Single_cart_prod(
{this.prod_name,
this.prod_img,
this.prod_price,
this.prod_ice_cream,
this.prod_sugar});
#override
Widget build(BuildContext context) {
return Card(
child: ListTile(
title: new Text(prod_name),
leading: Image.asset(
prod_img,
width: 50.0,
height: 50.0,
),
subtitle: new Column(
children: <Widget>[
new Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Row(
children: <Widget>[
new Text("Ice-cream: "),
new Text(
prod_ice_cream,
style: TextStyle(color: Colors.red),
),
],
),
Row(
children: <Widget>[
new Text(" Sugar: "),
new Text(
prod_sugar,
style: TextStyle(color: Colors.red),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: new Container(
alignment: Alignment.topLeft,
child: new Text(
"\Rs${prod_price}",
style: TextStyle(
fontSize: 16.0, fontWeight: FontWeight.bold),
),
),
),
],
),
new Column(
children: <Widget>[
GestureDetector(
onTap: () {}, child: new Icon(Icons.arrow_drop_up)),
GestureDetector(
onTap: () {}, child: new Icon(Icons.arrow_drop_down)),
],
),
],
),
],
),
),
);
}
}
Let me know if it works
use SingleChildScrollView widget like this:
SingleChildScrollView(
child: your_main_Widget()
)
Related
the question is exactly same as this post
but I did not find an answer from the post.
my goal is to set the title widget in the center
and other two buttons on the right side.
the solutions in the link all put the title widget slightly on the left side.
does anyone know how to do this without using Stack? I'm afraid of the title widget being over the button widgets when the title gets too long.
the code:
SizedBox(
height: 40,
width: MediaQuery.of(context).size.width,
child: Row(
children: [
const SizedBox(width: 17),
Text(
'TITLE',
style: const TextStyle(
color: Colors.white, fontSize: 30, fontWeight: FontWeight.w500),
),
Spacer(),
Row(
children: [
GestureDetector(
onTap: (){null;},
child: SizedBox(
child: Icon(Icons.wallet_membership, color: Colors.white),
),
),
const SizedBox(width: 17),
GestureDetector(
onTap: (){Get.toNamed('/wallet_setting');},
child: const SizedBox(
child: Icon(Icons.settings, color: Colors.white),
),
),
]
)
]
),
)
Try this One
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Expanded(
child: Container(
child: Center(
child: Text(
'TITLE',
style: const TextStyle(color: Colors.white, fontSize: 30, fontWeight: FontWeight.w500),
),
)
)
),
Align(
alignment: Alignment.centerRight,
child: Row(
children: [
GestureDetector(
onTap: (){null;},
child: SizedBox(
child: Icon(Icons.wallet_membership, color: Colors.white),
),
),
const SizedBox(width: 17),
GestureDetector(
onTap: (){Get.toNamed('/wallet_setting');},
child: const SizedBox(
child: Icon(Icons.settings, color: Colors.white),
),
),
]
)
)
],
)
OutPut:
i think you should wrap it with centre
Try this approach:
first make widget from your right widgets:
Widget _rightIcons() {
return Row(children: [
GestureDetector(
onTap: () {
null;
},
child: SizedBox(
child: Icon(Icons.wallet_membership, color: Colors.white),
),
),
const SizedBox(width: 17),
GestureDetector(
onTap: () {
Get.toNamed('/wallet_setting');
},
child: const SizedBox(
child: Icon(Icons.settings, color: Colors.white),
),
),
]);
}
then build your header like this:
Row(
children: [
_rightIcons(),
Expanded(
child: Center(
child: Text(
'TITLE',
style: const TextStyle(
color: Colors.white,
fontSize: 30,
fontWeight: FontWeight.w500),
),
),
),
Opacity(
opacity: 0,
child: _rightIcons(),
)
],
),
You can use centerTitle:true on Appbar
appBar: AppBar(
centerTitle: true,
title: Text("title"),
actions: [
Another way
Container(
color: Colors.purple,
height: 40 * 2,
width: MediaQuery.of(context).size.width,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
IconButton(onPressed: () {}, icon: Icon(Icons.navigate_before)),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(
flex: 1,
child: SizedBox(),
),
Expanded(
flex: 4,
child: Text(
'TITLE',
textAlign: TextAlign.center,
style: const TextStyle(
color: Colors.white,
fontSize: 30,
fontWeight: FontWeight.w500),
),
),
Expanded(
flex: 1,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
GestureDetector(
child: Icon(Icons.wallet_membership,
color: Colors.white),
),
const SizedBox(width: 17),
GestureDetector(
onTap: () {},
child: const SizedBox(
child:
Icon(Icons.settings, color: Colors.white),
),
),
]),
),
)
],
),
],
),
)
Preferable solution:
You can implements PreferredSizeWidget to create custom Appbar. Play with color and other decoration.
class MyAppBar extends StatelessWidget implements PreferredSizeWidget {
const MyAppBar({super.key});
#override
Widget build(BuildContext context) {
return Stack(
children: [
Align(
alignment: Alignment.bottomCenter,
child: Text("title"),
),
Align(
alignment: Alignment.bottomRight,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
IconButton(
//you can use callback method to get onpressed
onPressed: () {},
icon: Icon(Icons.settings),
),
IconButton(
onPressed: () {},
icon: Icon(Icons.settings),
),
],
),
)
],
);
}
#override
Size get preferredSize => const Size.fromHeight(kToolbarHeight * 2);
}
And use like appBar: MyAppBar(),
class EditNoteList extends StatelessWidget {
final String child;
final IconData icon;
EditNoteList({required this.child, required this.icon});
#override
Widget build(BuildContext context) {
return Center(
child: Padding(
padding: EdgeInsets.symmetric(vertical: 5.0, horizontal: 5.0),
child: Stack(
children: [
Center(
child: Container(
height: 100,
width: 400,
color: Colors.cyan,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Center(
child: Text(
child,
style: TextStyle(fontSize: 16, fontFamily: 'Montserrat'),
textAlign: TextAlign.center,
)),
Row(
children: [
Icon(icon),
],
),
],
),
),
),
],
),
),
);
Here I am trying to set the design, position of the listView I want to show in the page.
class EditNote extends StatelessWidget {
final List _posts = [
'List 1',
'List 2',
'List 3',
];
final List<IconData> icons = [Icons.add, Icons.access_alarms, Icons.account_circle_outlined];
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
Expanded(
child: ListView.builder(
padding: const EdgeInsets.all(8.0),
itemCount: _posts.length,
itemBuilder: (context, index) {
return EditNoteList(
icon: icons[index],
child: _posts[index],
);
},
),
),
],
),
);
}
}
Here, I add the formatted lists to the page I want to show.
How it looks on this page.
This is how I want it to appear on the page. Each icon is on the right in the list, and all the lists are in the middle of the page.
Using an empty widget with mainAxisAlignment: MainAxisAlignment.spaceAround, can do the trick
Center(
child: Container(
height: 100,
width: 400,
color: Colors.cyan,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const SizedBox.shrink(),
Center(
child: Text(
child,
style: TextStyle(fontSize: 16, fontFamily: 'Montserrat'),
textAlign: TextAlign.center,
)),
Icon(icon),
],
),
),
),
How do I use flutter to create bar show in the screenshot below?
Row(
children: [
Expanded(
child: SizedBox(
child: Text("CONTACT US",
style: TextStyle(color: Colors.white),
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.only(left: 30.0),
child: SizedBox(
child: Text("SOCIAL MEDIA", style: TextStyle(color: Colors.white),),
),
)
),
Expanded(
child: Padding(
padding: const EdgeInsets.only(left: 30.0),
child: SizedBox(
child: Text("NEWSLETTER", style: TextStyle(color: Colors.white),),
),
)
),
],
),
you can try with something similar and adjust for your purpose and color:
class Sample extends StatefulWidget {
#override
_SampleState createState() => _SampleState();
}
class _SampleState extends State<Sample> {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
title: Text('sample'),
),
body: Container(
height: 200,
width: 2000,
color: Color(0xff808080),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Contact Us'),
Row(children: [
Icon(Icons.phone),
Text('phone')]),
Row(children: [
Icon(Icons.mail),
Text('info#test.it')]),
Row(children: [
Icon(Icons.map_outlined),
Text('unavailable')]),
],
),
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Contact Us'),
Icon(Icons.wrap_text_rounded),
Icon(Icons.point_of_sale),
],
),
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Newsletter'),
Text('Susbcribe'),
],
),
],
),
)
);
}
}
Using a Column in a Row which is a child of a Card widget, how do I set the Column width as large as the row widget?
return Card(
child: Row(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(4.0),
child: Container(
width: 100.0,
height: 100.0,
// container for image
color: Colors.grey,
),
),
Column(
//crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'Item Name',
textAlign: TextAlign.left,
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
),
),
Text(
'Previous Price',
style: TextStyle(fontSize: 15.0, color: Colors.grey),
),
Row(
children: <Widget>[
RaisedButton(
textColor: Colors.white,
child: Text('Add'),
),
Icon(Icons.add),
Container(
width: 50.0,
height: 50.0,
child: Text('12'),
color: Colors.red[200],
),
Icon(Icons.remove),
],
),
],
),
],
),
)
import 'package:flutter/material.dart';
main() => runApp(MyApp());
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: Container(
child: Card(
child: Row(
children: [
Padding(
padding: const EdgeInsets.all(4.0),
child: Container(
width: 100.0, height: 100.0,
// container for image
color: Colors.grey,
),
),
Container(
color: Colors.red,
child: Column(
mainAxisSize: MainAxisSize.min,
//crossAxisAlignment: CrossAxisAlignment.stretch,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'Item Name',
textAlign: TextAlign.left,
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
),
),
Text(
'Previous Price',
style: TextStyle(fontSize: 15.0, color: Colors.grey),
),
Container(
child: Row(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
RaisedButton(
textColor: Colors.white,
child: Text('Add'),
onPressed: () {},
),
Icon(Icons.add),
Padding(
padding: const EdgeInsets.all(5.0),
child: Container(
width: 50.0,
height: 50.0,
child: Center(child: Text('12')),
color: Colors.red[200],
),
),
Icon(Icons.remove),
],
),
),
],
),
),
],
),
),
),
)));
}
}
let me know if this works maybe I am correct, but still, the description is about confusing. looking at your code I made the prediction.
Thanks.
I'm a little confused by your description,
but crossAxisAlignment: CrossAxisAlignment.stretch might be the answer to the title.
Try using,
Expanded(
child:Container(
child: Row(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
RaisedButton(
textColor: Colors.white,
child: Text('Add'),
onPressed: () {},
),
Icon(Icons.add),
Padding(
padding: const EdgeInsets.all(5.0),
child: Container(
width: 50.0,
height: 50.0,
child: Center(child: Text('12')),
color: Colors.red[200],
),
),
Icon(Icons.remove),
],
),
),
)
I'm trying to create a ListView with Card widget.
I have the ListView and the Card but I can't figure out how to place widgets correctly inside the Card itself. This is how it looks now:
I want the favorites icon and the image to keep their place, but to move the text to be near the image and to have the "Jean-Paul-Gaultier" text to be closer to the "Le Male" and have it align left.
This is what I have so far:
Widget _myListView(BuildContext context) {
return ListView.builder(
itemBuilder: (context, index){
return Card(
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Image.network('${decoded[index]['image']}'),
),
],
),
SizedBox(width: 8,),
Column(
children: <Widget>[
Text(
'${decoded[index]['name']}',
style: TextStyle(
color: Colors.black87,
fontWeight: FontWeight.bold,
fontSize: 14,
),
),
Row(
children: <Widget>[
Text(
'${decoded[index]['brand']}',
style: TextStyle(
color: Colors.blueGrey,
fontWeight: FontWeight.normal,
fontSize: 12,
),
),
],
),
],
),
Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
IconButton(
icon: Icon(Icons.favorite_border),
),
],
),
],
),
);
},
);
}
Without the mainAxisAlignment: MainAxisAlignment.space-between the text is indeed closer to the image, but the favorites icon gets right near the text which is something I do not want.
I tried to search for answers but as I'm new to Flutter I don't really know the right keywords so I couldn't find anything.
A much easier approach would be using a ListTile
Widget _myListView(BuildContext context) {
return ListView.builder(
itemBuilder: (context, index) {
return Card(
child: ListTile(
title: Text("title"),
subtitle: Text("subtitle"),
leading: Container(
width: 48, height: 48,
child: Placeholder(), //place image here replacing the Placeholder widget
),
trailing: IconButton(
icon: Icon(Icons.favorite_border),
onPressed: () {},
),
),
);
},
);
}
which gives us
But if you want your code then here it is
Widget _myListView(BuildContext context) {
return ListView.builder(
itemBuilder: (context, index) {
return Card(
child: Row(
children: <Widget>[
Container(
margin: const EdgeInsets.all(8.0),
child: Placeholder(),
width: 48,
height: 48,
),
SizedBox(
width: 8,
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'title',
style: TextStyle(
color: Colors.black87,
fontWeight: FontWeight.bold,
fontSize: 14,
),
),
Text(
'subtitle',
style: TextStyle(
color: Colors.blueGrey,
fontWeight: FontWeight.normal,
fontSize: 12,
),
),
],
),
Spacer(),
IconButton(
icon: Icon(Icons.favorite_border),
onPressed: () {},
),
],
),
);
},
);
}
EDIT
to reduce the space between title and subtitle replace the title with following code
title: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text("title",style: Theme.of(context).textTheme.body2,),
Text("subtitle",style: Theme.of(context).textTheme.caption,),
],
),