How to get border for a clipped Container() in flutter - flutter

I'm using 'polygon_clipper 1.0.2' to clip my container.
Container(
height: 100,
width: 100,
child: ClipPolygon(
child: Container(
color: Theme.of(context).primaryColor,
),
sides: 6,
borderRadius: 10,
),
),
Here I get a Filled Hexagon, whose vertexes are curved.
I want a hexagon with just the border.
The following code gives a container having a rounded border.
I want similar result but sides should be 6.
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12),
border: Border.all(
width: 2, color: Theme.of(context).primaryColor
),
),
height: 100,
width: 100,
)
Any Solution ?

You can use PolygonBorder:
import 'package:polygon_clipper/polygon_border.dart';
Container(
height: 100,
width: 100,
decoration: ShapeDecoration(
shape: PolygonBorder(
sides: 6,
borderRadius: 10,
border: BorderSide(
color: Theme.of(context).primaryColor,
)
),
)
),

Related

Is there any possibility to have a rounded Container with multiple colors?

I have a Container in Flutter, which has 3 different Container with different colors as a child. The Main Container doesn't have rounded corners. Any ideas?
Container(
decoration: const BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20.0)),
),
height: 8,
width: 300,
child: Row(children: [
Container(
decoration: BoxDecoration(color: Colors.lightGreen),
width: 300 * 0.37,
),
Container(
decoration: BoxDecoration(color: Colors.grey[300]),
width: 300 * 0.15,
),
Expanded(
child: Container(
decoration: BoxDecoration(color: Colors.redAccent),
),
),
]),
),
Wrap your outer container with ClipRRect and give it a border radius:
ClipRRect(
borderRadius: BorderRadius.circular(20),
child: Container(
decoration: const BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20.0)),
),
height: 100,
width: 300,
child: Row(children: [
Container(
decoration: BoxDecoration(color: Colors.lightGreen),
width: 300 * 0.37,
),
Container(
decoration: BoxDecoration(color: Colors.grey[300]),
width: 300 * 0.15,
),
Expanded(
child: Container(
decoration: BoxDecoration(color: Colors.redAccent),
),
),
]),
),
),
You can give different border and color for each container like that
Container(child : Widget(),
decoration: BoxDecoration(
border: Border.all(
color: Colors.anyColor,
),
borderRadius: BorderRadius.all(Radius.circular(anyValue))
))

How can I make the outer circles

I am trying to make the display feature for my app, but I am having a hard time making these outer circles, any input is appreciated
There is a way to draw using coordinates.
You can set the ratio using MediaQuery.of(context).size.
return Scaffold(
body: SafeArea(
child: Stack(
children: <Widget>[
Positioned(
top: 200,
left: 80,
child: Container(
width: 200,
height: 200,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(128.0),
border: Border.all(color: Colors.blue),
),
),
),
Positioned(
top: 225,
left: 105,
child: Container(
width: 150,
height: 150,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(128.0),
border: Border.all(color: Colors.blue),
),
),
),
],
),
),
);
Use this:
Container(
color: Colors.white,
child: Center(
child: Container(
width: 200,
height: 200,
decoration: BoxDecoration(
gradient: const LinearGradient(
colors: [Colors.white,Colors.white, Colors.red, Colors.red, Colors.white,Colors.white],
transform: GradientRotation(pi / 2),
),
borderRadius: BorderRadius.circular(128.0),
),
child: Center(
child: Container(
width: 198,
height: 198,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(120.0),
),
),
),
),
),
),
It leads to this output:
use stack widget and put 2 circle containers with different size as children in stack widget

Custom border for a flutter widget

I have a custom border to draw around a widget. I have attached an image
Can anyone have an idea how to do this in flutter without a custom painter?
Please notice it doesn't have a bottom border.
Container(
height: 100,
width: 200,
alignment: Alignment.center,
decoration: const BoxDecoration(
border: Border(
top: BorderSide(width: 5.0, color: Colors.black),
left: BorderSide(width: 5.0, color: Colors.black),
right: BorderSide(width: 5.0, color: Colors.black),
bottom: BorderSide(width: 0, color: Colors.white),
),
color: Colors.white,
),
child: const Text('OK', textAlign: TextAlign.center, style: TextStyle(color: Color(0xFF000000))),
)
Wrap the widget with Container
Container(
decoration: BoxDecoration(
border: Border.all(width: 5.0, color: Colors.black),
borderRadius: BorderRadius.circular(20.0) )// change value as per your needs
child: //your widget
)
Try below code hope its help to you.
If you want only Top corner are rounded :
Container(
width: double.infinity,
height: 100,
decoration: BoxDecoration(
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(10),
topRight: Radius.circular(10),
),
border: Border.all(
width: 3,
color: Colors.black,
),
),
),
Result top rounded corner:
If you want all container rounded :
Container(
width: double.infinity,
height: 100,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
border: Border.all(
width: 5,
color: Colors.black,
),
),
),
All rounded contaner result:
Please Refer Below code:
Padding(
padding: const EdgeInsets.all(15.0),
child: ClipRRect(
borderRadius:
BorderRadius.all(Radius.circular(7)),
child: Container(
height: 100,
width: double.infinity,
decoration: BoxDecoration(
border: Border(
left: BorderSide(
color: Colors.black,
width: 4.0,
),
top: BorderSide(
color: Colors.black,
width: 4.0,
),
right: BorderSide(
color: Colors.black,
width: 4.0,
),
),
),
child: Center(
child: Text('Custom Border'),
),
),
),
)

