Align bottom center a text inside a Container of stack - flutter

It's the first time I use Flutter and I'm having some problem aligning containers.
Here is my code:
#override
Widget build(BuildContext context) {
return const MyItem(
imgSrc: 'https://images.unsplash.com/photo-1610655012457-9cbd66fe510b?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2940&q=80',
text: 'Lorem ipsum');
}
class MyItem extends StatelessWidget {
const MyItem(
{Key? key,
required this.imgSrc,
required this.text})
: super(key: key);
final String imgSrc;
final String text;
#override
Widget build(BuildContext context) {
return Container(
decoration:
BoxDecoration(border: Border.all(color: Colors.purple, width: 3)),
margin: const EdgeInsets.symmetric(horizontal: 3),
child: Stack(children: [
Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.blueAccent, width: 3)),
child: Image.network(imgSrc, fit: BoxFit.cover),
),
Container(
alignment: Alignment.bottomCenter,
decoration: BoxDecoration(
border: Border.all(color: Colors.orange, width: 3)),
child: Text(
text.toUpperCase(),
style: const TextStyle(
color: Colors.white,
fontSize: 14.0,
fontWeight: FontWeight.bold),
),
),
]));
}
}
The result is:
But I want the text lorem ipsum at the bottom side, not near the top side.
Seems like alignment: Alignment.bottomCenter doesn't work.
What am I missing?

You just have to wrap the container widget with the Positioned widget, just tell the bottom: 0,
import 'package:flutter/material.dart';
class MyItem extends StatelessWidget {
const MyItem ({Key? key}) : super(key: key);
final imgSrc =
'https://images.unsplash.com/photo-1610655012457-9cbd66fe510b?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2940&q=80';
final String text = 'Lorem ipsum';
#override
Widget build(BuildContext context) {
return Container(
decoration:
BoxDecoration(border: Border.all(color: Colors.purple, width: 3)),
margin: const EdgeInsets.symmetric(horizontal: 3),
child: Stack(
children: [
Container(
height: MediaQuery.of(context).size.height,
decoration: BoxDecoration(
border: Border.all(color: Colors.blueAccent, width: 3)),
child: Image.network(imgSrc, fit: BoxFit.cover),
),
Positioned(
bottom: 0,
width: MediaQuery.of(context).size.width,
child: Container(
height: 50,
alignment: Alignment.center,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
border: Border.all(color: Colors.orange, width: 3),
),
child: Text(
'$text'.toUpperCase(),
//text.toUpperCase(),
style: const TextStyle(
color: Colors.white,
fontSize: 14.0,
fontWeight: FontWeight.bold),
),
),
),
],
),
);
}
}

You also can use below codes
class MyItem extends StatelessWidget {
const MyItem({Key? key, required this.imgSrc, required this.text})
: super(key: key);
final String imgSrc;
final String text;
#override
Widget build(BuildContext context) {
return Container(
decoration:
BoxDecoration(border: Border.all(color: Colors.purple, width: 3)),
margin: const EdgeInsets.symmetric(horizontal: 3),
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(
imgSrc,
),
fit: BoxFit.cover),
border: Border.all(color: Colors.blueAccent, width: 3)),
child: Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.orange, width: 3)),
child: Text(
text.toUpperCase(),
textAlign: TextAlign.center,
style: const TextStyle(
color: Colors.white,
fontSize: 14.0,
fontWeight: FontWeight.bold),
),
),
));
}
}

