Flutter: Container color overflowing border - flutter

I need to make a container with rounded borders, color, and an outline, but the background color is overflowing the outline color.
How can I fix this?
class MyWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
height: 21,
constraints: BoxConstraints(
minWidth: 21,
),
decoration: BoxDecoration(
border: Border.all(color: Colors.white, width: 2),
borderRadius: BorderRadius.circular(21),
color: Colors.pink,
),
child: Align(
child: Text(
"1",
style: TextStyle(
color: Colors.white, fontWeight: FontWeight.bold, fontSize: 12),
)));
}
}
Result: (most visible on the left side)

It... looks like a bug? I think you can report the issue to flutter github.
If you just want a workaround solution, you can try to move the background color (pink) to the lower level of your widget.
class MyWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
height: 21,
constraints: BoxConstraints(
minWidth: 21,
),
decoration: BoxDecoration(
border: Border.all(color: Colors.white, width: 2),
borderRadius: BorderRadius.circular(21),
),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(21),
color: Colors.pink,
),
child: Align(
child: Text(
"1",
style: TextStyle(
color: Colors.white, fontWeight: FontWeight.bold, fontSize: 12,
),
),
),
),
);
}
}

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

how to stack a container inside or just below another container in flutter

import 'package:flutter/material.dart';
void main() {
runApp(MainScreen());
}
class MainScreen extends StatelessWidget {
const MainScreen({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
double width = MediaQuery.of(context).size.width;
return Scaffold(
body: Column(
children: <Widget>[
Stack(
children: [
Container(
height: 250.0,
width: width,
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(150),
bottomRight: Radius.circular(150),
),
color: Colors.redAccent,
),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
"Hello World",
style: TextStyle(
fontSize: 22,
fontFamily: "Arial",
fontWeight: FontWeight.bold,
color: Colors.white),
),
Text(
"Hello World",
style: TextStyle(
fontSize: 22,
fontFamily: "Arial",
fontWeight: FontWeight.bold,
color: Colors.white),
),
],
),
),
),
],
),
Stack(
children: [
Positioned(
top: 40,
left: 20,
child: Container(
height: 500,
width: 200,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: Colors.grey,
),
),
),
],
),
],
),
);
}
}
I tried to add a new container based on same row but couldn't find how to stack with another container used positioned widget for the second container but didn't worked. after all made the first container same as second container using stack and positioned widget still didn't worked out
I tried to add a new container based on same row but couldn't find how to stack with another container used positioned widget for the second container but didn't worked. after all made the first container same as second container using stack and positioned widget still didn't worked out
I uploaded a picture which contains two container together but not stack just at the bottom. Thank You.
If you want to put two containers in stack, you dont need to create two stacks.
You just need to put two containers in a stack and wrap one of them with positioned.
Like this:
import 'package:flutter/material.dart';
void main() {
runApp(MainScreen());
}
class MainScreen extends StatelessWidget {
const MainScreen({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
double width = MediaQuery.of(context).size.width;
return Scaffold(
body: Stack(
children: [
Container(
height: 250.0,
width: width,
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(150),
bottomRight: Radius.circular(150),
),
color: Colors.redAccent,
),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
"Hello World",
style: TextStyle(
fontSize: 22,
fontFamily: "Arial",
fontWeight: FontWeight.bold,
color: Colors.white),
),
Text(
"Hello World",
style: TextStyle(
fontSize: 22,
fontFamily: "Arial",
fontWeight: FontWeight.bold,
color: Colors.white),
),
],
),
),
),
Positioned(
top: 40,
left: 20,
child: Container(
height: 500,
width: 200,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: Colors.grey,
),
),
),
],
),
);
}
}

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

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:

is there a way to add borders in a Text widget without wrapping it in a container widget

enter image description hereplease I need to delete the container widget and add border-radius to the text widget, or is it possible to make the text widget and container widget respond to changes together(meaning: when the text is null the container shouldn't display)
child: Container(
padding: EdgeInsets.fromLTRB(25.0, 0, 10.0, 0),
decoration: ShapeDecoration(
color: Colors.grey[850],
shape: RoundedRectangleBorder(
side: BorderSide(
width: 2.0,
style: BorderStyle.solid,
color: Colors.black,
),
borderRadius:
BorderRadius.all(Radius.circular(15.0)),
),
),
child: Text(
costOfSales,
style: TextStyle(
`enter code here` color: Colors.white,
fontSize: 25.0,
),
)),
Widget build(BuildContext context) {
var costOfSales = "\$99";
return costOfSales == null
? Container()
: Container(
padding: EdgeInsets.fromLTRB(25.0, 0, 10.0, 0),
decoration: ShapeDecoration(
color: Colors.grey[850],
shape: RoundedRectangleBorder(
side: BorderSide(
width: 2.0,
style: BorderStyle.solid,
color: Colors.black,
),
borderRadius: BorderRadius.all(Radius.circular(15.0)),
),
),
child: Text(
costOfSales,
style: TextStyle(
color: Colors.white,
fontSize: 25.0,
),
),
);
}