Create circle around a letter in flutter - 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,
);
}
}

Related

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
),
),
),
),

Align bottom center a text inside a Container of stack

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),
),
),),
]));
}
}

Custom card or container with border, shape and three parts

How can i create following flutter widget.
I want to use it in a column to navigate to the next page.
I've already tried a card or a container consisting of a row with three expandables. But either the red area with the icon overlaps the container with the rounded edges, or it is smaller than the container. What can I do or maybe someone has a code example?
You could create your own custom widget.
Here's a code sample you could use:
class MyWidget extends StatelessWidget {
final double height;
final Function onTap;
MyWidget({this.height = 40.0, this.onTap});
#override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () => onTap(),
child: Container(
decoration: BoxDecoration(
color: Colors.red,
border: Border.all(color: Colors.red),
borderRadius: BorderRadius.circular(10),
),
child: Row(
children: [
Container(
padding: const EdgeInsets.all(8),
child: Icon(Icons.inbox, color: Colors.white),
),
Expanded(
child: Container(
padding: const EdgeInsets.only(left: 8),
alignment: Alignment.centerLeft,
height: height,
decoration: BoxDecoration(color: Colors.white),
child: Text(
'ARCHIV',
style: TextStyle(
color: Colors.red,
fontWeight: FontWeight.bold,
),
),
),
),
Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius:
BorderRadius.horizontal(right: Radius.circular(10)),
),
height: height,
child: Icon(Icons.arrow_forward_ios, color: Colors.red),
),
],
),
),
);
}
}
Try full code on DartPad
Result:

How to build a proper card in flutter?

I am developing a flutter application where I want to use a card but I am not getting the desired result.
I want the this with an image to left side also:
But what I am getting is:
Following is my code:
class CommunityCard extends StatelessWidget {
#override
Widget build(BuildContext context) {
// TODO: implement build
return Card(
child: Container(
decoration: BoxDecoration(
border: Border.all(
color: Colors.black,
),
borderRadius: BorderRadius.all(Radius.circular(20))),
child: Wrap(
direction: Axis.horizontal,
children: [
SizedBox(
width: 5,
),
Container(
padding: EdgeInsets.symmetric(horizontal: 10, vertical: 10),
decoration: BoxDecoration(
border: Border.all(
color: Colors.black,
),
borderRadius: BorderRadius.all(Radius.circular(20))),
child: Icon(
Icons.sports_cricket,
size: 15,
color: Colors.blue,
),
),
Text(
'Sports',
style: TextStyle(fontSize: 15),
),
Icon(
Icons.cancel_outlined,
size: 15,
)
],
),
),
);
}
}
Can someone help me how to do this please?
class CommunityCard extends StatelessWidget {
#override
Widget build(BuildContext context) {
// TODO: implement build
return Card(
child: Container(
decoration: BoxDecoration(
border: Border.all(
color: Colors.black,
),
borderRadius: BorderRadius.all(Radius.circular(20))),
child: Padding(
EdgeInsets.symmetric(horizontal:10.0,vertical:8.0), //Keep experimenting with values till you get the correct one
child: Wrap(
direction: Axis.horizontal,
crossAxisAlignment: WrapCrossAlignment.center,//The change
children: [
SizedBox(
width: 5,
),
Container(
padding: EdgeInsets.symmetric(horizontal: 10, vertical: 10),
decoration: BoxDecoration(
border: Border.all(
color: Colors.black,
),
borderRadius: BorderRadius.all(Radius.circular(20))),
child: Icon(
Icons.sports_cricket,
size: 12,
color: Colors.blue,
),
),
Text(
'Sports',
style: TextStyle(fontSize: 15),
),
Icon(
Icons.cancel_outlined,
size: 15,
)
],
),
),
),
);
}
}
I have added crossAxisAlignment: WrapCrossAlignment.center, which makes it align to the center on the other axis
Also I have added some padding for aesthetics
write crossAxisAlignment: WrapCrossAlignment.center, in wrap widget property
Use ListTile in ClipRRect for the curve and proper alignment.
ClipRRect(
borderRadius: BorderRadius.circular(25.0),
child:Container(
color:Colors.green,
child:ListTile(
leading:Icon(Icons.sports_cricket,),
title:Text('Sports'),
trailing:Icon(Icons.close)
),
)
),
You just need a chip Here's an example:-
Chip(
onDeleted: () => (redirection call here),
deleteIcon: Icon(
Icons.sports_cricket,
size: rowIconSize,
color: companyThemeData.primaryColor,
),
labelStyle:
normal12blackish.copyWith(color: companyThemeData.primaryColor),
backgroundColor: companyThemeData.primaryColorLight,
label: Text('Sports'),
);

Add border to a Container with borderRadius in Flutter

