How to place image & textfield both at center in flutter.
Below is code what i have tried so far any help is appreciated!
Container(
child: Padding(
padding: const EdgeInsets.only(top: 32.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
child: Image.asset(
'images/Warning.png',
),
),
Padding(
padding: const EdgeInsets.only(left: 8.0),
child: Container(
alignment: Alignment.center,
child: Text(
'OTP is valid for 5 minutes only.',
style: TextStyle(
color: Color(0xFF4B4B4B),
fontSize: 12,
fontFamily: 'Soleil_Regular',
),
),
),
),
],
),
),
),
trying as some thing shown in the picture both text & image is in same line any help is appreciated!
Simple way of doing it by using RichText
RichText(
textAlign: TextAlign.center,
text: TextSpan(children: [
WidgetSpan(child: Icon(Icons.ac_unit)), // use image in your case instead of icon
TextSpan(text: "This is Text")
]))
Full Snippet
Container(
child: Padding(
padding: const EdgeInsets.only(top: 32.0),
child: Text.rich(TextSpan(
style: TextStyle(
color: Color(0xFF4B4B4B),
fontSize: 12,
fontFamily: 'Soleil_Regular'),
children: [
WidgetSpan(
child: Image.asset(
'images/Warning.png',
alignment: Alignment.center,
),
),
WidgetSpan(
child: SizedBox(
width: 10,
)),
TextSpan(
text: 'OTP is valid for 5 minutes only.',
),
],
)),
),
),
Give your image a size and you will get your result
Container(
child: Padding(
padding: const EdgeInsets.only(top: 32.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
child: Image.network(
'http://orbitsystemsinc.com/images/banner-round-bg.png',width: 40,height: 40,
),
),
Padding(
padding: const EdgeInsets.only(left: 8.0),
child: Container(
alignment: Alignment.center,
child: Text(
'OTP is valid for 5 minutes only.',
style: TextStyle(
color: Color(0xFF4B4B4B),
fontSize: 12,
fontFamily: 'Soleil_Regular',
),
),
),
),
],
),
),
)
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.
I have 3 container in a column i need to show first 2 container in middle and the 3rd in last on the screen
Something like this
my code is this
return Container(
child: Column(
children: <Widget>[
Container(
width: stackWidth * 0.75,
child: Image.asset('assets/logo.png')),
Container(
color: Color(0xffff4b4b),
child: Padding(
padding: EdgeInsets.all(5),
child: Text(
'Only for LunchBox management',
style: TextStyle(color: Colors.white, fontSize: 18),
)),
),
Container(
child: Padding(
padding: EdgeInsets.all(5),
child: Text(
'Powered by LunchBox KSA',
style: TextStyle(color: Colors.white, fontSize: 18),
)),
)
],
),
);
Its showing in column but i need to show first 2 container of column in middle and third in last
You can do the following:
return Container(
child: Column(
children: <Widget>[
const Spacer(), // <---- New
Container(
width: stackWidth * 0.75,
child: Image.asset('assets/logo.png')),
Container(
color: Color(0xffff4b4b),
child: Padding(
padding: EdgeInsets.all(5),
child: Text(
'Only for LunchBox management',
style: TextStyle(color: Colors.white, fontSize: 18),
)),
),
const Spacer(), // <---- New
Container(
child: Padding(
padding: EdgeInsets.all(5),
child: Text(
'Powered by LunchBox KSA',
style: TextStyle(color: Colors.white, fontSize: 18),
)),
)
],
),
);
Please use this!
return Scaffold(
backgroundColor: Colors.black,
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
color: Colors.white,
width: MediaQuery.of(context).size.width * 0.75,
height: 80,
alignment: Alignment.center,
child: Text(
"YOUR LOGO",
style: Theme.of(context).textTheme.headline6,
),
),
SizedBox(height: 10),
Container(
color: Color(0xffff4b4b),
child: Padding(
padding: EdgeInsets.all(5),
child: Text(
'Only for LunchBox management',
style: TextStyle(color: Colors.white, fontSize: 18),
)),
),
],
),
),
bottomNavigationBar: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
child: Padding(
padding: EdgeInsets.all(20),
child: Text(
'Powered by LunchBox KSA',
style: TextStyle(color: Colors.white, fontSize: 18),
)),
),
],
),
);
\ **
it may be help you
**
body: Container(
child: Column(
children: [
Expanded(
flex: 0,
child: Container(
height: 80,
color: Colors.green,
)),
Expanded(
flex: 2,
child: Container(
height: 150,
color: Colors.white,
child: Image.asset("assets/github.png"),
)),
Expanded(
flex: 0,
child: Container(
height: 80,
color: Colors.deepOrange,
child: Align(
alignment: Alignment.bottomCenter,
child: Padding(
padding: EdgeInsets.all(8),
child: Text(
"Git Hub Power",
style: TextStyle(color: Colors.white, fontSize: 25),
))),
)),
],
),
),
Here's my widget tree that's causing me the problems:
Container(
margin: EdgeInsets.all(15),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
ClipRRect(
borderRadius: cardBorderRadius,
child: Image(
image: NetworkImage(widget.article["thumbnail"]),
width: MediaQuery.of(context).size.width * .18,
height: MediaQuery.of(context).size.width * .18,
fit: BoxFit.cover,
),
),
Container(
margin: EdgeInsets.only(left: 15),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
padding: EdgeInsets.only(top: 15),
child: Text(
widget.article["title"],
overflow: TextOverflow.clip,
style: TextStyle(
fontWeight: FontWeight.w700,
fontSize: 28,
),
),
),
Container(
padding: EdgeInsets.only(top: 20),
child: Row(
children: <Widget>[
Container(
margin: EdgeInsets.only(right: 5),
child: Icon(Icons.star_border, size: 15),
),
Text(
'${widget.article["readTime"] ~/ 60} min',
overflow: TextOverflow.clip,
maxLines: 3,
softWrap: true,
style: TextStyle(
fontWeight: FontWeight.w500,
fontSize: 14,
),
),
],
),
),
],
),
),
],
),
);
Now the problem is I am getting this effect rather than text get clipped.
Note: I have tried other answers as well using the Expanded & Flexible widgets but none of them worked for me.
Any help will be appreciated!
Put the textfield into a flexible widget like this (work for me):
Row(
children: <Widget>[
Flexible(
child: Text(
"some text",
textAlign: TextAlign.start,
overflow: TextOverflow.ellipsis,
),
),
... more widgets
],
),
I'm trying to align all the text to left. However, it's in the middle always as you can see the image below. I've tried using Align widget and setting textAlign property on Text widget to TextAlign.left but no luck.
card.dart
Row(
children: <Widget>[
Card(
elevation: 2,
child: ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(4)),
child: Image.network('https://loremflickr.com/100/100'),
),
),
Padding(
padding: const EdgeInsets.only(left: 8),
child: Column(
children: <Widget>[
Align(
alignment: Alignment.centerLeft,
child: Text(
'Marry Jane',
textAlign: TextAlign.left,
style: TextStyle(fontSize: 16),
),
),
Align(
alignment: Alignment.centerLeft,
child: Text(
'Make-up Artist',
textAlign: TextAlign.left,
style: TextStyle(fontSize: 14, color: Color(0xff666666)),
),
),
Row(
children: <Widget>[
Text(
'3.5',
textAlign: TextAlign.left,
style: TextStyle(fontSize: 18, color: Color(0xff666666)),
),
Icon(FontAwesomeIcons.starHalf)
],
)
],
),
)
],
),
Have you tried giving the Column CrossAxisAlignment.start?
Row(
children: <Widget>[
Card(
elevation: 2,
child: ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(4)),
child: Image.network('https://loremflickr.com/100/100'),
),
),
Padding(
padding: const EdgeInsets.only(left: 8),
child: Column(
/// Add this
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'Marry Jane',
style: TextStyle(fontSize: 16),
),
Text(
'Make-up Artist',
style: TextStyle(fontSize: 14, color: Color(0xff666666)),
),
Text(
'3.5',
style: TextStyle(fontSize: 18, color: Color(0xff666666)),
),
],
),
)
],
)
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.