How to automatically wrap text after N characters? - flutter

I'm displaying items with ListView.builder like this
As you can see, if really long names like "Speedy Motor Services 1234567890" is used, then the text beside it will be severely squeezed.
Code:
return ListView.builder(
itemBuilder: (context, position) {
return Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: const EdgeInsets.fromLTRB(12.0, 12.0, 12.0, 6.0),
child: FittedBox(
fit:BoxFit.fitWidth,
child: Text(_displayedList[position].name,
style: TextStyle(fontSize: 18.0, fontWeight: FontWeight.bold)),
),
),
Padding(
padding: const EdgeInsets.fromLTRB(12.0, 6.0, 12.0, 12.0),
child: Text(_displayedList[position].location,
style: TextStyle(fontSize: 16.0),
),
),
Padding(
padding: const EdgeInsets.fromLTRB(12.0, 6.0, 12.0, 12.0),
child: Text(_displayedList[position].distance.toString()+" KM", style: TextStyle(fontSize: 16.0),
),
),
],
),
Expanded(
child:Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
//Expanded(),
Text(_displayedList[position].services.toString(),
style: TextStyle(color: Colors.grey), softWrap: true,
),
Padding(
padding: const EdgeInsets.all(8.0),
child: IconButton(
onPressed: () {},
icon: Icon(Icons.phone, size: 20, color: Colors.green[700],),
),
),
],
),
)
),
],
),
Divider(
height: 2.0,
color: Colors.grey,
)
],
);
},
itemCount: _displayedList.length);
},))]),)
I tried using FittedBox, it didn't perform like I expect.
Is there any way to, say wrap the text each 15 characters?
What I want is something like this:

You just need to wrap the content with the expanded and use flex
Here is the code snnipet inside the column
Expanded(
flex : 2,
child :Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: const EdgeInsets.fromLTRB(12.0, 12.0, 12.0, 6.0),
child: FittedBox(
fit:BoxFit.fitWidth,
child: Text(_displayedList[position].name,
style: TextStyle(fontSize: 18.0, fontWeight: FontWeight.bold)),
),
),
Padding(
padding: const EdgeInsets.fromLTRB(12.0, 6.0, 12.0, 12.0),
child: Text(_displayedList[position].location,
style: TextStyle(fontSize: 16.0),
),
),
Padding(
padding: const EdgeInsets.fromLTRB(12.0, 6.0, 12.0, 12.0),
child: Text(_displayedList[position].distance.toString()+" KM", style: TextStyle(fontSize: 16.0),
),
),
],
),
),Expanded(
flex :1,
child:Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
//Expanded(),
Text(_displayedList[position].services.toString(),
style: TextStyle(color: Colors.grey), softWrap: true,
),
Padding(
padding: const EdgeInsets.all(8.0),
child: IconButton(
onPressed: () {},
icon: Icon(Icons.phone, size: 20, color: Colors.green[700],),
),
),
],
),
)
),
],
),
Now you need to just adjust the flex to the UI requirement.

Related

Align children to top of row in Flutter?