You must fix the height and width of Stack as it is like a graph, where the centre is (0,0). There x & y has a value from -1 to 1 in the container. You go up from the centre value goes nearer to -1 and down +1 same as If you go left -1 right +1.
After that, you can use AlignmentDirectional to tell where exactly you want to put chid of the stack. in your case as below.
Align(
alignment: const AlignmentDirectional(0, 1),
child: SizedBox()....
==Now coming to your code must be like this... review comments in the same too.
#override
Widget build(BuildContext context) {
return Container(
width: MediaQuery.of(context).size.width * 1,
//Master container size of mobile screen and limits of stack.
height: MediaQuery.of(context).size.height * 1,
decoration: BoxDecoration(
border: Border.all(color: Colors.purple, width: 3),
),
margin: const EdgeInsets.symmetric(horizontal: 3),
child: Stack(
children: [
Align(
alignment: const AlignmentDirectional(0, 0),
child: Container(
width:
MediaQuery.of(context).size.width * 1, //You can fix your own
height: MediaQuery.of(context).size.height * 1,
decoration: BoxDecoration(
border: Border.all(color: Colors.blueAccent, width: 3),
),
child: Image.network(imgSrc, fit: BoxFit.cover),
),
),
Align(
alignment: const AlignmentDirectional(0, 1),
child: Container(
width: MediaQuery.of(context).size.width * 1,
//You can fix your own
height: MediaQuery.of(context).size.height * 0.1,
/*alignment: Alignment.bottomCenter,*/
//This will only work inside of container.
decoration: BoxDecoration(
border: Border.all(color: Colors.orange, width: 3),
),
child: Text(
text.toUpperCase(),
style: const TextStyle(
color: Colors.white,
fontSize: 14.0,
fontWeight: FontWeight.bold,
),
),
),
),
],
),
);
}

You should use the Align widget. with assign value to the alignment attribute the container will expand to fill its parent and position its child within itself according to the given value.
here is the sample :
class MyItem extends StatelessWidget {
const MyItem(
{Key? key,
required this.imgSrc,
required this.text})
: super(key: key);
final String imgSrc;
final String text;
#override
Widget build(BuildContext context) {
return Container(
decoration:
BoxDecoration(border: Border.all(color: Colors.purple, width: 3)),
margin: const EdgeInsets.symmetric(horizontal: 3),
child: Stack(children: [
Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.blueAccent, width: 3)),
child: Image.network(imgSrc, fit: BoxFit.cover),
),
Align(
alignment: Alignment.bottomCenter,
child:Container(
alignment: Alignment.bottomCenter,
decoration: BoxDecoration(
border: Border.all(color: Colors.orange, width: 3)),
child: Text(
text.toUpperCase(),
style: const TextStyle(
color: Colors.white,
fontSize: 14.0,
fontWeight: FontWeight.bold),
),
),),
]));
}
}

Related

How To Make Responsive positioned inside a Stack in flutter and how to control the space between elements inside the row and the row inside coulmn

This is The Result in tablet and phone the different is show
this is main design i build it
**How To Solve It **
this is my code in this design
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import '../../../../core/app_color.dart';
import '../../../../core/app_style.dart';
import '../../../../models/product_model.dart';
class ProductView extends StatelessWidget {
ProductView({Key? key, required this.productModel}) : super(key: key);
final ProductModel productModel;
final ScrollController scrollController = ScrollController();
final double _height = 100;
#override
Widget build(BuildContext context) {
double height = MediaQuery.of(context).size.height;
double width = MediaQuery.of(context).size.width;
final double percent = (productModel.disPrice - productModel.price) /
productModel.disPrice *
100;
return Container(
decoration: BoxDecoration(
color: AppColor.white,
borderRadius: BorderRadius.circular(20),
),
margin: const EdgeInsets.only(left: 15, right: 15, bottom: 20),
padding: const EdgeInsets.symmetric(
horizontal: 8,
),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Stack(children: [
Container(
width: width * 0.42,
height: height * 0.21,
decoration: BoxDecoration(
color: Colors.transparent,
image: DecorationImage(
image: AssetImage(productModel.imageUrl),
),
),
),
Positioned(
bottom: height * 0.01,
right: 0.0,
child: Container(
width: 48.31,
height: 19,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: AppColor.lightBlue,
// border: Border.all(color: AppColor.white)),
),
child: Text(
productModel.productNameType,
style: bodyStyle1.copyWith(
fontSize: 7.sp, color: AppColor.white),
textAlign: TextAlign.center,
),
),
),
//height * 0.026
//width * 0.04
Align(
widthFactor: width * 0.42 / width * 12,
child: Container(
decoration: BoxDecoration(
color: AppColor.lightBlue,
borderRadius: BorderRadius.circular(30)),
child: const FaIcon(
Icons.favorite_outline_rounded,
color: AppColor.white,
),
),
),
]),
Text(
productModel.productName,
style: bodyStyle1.copyWith(color: AppColor.lightBlue),
),
Text(
productModel.productType,
style: bodyStyle2.copyWith(fontSize: 11.sp, color: AppColor.grey),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'${productModel.price}',
style: bodyStyle1.copyWith(
fontSize: 12.sp,
color: AppColor.lightGreyHeader,
decoration: TextDecoration.lineThrough),
),
Text(
'${productModel.disPrice}',
style: bodyStyle1.copyWith(
fontSize: 12.sp,
color: AppColor.black,
),
),
percent != 0
? Container(
width: 48.31,
height: 19,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: Colors.transparent,
border: Border.all(color: AppColor.lightBlue)),
child: Text(
'${percent.toStringAsFixed(0)}%',
style: bodyStyle2.copyWith(
fontSize: 9.sp, color: AppColor.lightBlue),
textAlign: TextAlign.center,
),
)
: Container(),
],
)
],
),
);
}
}
how to solve the responsive positioned
and how to control the space between elements inside the row
i tired of try and try and can not any soloution of that issue