How to create circle container with border in flutter?

As you can notice, the background color of the decoration is slightly overflowing the circular border. I've tried in different ways (e.g. using ClipOval) but the result is always the same.
Container(
height: 200,
width: 200,
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(width: 1, color: Colors.red)
),
),
I have just faced the same issue...
Easy workaround:
Container(
width: 28,
height: 28,
decoration: BoxDecoration(
color: Colors.green.withOpacity(0.25), // border color
shape: BoxShape.circle,
),
child: Padding(
padding: EdgeInsets.all(2), // border width
child: Container( // or ClipRRect if you need to clip the content
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.blue, // inner circle color
),
child: Container(), // inner content
),
),
),
Ref:
Container(
height:80,
width:80,
decoration:BoxDecoration(
color:Colors.red,
borderRadius:BorderRadius.circular(50),
border:Border.all(
color:Colors.white,
width:8
),
),
child:
Center(child:
Text("4",
style:TextStyle(
color:Colors.white,
fontWeight:FontWeight.bold,
fontSize:20
)))
Container with border

How to add border on a container inside a row widget in Flutter?

Container(
// decoration: BoxDecoration(
// border: Border.all(color: Colors.black45),
// borderRadius: BorderRadius.circular(8.0),
// ),
child: Row(
children: <Widget>[
Container(
child: Text("hi"),
margin : EdgeInsets.fromLTRB(20, 8, 8, 16),
width: MediaQuery.of(context).size.width *0.42,
height: 90,
color: Colors.black12,
),
Container(
child: Text("Hi"),
margin: EdgeInsets.fromLTRB(16, 8, 8, 16),
width: MediaQuery.of(context).size.width * 0.42 ,
height: 90,
color: Colors.black12,
)
],
),
),
I can add border using Box decoration on the outer container, but it's throwing me an error when I am trying to do the same on the inner containers. What is the problem and how to solve it?
In order to add border on container inside row widget , we have to use decoration for the inner containers.
Once you will post the error, we can answer you better but i think the below code will be helpful for you.
If you are using decoration then you must not add colour attribute in container directly, it should be in decoration only.
Container(
child: Row(
children: <Widget>[
Container(
child: Text("hi"),
margin: EdgeInsets.fromLTRB(20, 8, 8, 16),
width: MediaQuery.of(context).size.width * 0.42,
height: 90,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(4)),
shape: BoxShape.rectangle,
border: Border.all(
color: Colors.blue,
width: 4,
)),
),
Container(
child: Text("Hi"),
margin: EdgeInsets.fromLTRB(16, 8, 8, 16),
width: MediaQuery.of(context).size.width * 0.42,
height: 90,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(4)),
shape: BoxShape.rectangle,
border: Border.all(
color: Colors.blue,
width: 4,
)),
)
],
),
),
In container widgets you cannot use the color and decoration at the same time. Remove the color property from the Container and move it into the BoxDecoration widget
This should work:
Container(
child: Row(
children: <Widget>[
Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.black45),
borderRadius: BorderRadius.circular(8.0),
color: Colors.black12, //add it here
),
child: Text("hi"),
margin : EdgeInsets.fromLTRB(20, 8, 8, 16),
width: MediaQuery.of(context).size.width *0.42,
height: 90,
//color: Colors.black12, //must be removed
),
Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.black45),
borderRadius: BorderRadius.circular(8.0),
color: Colors.black12, //add it here
),
child: Text("Hi"),
margin: EdgeInsets.fromLTRB(16, 8, 8, 16),
width: MediaQuery.of(context).size.width * 0.42 ,
height: 90,
//color: Colors.black12, // must be removed
)
],
),
),
Consider the humble Divider widget to keep things simple. If you add it to the bottom of a Column in your row it will add a line that will act as a border.
const Divider(height: 1.0,),