after I write my posts in my application, my photo and date center the text. I need to pin the photo and date to the beginning. What should I do for this?
............................................................................................................................................................................................
child: Row( children: [
Padding(
padding: EdgeInsets.only(top: 15),
child: CircleAvatar(
radius: 20,
backgroundImage: NetworkImage(
userData['photoUrl'],
),
),
),
Expanded(
child: Padding(
padding:
EdgeInsets.only(left: 8, top: 20),
child: Container(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment:
CrossAxisAlignment.start,
children: [
Text(
userData['username'],
style: TextStyle(
fontWeight:
FontWeight.bold),
),
Container(
width: double.infinity,
padding: const EdgeInsets.only(
top: 8,
),
child: RichText(
text: TextSpan(
style: const TextStyle(
color: primaryColor),
children: [
TextSpan(
text:
' ${(snap.data()! as dynamic)['description']}',
),
],
),
),
),
],
),
),
),
),
Container(
padding:
EdgeInsets.symmetric(vertical: 4),
child: Text(
DateFormat.yMd().format(
(snap.data()!
as dynamic)['datePublished']
.toDate(),
),
style: const TextStyle(
fontSize: 15,
color: secondaryColor,
),
),
),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
In your Row, set crossAxisAlignment: CrossAxisAlignment.start. This will cause the Row's children to be aligned at the top of the row.

Flutter - How to fix text overflow in a card?

I'm building an app and I want to show this text but i need put some "limit" to not happen this:
card with overflow
so, I tried a few things (like SizedBox to limit the text, AutoSizeText to change the font size... but i don't know how to limit this and make this responsive), but to no avail.
this is the code of the card:
Card(
child: Container(
height: 120,
child: Padding(
padding: EdgeInsets.only(
left: 15,
right: 15,
top: 10,
bottom: 10,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
ClipOval(
child: Image.network(
pedido.logomarca,
height: 90,
width: 90,
),
),
Padding(
padding: EdgeInsets.only(left: 20),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
SizedBox(
child: AutoSizeText(
pedido.nomefantasia.toUpperCase(),
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
),
Row(
children: <Widget>[
ClipOval(
child: Container(
height: 10,
width: 10,
color: Colors.green,
),
),
Padding(
padding: EdgeInsets.only(
left: 8,
),
child: Text(
pedido.statuspedidodescricao,
),
),
),
],
),
],
),
),
Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
DateFormat('dd/MM/yyyy').format(pedido.datavenda),
style: TextStyle(
color: Colors.blueGrey,
fontSize: 14,
),
),
Padding(
padding: EdgeInsets.only(top: 10),
child: Text(
'R\$ ${pedido.valortotal.toStringAsFixed(2).replaceAll('.', ',')}',
style: TextStyle(
color: Colors.green[600],
fontSize: 16,
),
),
),
telefone
? OutlineButton(
onPressed: () {},
splashColor: Colors.green,
highlightColor: Colors.green,
highlightedBorderColor: Colors.green,
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: <Widget>[
Text('Ligar'),
Padding(
padding: EdgeInsets.only(left: 3),
child: Icon(
Icons.call,
size: 15,
),
),
],
),
)
: Container(),
],
),
),
],
),
),
),
),
AutoSized text has a parameter you need to set. maxlines: 2
AutoSizeText(
pedido.nomefantasia.toUpperCase(),
maxlines: 2,
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
Documentation
change this:
Padding(
padding: EdgeInsets.only(
left: 8,
),
child: Text(
pedido.statuspedidodescricao,
),
),
to this:
Expanded(
child: Padding(
padding: EdgeInsets.only(
left: 8,
),
child: Text(
pedido.statuspedidodescricao,
overflow: TextOverflow.ellipsis,
maxLines: 1,
),
),
)

How can I remove space between children in the row?

enter image description here
I would like to remove space between icon and text. How can I remove space between children in the row?/////////
/////////////////////////////////////////////////////////////////////////////////////////
Widget topSection() {
return Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Padding(
padding: EdgeInsets.only(left: 20.0, top: 180),
child: MaterialButton(
onPressed: () {},
color: Colors.black,
textColor: Colors.white,
child: Icon(
Icons.done,
size: 24,
),
shape: CircleBorder(),
)),
Expanded(
child: Padding(
padding: EdgeInsets.only(top: 180),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: EdgeInsets.only(left: 4),
child: Text("Wellness Coaching",
style: TextStyle(
fontSize: 18,
fontFamily: "LatoR",
fontWeight: FontWeight.w900)),
),
Padding(
padding: EdgeInsets.only(left: 4),
child: Text("Connect to your data",
style: TextStyle(fontSize: 18, fontFamily: "LatoR")),
)
],
),
)),
],
);
}
mainAxisAlignment: MainAxisAlignment.end,
Try deleting this line.

List Item UI design with flutter

