How to create a hexagon clippath on flutter? - flutter

I'm trying to use a hexagon profile picture, but I'm having trouble using clip path with flutter.
The CSS code for the hexagon is this:
-webkit-clip-path: polygon(50% 0%, 100% 25%, 100% 75%, 50% 100%, 0% 75%, 0% 25%);
clip-path: polygon(50% 0%, 100% 25%, 100% 75%, 50% 100%, 0% 75%, 0% 25%);
how can I do it on flutter?

Let's use ClipPath to get this shape and follow the css the way you did.
50% 0% mean (x, y) and also same forlineTo(x,y) and moveTo.
class HexagonClipper extends CustomClipper<Path> {
#override
Path getClip(Size size) {
Path path = Path();
path
..moveTo(size.width / 2, 0) // moving to topCenter 1st, then draw the path
..lineTo(size.width, size.height * .25)
..lineTo(size.width, size.height * .75)
..lineTo(size.width * .5, size.height)
..lineTo(0, size.height * .75)
..lineTo(0, size.height * .25)
..close();
return path;
}
#override
bool shouldReclip(covariant CustomClipper<Path> oldClipper) {
return false;
}
}
And use like
ClipPath(
clipper: HexagonClipper(),
child: Container(
width: 100, /// controll the size and color
height: 110,
color: Colors.amber,
),
)
Result
learn more about ClipPath

code
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { #override Widget build(BuildContext context) { return MaterialApp( title: 'Hexagon', theme: ThemeData( primarySwatch: Colors.blue, ), home: MyHomePage(title: 'Hexagon'), ); } } class MyHomePage extends StatefulWidget { MyHomePage({Key key, this.title}) : super(key: key); final String title; #override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { #override Widget build(BuildContext context) { return Scaffold( body: Padding( padding: const EdgeInsets.all(50.0), child: ClipPath( child: Container( color: Colors.amber, ), clipper: _MyClipper(), ), ), ); } } class _MyClipper extends CustomClipper<Path> { #override Path getClip(Size size) { final path = Path(); path.lineTo(0, 0); path.lineTo(size.width, 0); path.lineTo(size.width, size.height * 0.8); path.lineTo(size.width * 0.8, size.height); path.lineTo(size.width * 0.2, size.height); path.lineTo(0, size.height * 0.8); path.lineTo(0, 0); path.close(); return path; } #override bool shouldReclip(CustomClipper<Path> oldClipper) => false; }
or you can use a package called
flutter_custom_clippers: ^2.0.0

Related

How to place content inside custompaint widget?

import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
#override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: CustomPaint(
painter: RPSCustomPainter(),
child: Container(
width: MediaQuery.of(context).size.width * 0.8,
height: MediaQuery.of(context).size.width * 0.73.toDouble(),
child: Text('Hi Nithya'))));
}
}
class RPSCustomPainter extends CustomPainter {
#override
void paint(Canvas canvas, Size size) {
Paint paint0 = Paint()
..color = const Color.fromARGB(255, 33, 150, 243)
..style = PaintingStyle.stroke
..strokeWidth = 1;
Path path0 = Path();
path0.moveTo(size.width * 0.3147368, size.height * 0.1385714);
path0.lineTo(size.width * 0.3145895, size.height * 0.4253429);
path0.quadraticBezierTo(size.width * 0.3334211, size.height * 0.4532571,
size.width * 0.3331895, size.height * 0.4633429);
path0.quadraticBezierTo(size.width * 0.3334526, size.height * 0.4708429,
size.width * 0.3147368, size.height * 0.4957143);
path0.lineTo(size.width * 0.3136842, size.height * 0.7828571);
path0.lineTo(size.width * 0.7378947, size.height * 0.7842857);
path0.lineTo(size.width * 0.7368421, size.height * 0.4985714);
path0.quadraticBezierTo(size.width * 0.7134000, size.height * 0.4737143,
size.width * 0.7131368, size.height * 0.4655000);
path0.quadraticBezierTo(size.width * 0.7134000, size.height * 0.4558571,
size.width * 0.7368421, size.height * 0.4271429);
path0.lineTo(size.width * 0.7357895, size.height * 0.1428571);
path0.lineTo(size.width * 0.3147368, size.height * 0.1385714);
path0.close();
canvas.drawPath(path0, paint0);
}
#override
bool shouldRepaint(covariant CustomPainter oldDelegate) {
return false;
}
}
I want the text to print inside the shape . But it is printing outside. How to increase or decrease the size of the shape based on content's width and height? I want the content to print only inside the custom shape. The shape should adjust to content's width and height automatically. How to achieve it?.
Wrapping your child Widget by Center Widget will solve the issue...
below code for your reference
CustomPaint(
painter: RPSCustomPainter(),
child: SizedBox(
width: MediaQuery.of(context).size.width * 0.8,
height: MediaQuery.of(context).size.width * 0.73.toDouble(),
child: const Center(child: Text('Hi Hari'))
),
);
have a good day!!!