Flutter add Text styling to a custom class

I want to add text styling to the Score class in this widget. I have another widget that uses this widget to generate a circle with a number in it.
Center(
child: Container(
width: 100.0,
height: 100.0,
decoration: BoxDecoration(
color: Colors.grey[600],
shape: BoxShape.circle,
),
child: Score(score: 'score',
// Are you looking for something like this
class CenterClass extends StatelessWidget {
const CenterClass({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Container(
width: 100.0,
height: 100.0,
decoration: BoxDecoration(
color: Colors.grey[600],
shape: BoxShape.circle,
));
}
}
YOu can achieve same via Widgets
Widget circleContainer() {
return Container(
width: 100.0,
height: 50.0,
decoration: BoxDecoration(
color: Colors.grey[600],
shape: BoxShape.circle,
));
}
I think you wanted to like A BALL that show the score of ball like below image
If I Am Right then code is below
Container(
width: 27,
height: 27,
child: Padding(
padding: const EdgeInsets.all(1),
child: Container(
child: Center(
child: Text(
'4',
style: TextStyle(
color: Colors.white,
fontWeight:
FontWeight.bold,
fontSize: 11,
fontFamily: 'Tahoma',
),
),
),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Color(0xffffc11c), // inner circle color
),
),
),
),

Shaping for circular cylinder UI

I'm having trouble with doing the UI for the Parcel shape. Tried using Stack but the background for the Text just gets in the way of the Icon. Below are my code and the image for the Parcel UI that I want to achieve.
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
#override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
#override
Widget build(BuildContext context) {
final _mediaQuery = MediaQuery.of(context);
final _height = _mediaQuery.size.height - _mediaQuery.padding.top;
final _width = _mediaQuery.size.width;
final _roundBorder = OutlineInputBorder(
borderRadius: BorderRadius.circular(15.0),
borderSide: BorderSide.none,
);
final _deliveryContainer = _width * 0.25;
return SafeArea(
child: Scaffold(
appBar: PreferredSize(
preferredSize: const Size.fromHeight(kToolbarHeight),
child: Container(
decoration: const BoxDecoration(
color: Constants.orangeColor,
),
child: Row(
children: [
Expanded(
flex: 3,
child: Padding(
padding: EdgeInsets.fromLTRB(
_width * 0.02,
_height * 0.01,
_width * 0.02,
_height * 0.01,
),
child: TextField(
decoration: InputDecoration(
prefixIcon: const Icon(
FeatherIcons.search,
color: Colors.black,
),
filled: true,
fillColor: Colors.white,
enabledBorder: _roundBorder,
errorBorder: _roundBorder,
disabledBorder: _roundBorder,
focusedBorder: _roundBorder,
focusedErrorBorder: _roundBorder,
),
),
),
),
Stack(
clipBehavior: Clip.none,
alignment: Alignment.center,
children: [
Container(
width: _deliveryContainer,
color: Color(0xFF05004E),
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 8.0,
vertical: 2.0,
),
child: Text(
'Parcel',
),
),
),
const Positioned(
left: 0.1,
child: Material(
shape: const CircleBorder(),
color: Color(0xFF05004E),
child: Icon(
Icons.toys,
color: Colors.white,
size: 34,
),
),
),
],
),
],
),
),
),
),
);
}
}
You can include decoration on _deliveryContainer.
Container(
width: _deliveryContainer,
decoration: BoxDecoration(
color: Color(0xFF05004E),
borderRadius: BorderRadius.only(
topLeft: Radius.circular(_height * 0.01),
bottomLeft: Radius.circular(_height * 0.01),
),
),
Try this (:
Row(
children: [
CircleAvatar(
radius: 25,
child: Icon(Icons.train),
backgroundColor: Colors.blue,
),
Container(
height: 30,
width: 100,
color: Colors.blue,
transform: Matrix4.translationValues(-10.0, 00.0, 0.0),
child: Center(
child: Text("Parcel"),
),
)
],
)

Flutter - How to get faded color border along a circle?

See how the edges are faded and merged with the color of the button? How do you get something like that in flutter?
I have the button, don't know how to get that effect.
#override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(top: 30),
child: Column(
children: [
//OUTER CIRCLE (BUTTON INSIDE)
Container(
height: 275,
width: 275,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(150),
border: Border.all(
width: 3,
color: Colors.grey.withOpacity(0.6),
style: BorderStyle.solid,
),
),
child: Padding(
padding: const EdgeInsets.all(40),
//SHADOW (Button inside)
child: Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
boxShadow: [
BoxShadow(
color: const Color(0xfffd194b).withOpacity(0.7),
spreadRadius: 10,
blurRadius: 40,
offset: const Offset(0, 25),
),
],
),
//BUTTON
child: RecordButtonWithText(
buttonText: _buttonText,
startStopRecording: startStopRecording,
),
),
),
),
],
),
);
class RecordButtonWithText extends StatelessWidget {
const RecordButtonWithText({
Key? key,
required String buttonText,
required this.startStopRecording,
}) : _buttonText = buttonText,
super(key: key);
final String _buttonText;
final VoidCallback startStopRecording;
#override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: () {
startStopRecording();
},
style: ElevatedButton.styleFrom(
elevation: 10,
padding: const EdgeInsets.all(30),
shape: const CircleBorder(),
shadowColor: const Color(0xfffd194b),
primary: const Color(0xfffd194b),
),
child: Text(
_buttonText.toUpperCase(),
style: const TextStyle(fontSize: 22, letterSpacing: 3),
),
);
}
}
If the you don't have to use the "ElevatedButton" widget, you can achieve what you want like that:
class RecordButtonWithText extends StatelessWidget {
final String _buttonText;
final VoidCallback startStopRecording;
final EdgeInsets padding;
final double size;
const RecordButtonWithText({
Key? key,
required String buttonText,
required this.startStopRecording,
this.padding = const EdgeInsets.all(30.0),
this.size = 275.0,
}) : _buttonText = buttonText,
super(key: key);
#override
Widget build(BuildContext context) {
return Container(
height: size,
width: size,
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(
width: 2,
color: Colors.grey.withOpacity(0.4),
style: BorderStyle.solid,
),
),
child: Padding(
padding: EdgeInsets.all(size * 0.145),
//SHADOW (Button inside)
child: Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
boxShadow: [
BoxShadow(
color: const Color(0xfffd194b).withOpacity(0.7),
spreadRadius: size * 0.03,
blurRadius: size * 0.145,
offset: Offset(0, size * 0.09),
),
],
),
//BUTTON
child: InkWell(
borderRadius: BorderRadius.circular(size / 2),
onTap: () {
startStopRecording();
},
child: Container(
alignment: Alignment.center,
padding: EdgeInsets.all(size * 0.1),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.black,
gradient: RadialGradient(colors: [
const Color(0xfffd194b),
const Color(0xfffd194b),
const Color(0xfffd194b).withOpacity(0.5),
], stops: const [
0,
0.8,
1
]),
),
child: Text(
_buttonText.toUpperCase(),
style: TextStyle(
color: Colors.white,
fontSize: size * 0.08,
),
),
),
),
),
),
);
}
}

