So, I'm new in the Flutter scene and I'm having some trouble finishing a message bubble for a chat practice app. It's working ok, like this:
https://i.ibb.co/1qstjqR/now.jpg
The code that generates the bubble is like this:
Widget bubble(){
return Padding(
padding: padding(),
child: Row(
mainAxisAlignment:
mine ? MainAxisAlignment.end : MainAxisAlignment.start,
children: <Widget>[
SizedBox(height: 30),
Flexible(
child: Row(
mainAxisAlignment:
mine ? MainAxisAlignment.end : MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
//BUBBLE TAIL
mine
? Container()
: Transform(
alignment: Alignment.center,
transform: Matrix4.rotationY(math.pi),
child: CustomPaint(
painter: CustomShape(colorChat),
),
),
Flexible(
child: Container(
padding: EdgeInsets.all(10),
decoration: BoxDecoration(
color: colorChat,
borderRadius: border(),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
//MESSAGE STR CONTENT
Text(
messageStr,
textAlign: TextAlign.justify,
style: TextStyle(fontSize: 16),
),
//TIMESTAMP (AND DETAIL IN FUTURE)
Padding(
padding: const EdgeInsets.only(top: 5),
child: Text(
dateTimeStr,
style: TextStyle(color: Colors.white, fontSize: 10),
),
),
],
),
),
),
//BUBBLE TAIL
mine ? CustomPaint(painter: CustomShape(colorChat)) : Container(),
],
)),
],
),
);
The problem is: I'm trying to put a string containing more detail about de message (like the extension and size of a file) in the same row of the datetime, to the far left.
The closest I've managed to do is the code below:
Padding(
padding: const EdgeInsets.only(top: 5),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Expanded(
child: Align(
alignment: Alignment.centerLeft,
child: Text(
"some detail",
style: TextStyle(color: Colors.white, fontSize: 10),
),
),
),
Align(
alignment: Alignment.centerRight,
child: Text(
dataMsg,
style: TextStyle(color: Colors.white, fontSize: 10),
),
),
],
),
)
But that makes the bubble being always big:
https://i.ibb.co/JjYx0vY/detail-bad.jpg
That's because of the Expanded. But without it the first Align widget don't work and stay to the far right.
How I'd like it to be:
https://i.ibb.co/gt633Gc/detail-ok.jpg
Thanks!
Remove the Expanded and Align widgets. And add property mainAxisAlignment: MainAxisAlignment.end to the Row widget.
Padding(
padding: const EdgeInsets.only(top: 5),
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Text(
"some detail",
style: TextStyle(color: Colors.white, fontSize: 10),
),
Align(
alignment: Alignment.centerRight,
child: Text(
"hello",
style: TextStyle(color: Colors.white, fontSize: 10),
),
),
],
),
)
Related
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.
Why does the 1st two work but not the 3rd?? what's the alternative to add vertical divider with height that stretch to the max height of the row?
These 2 works
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
// crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Container(
child: Text('Kicukiro'),
),
Container(width: 1,color: Colors.black,height: double.infinity,),
Container(
padding: EdgeInsets.symmetric(horizontal: 10),
child: Text('Kicukiro'),
)
],
)
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: Container(
child: Text('Kicukiro'),
),
),
// Container(width: 1,color: Colors.black,height: double.infinity,),
Expanded(
child: Container(
padding: EdgeInsets.symmetric(horizontal: 10),
child: Text('Kicukiro'),
),
)
],
)
this does not work
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: Container(
child: Text('Kicukiro'),
),
),
Container(width: 1,color: Colors.black,height: double.infinity,),
Expanded(
child: Container(
padding: EdgeInsets.symmetric(horizontal: 10),
child: Text('Kicukiro'),
),
)
],
)
Test this and it will work
IntrinsicHeight(
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: Container(
child: const Text("Kicukiro", style: TextStyle(fontSize: 52),)),
),
Container(color:Colors.black, width: 1),
Expanded(
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 10),
child: const Text("Kicukiro", style: TextStyle(fontSize: 52),)),
),
],
),
),
In the last screenshot please add the Container under an Expanded widget. Use children property under Row widget.
Here is an example code
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Expanded(
flex: 5,
child: Padding(
padding: EdgeInsets.all(10.0),
child: Center(
child: Text(
'This is question',
style: TextStyle(
fontSize: 25.0,
color: Colors.white,
),
),
),
),
),
Expanded(
child: Padding(
padding: EdgeInsets.all(10.0),
child: FlatButton(
//textColor: Colors.white,
color: Colors.green,
child: Text(
'True',
style: TextStyle(color: Colors.white, fontSize: 20.0),
),
onPressed: () {},
),
)),
Expanded(
child: Padding(
padding: EdgeInsets.all(10.0),
child: FlatButton(
//textColor: Colors.white,
color: Colors.red,
child: Text(
'False',
style: TextStyle(color: Colors.white, fontSize: 20.0),
),
onPressed: () {},
),
)),
]
The third is true, in fact, its existence in an SingleChildScrollView has created a problem
It is fixed by putting SizedBox in Row and giving it height
The reason for this problem is the size of the height of the container, which is determined by double.infinity and due to its presence in an SingleChildScrollView, it continues indefinitely and an error was created.
SingleChildScrollView(
child: SizedBox(
height: 250,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(child: Container(child: Text("Kicukiro"))),
Container(
width: 1,color: Colors.black,height: double.infinity,
),
Expanded(child: Container(
padding: EdgeInsets.symmetric(horizontal: 10),
child: Text("Kicukiro"))),
],
),
),
),
How to make alignment in row if I give space in between spaces occupies between widgets, but I need to get two text at the left and another one in the right. How to do it?
Row(
// mainAxisAlignment:
// MainAxisAlignment.spaceBetween,
children: [
Container(
child: Icon(Icons.comment),
margin: EdgeInsets.only(left: 5.0),
),
Container(
child: Text(snapshot.data![index].comments.toString(),
style: TextStyle(
fontSize: 14.0, fontWeight: FontWeight.bold)),
margin: EdgeInsets.only(left: 5.0),
),
Align(
alignment: Alignment.topRight,
child: Text(snapshot.data![index].date.toString()),
)
]),
check the image in the description
Try below code hope its help to you. You can used Spacer class for that
Row(
children: [
Container(
padding: EdgeInsets.all(5),
child: Icon(Icons.comment),
),
Text('6'),
Spacer(),
Container(
padding: EdgeInsets.all(5),
child: Text('Aug 4, 2021'),
),
],
),
Your result screen->
you can use Spacer() for this purpose, like this:
Row(
children:[
Text("TEXT 1"),
Text("TEXT 2"),
Spacer(),
Text("TEXT 3"),
]
)
Row(
children: [
Expanded(
child: Align(
alignment: Alignment.topLeft,
child: Row(
children: [
Container(
child: Icon(Icons.comment),
margin: EdgeInsets.only(left: 5.0),
),
Container(
child: Text("6",
style: TextStyle(
fontSize: 14.0,
fontWeight: FontWeight.bold)),
margin: EdgeInsets.only(left: 5.0),
)
],
),
),
),
Expanded(
child: Align(
alignment: Alignment.topRight,
child: Text("Aug 4,2021"),
),
)
])
OR
Row(
// mainAxisAlignment:
// MainAxisAlignment.spaceBetween,
children: [
Wrap(
children: [
Container(
child: Icon(Icons.comment),
margin: EdgeInsets.only(left: 5.0),
),
Container(
child: Text("6",
style: TextStyle(
fontSize: 14.0,
fontWeight: FontWeight.bold)),
margin: EdgeInsets.only(left: 5.0),
)
],
),
Expanded(
child: Align(
alignment: Alignment.topRight,
child: Text("Aug 4,2021"),
),
)
])
OR
Row(
// mainAxisAlignment:
// MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: Wrap(
children: [
Container(
child: Icon(Icons.comment),
margin: EdgeInsets.only(left: 5.0),
),
Container(
child: Text("6",
style: TextStyle(
fontSize: 14.0,
fontWeight: FontWeight.bold)),
margin: EdgeInsets.only(left: 5.0),
)
],
),
),
Text("Aug 4,2021")
])
FullCode:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
// testIt();
runApp(MaterialApp(home: Mainwidget()));
}
class Mainwidget extends StatefulWidget {
const Mainwidget({Key? key}) : super(key: key);
#override
_MainwidgetState createState() => _MainwidgetState();
}
class _MainwidgetState extends State<Mainwidget> {
#override
Widget build(BuildContext context) {
var richText = RichText(
text: TextSpan(
text: '*',
style: TextStyle(
fontSize: 25, color: Colors.red, fontWeight: FontWeight.bold)));
return Scaffold(
body: SafeArea(
child: Container(
padding: EdgeInsets.all(8.0),
height: 100,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0,vertical: 8),
child: Row(
// mainAxisAlignment:
// MainAxisAlignment.spaceBetween,
children: [
Wrap(
children: [
Container(
child: Icon(Icons.comment),
margin: EdgeInsets.only(left: 5.0),
),
Container(
child: Text("6",
style: TextStyle(
fontSize: 14.0,
fontWeight: FontWeight.bold)),
margin: EdgeInsets.only(left: 5.0),
)
],
),
Expanded(
child: Align(
alignment: Alignment.topRight,
child: Text("Aug 4,2021"),
),
)
]),
)
],
),
),
),
);
}
Row buildRow2() {
return Row(
// mainAxisAlignment:
// MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: Wrap(
children: [
Container(
child: Icon(Icons.comment),
margin: EdgeInsets.only(left: 5.0),
),
Container(
child: Text("6",
style: TextStyle(
fontSize: 14.0,
fontWeight: FontWeight.bold)),
margin: EdgeInsets.only(left: 5.0),
)
],
),
),
Text("Aug 4,2021")
]);
}
Expanded buildExpanded() {
return Expanded(
child: Align(
alignment: Alignment.topLeft,
child: Row(
children: [
Container(
child: Icon(Icons.comment),
margin: EdgeInsets.only(left: 5.0),
),
Container(
child: Text("6",
style: TextStyle(
fontSize: 14.0,
fontWeight: FontWeight.bold)),
margin: EdgeInsets.only(left: 5.0),
)
],
),
),
);
}
}
Use Expanded widget.
Row(
children: [
Container(
child: Icon(Icons.comment),
margin: EdgeInsets.only(left: 5.0),
),
Expanded(
child: Container(
child: Text(
snapshot.data![index].comments
.toString(),
style: TextStyle(
fontSize: 14.0,
fontWeight:
FontWeight.bold)),
margin: EdgeInsets.only(left: 5.0),
),
),
Align(
alignment: Alignment.topRight,
child: Text(snapshot
.data![index].date
.toString()),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Row(
children: [
Container(
child: Icon(Icons.comment),
margin: EdgeInsets.only(left: 5.0),
),
Container(
child: Text(
snapshot.data![index].comments.toString(),
style: TextStyle(
fontSize: 14.0,
fontWeight: FontWeight.bold,
),
),
margin: EdgeInsets.only(left: 5.0),
),
],
),
Text(snapshot.data![index].date.toString()),
],
),
I guess this is what you want.
This will help you.. result in pic you can decore your's
Padding(
padding: const EdgeInsets.only(right: 10,left:10),
child: Container(
padding: const EdgeInsets.only(left:20,right:20),
color: Colors.blue,
width: MediaQuery.of(context).size.width,
height: 50,
child: Row(
children:[
Align(
alignment: Alignment.centerLeft,
child: Wrap(
children: const [
Icon(Icons.settings),
Icon(Icons.settings)
]
),
),
const Spacer(),
const Align(
alignment: Alignment.centerRight,
child:Text('00:00'),
),
],
),
),
),
🚀 Here's a straight forward way to do:
flutter center row:
mainAxisAlignment: MainAxisAlignment.center //Center Column contents vertically,
flutter float right
Center(
child: Container(
height: 120.0,
width: 120.0,
color: Colors.blue[50],
child: Align(
alignment: Alignment.topRight,
child: FlutterLogo(
size: 60,
),
),
),
)
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.
I am creating a screen like Instagram profile but mainaxisAlignment.spaceEvenly is not working. It works just fine if the row is not inside any other row or column but in this case It didn't.
How can I solve this? Please Help.
Container(
margin: EdgeInsets.all(15.0),
child: Column(
children: <Widget>[
Row(
children: <Widget>[
Container(
width: 100.0,
height: 100.0,
margin: EdgeInsets.only(
right: 10.0,
),
decoration: new BoxDecoration(
shape: BoxShape.circle,
image: new DecorationImage(
fit: BoxFit.fill,
image: new CachedNetworkImageProvider(
profile.dp,
scale: 100.0,
),
),
),
),
Column(
children: <Widget>[
Row(
mainAxisAlignment:
MainAxisAlignment.spaceEvenly,
children: <Widget>[
Column(
children: <Widget>[
Text(
profile.postcount.toString(),
style: TextStyle(
fontWeight: FontWeight.bold),
),
Text('posts'),
],
),
Column(
children: <Widget>[
Text(
profile.followers.toString(),
style: TextStyle(
fontWeight: FontWeight.bold),
),
Text('followers'),
],
),
Column(
children: <Widget>[
Text(
profile.followings.toString(),
style: TextStyle(
fontWeight: FontWeight.bold),
),
Text('following'),
],
),
],
),
new RaisedButton(
child: Text('Edit Profile'),
onPressed: () {},
),
],
)
],
)
],
)),
Expectation:
Reality:
Here you go
Widget _buildRow() {
return Container(
padding: const EdgeInsets.all(16),
child: Row(
children: <Widget>[
CircleAvatar(
radius: 40,
backgroundImage: AssetImage("your_image_asset"),
),
SizedBox(width: 12),
Expanded(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Column(
children: <Widget>[
Text("2", style: TextStyle(fontWeight: FontWeight.bold)),
Text("Gold", style: TextStyle(color: Colors.grey)),
],
),
Column(
children: <Widget>[
Text("22", style: TextStyle(fontWeight: FontWeight.bold)),
Text("Silver", style: TextStyle(color: Colors.grey)),
],
),
Column(
children: <Widget>[
Text("5464", style: TextStyle(fontWeight: FontWeight.bold)),
Text("Reputation", style: TextStyle(color: Colors.grey)),
],
),
],
),
OutlineButton(onPressed: () {}, child: Text("CopsOnRoad (Edit Profile)")),
],
),
),
],
),
);
}