Shaping Container using Bezier Curve in Flutter

Up to this point, I wasn't aware that we could make shapes of our choices in Flutter and hence I'm totally new to this. I would like to shape my container to resemble the below design but I am totally short of ideas as to how this can be done. I did a bit of reading to find out about the concepts of the Bezier Curve but upon trying to apply that knowledge to the container below, I ended up getting something pretty horrendous. The screenshots and the code are as follows:
This is what I ended up getting:
class ProfilePage extends StatefulWidget {
ProfilePageState createState() => ProfilePageState();
}
class ProfilePageState extends State<ProfilePage> {
#override
Widget build(BuildContext context) {
final height = MediaQuery.of(context).size.height;
final width = MediaQuery.of(context).size.width;
// TODO: implement build
return Scaffold(
body: Container(
height: height * 1,
width: width * 1,
color: Colors.red,
child: Column(
children: [
Stack(
children: [
ClipPath(
clipper: CurvedAppBar(),
child: Container(
height: height * 0.2,
width: double.infinity,
color: Colors.white,
),
),
Positioned(child: Image.asset('assets/images/logo-with-bg.png'))
],
)
],
),
),
);
}
}
import 'package:flutter/material.dart';
class CurvedAppBar extends CustomClipper<Path> {
#override
Path getClip(Size size) {
var path = Path();
path.lineTo(0, size.height);
var firstStart = Offset(size.width / 5, size.height);
var firstEnd = Offset(size.width / 2.25, size.height - 50);
path.quadraticBezierTo(
firstStart.dx, firstEnd.dy, firstEnd.dx, firstEnd.dy);
var secondStart =
Offset(size.width - (size.width / 3.24), size.height - 105);
var secondEnd = Offset(size.width, size.height - 10);
path.quadraticBezierTo(
secondStart.dx, secondStart.dy, secondEnd.dx, secondEnd.dy);
path.lineTo(size.width, 0);
path.close();
return path;
}
#override
bool shouldReclip(covariant CustomClipper<Path> oldClipper) {
// TODO: implement shouldReclip
return false;
}
}
Any ideas how this can be shaped?
Check this out:
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) {
return Scaffold(
backgroundColor: Colors.white,
body: SafeArea(
child: Center(
child: Container(
color: Colors.grey,
child: ClipPath(
clipper: Clipp(),
child: Container(
width: 500,
height: 500,
color: Colors.pink,
),
),
),
),
),
);
}
}
class Clipp extends CustomClipper<Path> {
#override
Path getClip(Size size) {
Path path = Path();
path.moveTo(150, 0);
path.quadraticBezierTo(120, 150, 0, 150);
path.lineTo(0, 380);
path.quadraticBezierTo(size.width / 4, size.height, size.width, 270);
path.lineTo(size.width, 0);
return path;
}
#override
bool shouldReclip(covariant CustomClipper<Path> oldClipper) => true;
}
The output:
Edit: To make it dynamic, try this:
class Clipp extends CustomClipper<Path> {
#override
Path getClip(Size size) {
Path path = Path();
double firstFactor = size.width / 3.3333;
double secondFactor = size.height / 2;
double thirdFactor = size.width / 4.166;
double fourthFactor = size.height / 1.4285;
path.moveTo(firstFactor, 0);
path.quadraticBezierTo(thirdFactor, firstFactor, 0, firstFactor);
path.lineTo(0, fourthFactor);
path.quadraticBezierTo(
size.width / 3,
size.height,
size.width,
secondFactor,
);
path.lineTo(size.width, 0);
return path;
}
#override
bool shouldReclip(covariant CustomClipper<Path> oldClipper) => true;
}

I want make this curve in the container