Create circle around a letter in flutter

I was trying to create a circle around a letter here is my code, but it was not creating a perfect circle
Container(
padding: EdgeInsets.all(8),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(50),
border: Border.all(color: Colors.white, width: 2)),
child: InkWell(
child: Text(
"A",
textAlign: TextAlign.center,
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white,
fontSize: 15),
),
),
),
I've also tried with height and width of the Container then some portion of letter is hidden.
Container(
height: 30,
width: 30,
padding: EdgeInsets.all(8),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(50),
border: Border.all(color: Colors.white, width: 2)),
child: InkWell(
child: Text(
"A",
textAlign: TextAlign.center,
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white,
fontSize: 15),
),
),
)
But if I use Icon Widget instead of text widget it was creating perfect circle,
Container(
padding: EdgeInsets.all(8),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(50),
border: Border.all(color: Colors.white, width: 2)),
child: InkWell(
child: Icon(
Icons.add,
color: Colors.white,
),
),
),
Use this:
CircleAvatar(
backgroundColor: Colors.blue,
child: Text("A"),
);
You can also make use of the many other flags in CircleAvatar, like maxRadius or foregroundImage, like this:
CircleAvatar(
backgroundColor: Colors.blue,
child: Text("A"),
maxRadius: 30,
foregroundImage: NetworkImage("enterImageUrl"),
);
Looks like this:
For Containers, you can use the shape property to give it a circular shape.
It would be something like this :
Container(
decoration: BoxDecoration (
shape: BoxShape.circle,
),
child: ....
)
If I face multiple widget I prefer by creating a widget and wrapping others.
Container BoxShape.circle, depends on height
class WrapWithCircle extends StatelessWidget {
final Widget child;
late final EdgeInsets edgeInsets;
late final Color color;
WrapWithCircle({
Key? key,
required this.child,
this.edgeInsets = const EdgeInsets.all(8),
this.color = Colors.white,
}) : super(key: key);
#override
Widget build(BuildContext context) {
return Container(
alignment: Alignment.center,
padding: edgeInsets,
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(
width: 2,
color: color,
),
),
child: child,
);
}
}