I am beginner for flutter.
I have one lock icon image, that need to display half within cell and half out of cell.
I want to create UI for ListItem.
I already write code as below, but it not work:
ListTile makeListTile() => ListTile(
contentPadding:
EdgeInsets.symmetric(horizontal: 10.0),
title: Text(
"Item Title",
style: TextStyle(color: Colors.black54, fontWeight: FontWeight.bold),
),
subtitle: Row(
children: <Widget>[
Expanded(
flex: 0,
child: Icon(Icons.settings, size: 20.0),),
Expanded(
flex: 4,
child: Padding(
padding: EdgeInsets.only(left: 10.0),
child: Text("120",
style: TextStyle(color: Colors.black54))),),
Expanded(
flex: 4,
child: Padding(
padding: EdgeInsets.only(left: 10.0),
child: Text("150",
style: TextStyle(color: Colors.black54))),
),
Expanded(
child: Padding(
padding: EdgeInsets.only(left: 5.0, right: 5.0),
child: Icon(Icons.add, size: 20,)),
),
Expanded(
child: Padding(
padding: EdgeInsets.only(left: 10.0, right: 10.0),
child: Text("1",
style: TextStyle(color: Colors.black54))),),
Expanded(
child: Padding(
padding: EdgeInsets.only(left: 5.0, right: 5.0),
child: Icon(Icons.add, size: 20,)),
)
],
),
trailing:
new Stack(
children:[
Icon(Icons.lock, size: 20.0)
]),
onTap: () {
},
);
Please help me.
Thanks.
I suggest you try first yourself, because it will help to learn a different way to build the UI. If it's urgent then here is the code. But again I suggest trying yourself first. Everything in the dummy you need to change according to your requirement.
Stack(
children: <Widget>[
Container(
margin: const EdgeInsets.all(15.0),
decoration: BoxDecoration(
shape: BoxShape.rectangle,
borderRadius: BorderRadius.circular(10.0),
border: Border.all(width: 2.0, color: Colors.yellow)),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(5.0),
child: Text(
'Item Name',
style: TextStyle(
color: Colors.yellow, fontSize: 14.0),
),
),
Padding(
padding: const EdgeInsets.all(5.0),
child: Icon(
Icons.close,
color: Colors.yellow,
size: 30.0,
),
)
],
),
Padding(
padding: const EdgeInsets.only(bottom: 5.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Row(
children: <Widget>[
Icon(
Icons.adjust,
color: Colors.yellow,
size: 20.0,
),
Text(
'Dummy Text',
style: TextStyle(
color: Colors.yellow, fontSize: 14.0),
)
],
),
Row(
children: <Widget>[
Icon(
Icons.add_circle_outline,
color: Colors.yellow,
size: 20.0,
),
Padding(padding: const EdgeInsets.all(2.0)),
Text(
'1',
style: TextStyle(
color: Colors.yellow, fontSize: 14.0),
),Padding(padding: const EdgeInsets.all(2.0)),
Icon(
Icons.remove_circle,
color: Colors.yellow,
size: 20.0,
),
Padding(padding: const EdgeInsets.all(5.0))
],
)
],
),
)
],
),
),
Align(
alignment: Alignment.centerRight,
child: Icon(
Icons.group,
size: 30.0,
color: Colors.yellow,
),
)
],
)
This is how its look like for now:
1 use stack widget
2 add card with hight wight
3 add right side icon
4 add column
5 add row inside column

Flutter: How to place a button in the middle of 2 containers?

UI Layout
If the purple area is inside an Expanded widget, how would I go about to position the button? I've implemented that UI by setting a fixed height to the purple container but haven't been able to understand how to achieve the same effect if the height is variable, depending on the content.
I was able to get close by using Stack and Positioned widgets but then the button is not really clickable. If anyone can give me a general idea on how to achieve the desired goal I would be thankful.
Here's my attempt (demo case)
#override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Expanded(
flex: 1,
child: SingleChildScrollView(
child: Column(
children: <Widget>[
Container(
decoration: BoxDecoration(
color: Colors.redAccent,
),
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Stack(
children: <Widget> [
Padding(
padding: const EdgeInsets.only(bottom: 40.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Icon(
Icons.book,
color: Colors.white,
size: 40.0
),
Container(
width: 90.0,
child: new Divider(color: Colors.white),
),
Text(
"Some random text -",
style: TextStyle(color: Colors.white, fontSize: 25.0),
),
Padding(
padding: const EdgeInsets.only(top: 8.0, right: 70.0),
child: Row(
children: <Widget>[
Flexible(
child: Text(
'More random text that can vary a lot!',
style: TextStyle(
color: Colors.white,
fontSize: 16.0,
fontWeight: FontWeight.bold,
),
),
),
],
),
),
Padding(
padding: const EdgeInsets.only(top: 8.0),
child: Row(
children: <Widget>[
Text(
'Random text ',
style: TextStyle(
color: Colors.white,
fontSize: 16.0,
fontWeight: FontWeight.bold,
),
),
],
),
),
Padding(
padding: const EdgeInsets.only(top: 8.0),
child: Row(
children: <Widget>[
Text(
'Random',
style: TextStyle(
color: Colors.white,
fontSize: 16.0,
fontWeight: FontWeight.bold,
),
),
],
),
),
],
),
),
Positioned(
bottom: 0.0,
left: MediaQuery.of(context).size.width - 100.0,
child: FractionalTranslation(
translation: const Offset(0.0, 0.8),
child: FloatingActionButton(
backgroundColor: Colors.white,
foregroundColor: Colors.redAccent,
child: new Icon(
Icons.map
),
onPressed: () {
},
),
),
),
]
),
),
),
Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 8.0, left: 8.0),
child: Row(
children: <Widget>[
Flexible(
child: Text(
"Random text",
style: new TextStyle(
fontFamily: 'Raleway',
fontSize: 16.0,
color: Colors.black38
)
),
),
],
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
"Random text - long text",
style: new TextStyle(
fontFamily: 'Raleway',
fontSize: 18.0
)
),
),
],
)
],
)
),
),
],
);
Explaining what I described in the comments:
You can use the FractionalTranslation widget to shift your FAB half way down.
FractionalTranslation(translation: Offset(0, .5), child: FloatingActionButton(...))
This requires you to have it positioned at the bottom of the purple area (referring to your screenshot) beforehand, but I assume that you have that figured out.