This image show what I want to do ,, I used container and made border radius to bottom right and this is ok , but in bottom left I don’t know how to make this curve
You can easily do it using CustomPainter.
Please check the following example.
import 'package:flutter/material.dart';
void main() async {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
home: new MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: SafeArea(
child: Container(
color: Colors.white,
child: CustomPaint(
painter: CurvePainterPage(),
),
),
),
);
}
}
class CurvePainterPage extends CustomPainter {
#override
void paint(Canvas canvas, Size size) {
var paint = Paint();
paint.color = Colors.blue[800];
paint.style = PaintingStyle.fill;
var path = Path();
path.moveTo(0, size.height * .70);
path.quadraticBezierTo(
0, size.height * .60, size.width * .1565, size.height * .60);
path.lineTo(size.width * .60, size.height * .60);
path.quadraticBezierTo(size.width * .69, size.height * .60,
size.width * .70, size.height * .50);
path.lineTo(size.width * .70, 0);
path.lineTo(0, 0);
canvas.drawPath(path, paint);
}
#override
bool shouldRepaint(CustomPainter oldDelegate) {
return true;
}
}
You can read about it more here
You can use BoxDecoration inside the Container.
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10.0),
color: Colors.white,),
child:Text(
'Greetings, planet!',
style: TextStyle(
fontSize: 20,
color: Colors.grey[300],
),))

How to clip the path to circular path of curved Navigation bar in flutter

I'm using Curved Navigation Bar in my flutter project. but is not exactly the same as I want. Can anyone clip it to exact circular like the picture attached?
Here is the clip path code.
class NavCustomPainter extends CustomPainter {
double loc;
double s;
Color color;
TextDirection textDirection;
NavCustomPainter(
double startingLoc, int itemsLength, this.color, this.textDirection) {
final span = 1.0 / itemsLength;
s = 0.2;
double l = startingLoc + (span - s) / 2;
loc = textDirection == TextDirection.rtl ? 0.8 - l : l;
}
#override
void paint(Canvas canvas, Size size) {
final paint = Paint()
..color = color
..style = PaintingStyle.fill;
final path = Path()
..moveTo(0, 0)
..lineTo((loc + 0.0) * size.width, 0)
..lineTo((loc + 0.015) * size.width, 0)
..cubicTo(
(loc + s * 0.05) * size.width,
size.height * 0.07,
loc * size.width,
size.height * 0.65,
(loc + s * 0.50) * size.width,
size.height * 0.65,
)
..cubicTo(
(loc + s + 0.01) * size.width,
size.height * 0.65,
(loc + s - s * 0.1) * size.width,
size.height * 0.07,
(loc + s - 0.01) * size.width,
0,
)
..lineTo(size.width, 0)
..lineTo(size.width, size.height)
..lineTo(0, size.height)
..close();
canvas.drawPath(path, paint);
}
what i have
What I want
Instead of doing your own painting, I suggest you use the shapes already available out of the box from the Flutter libraries.
Take a look at this example that takes advantage of the CircularNotchedRectangle shape:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Sample Code'),
),
body: Center(
child: Text('You have pressed the button $_counter times.'),
),
bottomNavigationBar: BottomAppBar(
shape: const CircularNotchedRectangle(),
child: Container(height: 50.0,),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment Counter',
child: Icon(Icons.add),
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
);
}
}
Which produces the following output:
You can customise the shape to achieve the output you want exactly. You can even use CircularNotchedRectangle.getOuterPath to get the Path that describes a rectangle with a smooth circular notch, and modify it however you want.

Draw Hexagon in Flutter

How can I draw same Hexagon as shown in the picture in Flutter?
You can use ClipPath
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Hexagon',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Hexagon'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Padding(
padding: const EdgeInsets.all(50.0),
child: ClipPath(
child: Container(
color: Colors.amber,
),
clipper: _MyClipper(),
),
),
);
}
}
class _MyClipper extends CustomClipper<Path> {
#override
Path getClip(Size size) {
final path = Path();
path.lineTo(0, 0);
path.lineTo(size.width, 0);
path.lineTo(size.width, size.height * 0.8);
path.lineTo(size.width * 0.8, size.height);
path.lineTo(size.width * 0.2, size.height);
path.lineTo(0, size.height * 0.8);
path.lineTo(0, 0);
path.close();
return path;
}
#override
bool shouldReclip(CustomClipper<Path> oldClipper) => false;
}