Container(
child: Text(
'This is a Container',
textScaleFactor: 2,
style: TextStyle(color: Colors.black),
),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: Colors.white,
border: Border(
left: BorderSide(
color: Colors.green,
width: 3,
),
),
),
height: 50,
),
This is supposed to show a rounded-edged container with a green left border 3px wide, and the child Text "This is a Container". However, it just shows a rounded-edged container with an invisible child and an invisible left border.
When I take out the borderRadius object, the child Text and the green left border is visible, but introducing it hides the left border and child Text again.
The major problem seems to be the custom left border, because using border: Border.all(width: 0) and borderRadius: BorderRadius.circular(10) makes the edges rounded as needed and also shows the child. But now I cant apply the green left border which is quite important in this particular setup.
So is there something I'm doing wrong, or is this a bug in flutter, or is it just something that is not allowed?
Thank you in advance.
Edit: The image below is the end result. The colors don't matter
It's not possible to add border: and borderRadius: at the same time, you'll get this error:
A borderRadius can only be given for uniform borders.
You can achieve what you want using the borderRadius: and a boxShadow: instead of border: like this:
boxShadow: [
BoxShadow(color: Colors.green, spreadRadius: 3)
]
Your sample code would be like this:
Container(
child: Text(
'This is a Container',
textScaleFactor: 2,
style: TextStyle(color: Colors.black),
),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: Colors.white,
boxShadow: [
BoxShadow(color: Colors.green, spreadRadius: 3),
],
),
height: 50,
),
Edit: To achieve the example you now provided, you could do this:
Container(
padding: EdgeInsets.only(left: 12.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10.0),
color: Colors.green,
),
height: 50,
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
topRight: Radius.circular(10.0),
bottomRight: Radius.circular(10.0)),
color: Colors.white,
),
child: Text(
'This is a Container',
textScaleFactor: 2,
style: TextStyle(color: Colors.black),
),
),
),
Another solution:
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10.0),
color: Colors.white,
),
height: 50,
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
width: 12.0,
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(10.0),
bottomLeft: Radius.circular(10.0)),
color: Colors.green,
),
),
Text(
'This is a Container',
textScaleFactor: 2,
style: TextStyle(color: Colors.black),
)
],
),
),
There is an answer here
Container(
decoration: BoxDecoration(
border: Border.all(
color: Color(0xFFF05A22),
style: BorderStyle.solid,
width: 1.0,
),
color: Colors.transparent,
borderRadius: BorderRadius.circular(30.0),
),
),
This might be so late but also it might be useful for others.
You can wrap your Container inside a ClipRRect, give the ClipRRect the radius and give the Container the border!
Example:
ClipRRect(
borderRadius: const BorderRadius.all(Radius.circular(16.0)),
child: Container(
height: 100,
width: double.maxFinite,
padding: const EdgeInsets.all(16.0),
decoration: BoxDecoration(
border: Border(
left: BorderSide(width: 8.0, color: Colors.green),
),
),
),
),
This should do the UI you lastly posted.
If you want to achieve borderRadius you could also use a PhysicalModel, but you'll have to provide colour. You also can have a shadow for your widget.
PhysicalModel(
color: Colors.red,
elevation: 5,
shadowColor: Colors.blue,
borderRadius: BorderRadius.circular(20),
child: SizedBox(width: 75, height: 75),
)
I think an easier way inspired by #pablo 's answer would be to just make a boxShadow with and offset but without any blur.
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.vertical(
top: Radius.circular(10),
),
boxShadow: [
// to make elevation
BoxShadow(
color: Colors.black45,
offset: Offset(2, 2),
blurRadius: 4,
),
// to make the coloured border
BoxShadow(
color: Colors.blue,
offset: Offset(0, 4),
),
],
),
The decoration above will give us an elevated box which has a blue border in the bottom.
Another benefit of this approcah is that you can use it with
borderRadius: BorderRadius.circular(num)
Which means you can have a container with all rounded sides + a colored border.
Please note that the coloured border comes under the original shadow. This is done to prevent the elevation color from darkening the border.
I archieve do a Inkwell (that simulate a button) circular with an icon inside :
InkWell(
onTap: (){},
child: Container(
width: 50,
height: 50,
decoration: ShapeDecoration(
shape: CircleBorder(), //here we set the circular figure
color: Colors.red
),
child: Center(
child: Icon(
Icons.email,
size: 30,
color: Colors.white,
)
),
)
)
link example of result: https://images.vexels.com/media/users/3/140138/isolated/preview/88e50689fa3280c748d000aaf0bad480-icono-de-ronda-de-correo-electronico-1.png
////Hope this will work for you,Thanks
import 'package:flutter/material.dart';
class System extends StatefulWidget {
const System({Key? key}) : super(key: key);
#override
_SystemState createState() => _SystemState();
}
class _SystemState extends State<System> {
#override
Widget build(BuildContext context) {
return Scaffold(
body:Padding(
padding: const EdgeInsets.only(top:80.0),
child: Column(
children: [
ClipRRect(
borderRadius: const BorderRadius.all(Radius.circular(16.0)),
child: Padding(
padding: const EdgeInsets.all(15.0),
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Container(
height: 100,
width: double.maxFinite,
padding: const EdgeInsets.all(16.0),
decoration: const BoxDecoration(
color: Color(0xffF6EBEC),
border: Border(
bottom: BorderSide(width: 8.0, color: Color(0xffA24949)),
),
),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
const CircleAvatar(
backgroundColor: Color(0xffA24949),
child: Icon(Icons.close,
color: Color(0xffF6EBEC), size: 40),
),
const SizedBox(width: 10),
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: const [
Text("Invalid",
style: TextStyle(color: Color(0xffA24949), fontSize: 18)),
SizedBox(
width: 243,
child: Text("Details do not match the issuer records",overflow: TextOverflow.ellipsis,maxLines: 1,))
])
],
),
),
),
),
),
SizedBox(height: 20,),
ClipRRect(
borderRadius: const BorderRadius.all(Radius.circular(16.0)),
child: Padding(
padding: const EdgeInsets.all(15.0),
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Container(
height: 100,
width: double.maxFinite,
padding: const EdgeInsets.all(16.0),
decoration: const BoxDecoration(
color: Color(0xFFE9F6EB),
border: Border(
bottom: BorderSide(width: 8.0, color: Color(0xFF69A258),),
),
),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
const CircleAvatar(
backgroundColor: Color(0xFF69A258),
child: Icon(Icons.check_rounded,
color: Color(0xFFE9F6EB), size: 40),
),
const SizedBox(width: 15),
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: const [
Text("Valid",
style: TextStyle(color: Color(0xFF69A258), fontSize: 18)),
Text("Document successfully validated.")
])
]),
),
),
),
),
],
),
),
);
}
}