Draw Hexagon in Flutter - 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;
}

Related

how to create Hexagon shape Container with flutter [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
how to create the same Container as below with 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: const MyHomePage(title: 'Hexagon'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required 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(
height: 200, //<- change height width
width: 180,
color: Colors.amber,
),
clipper: _MyClipper(),
),
),
);
}
}
class _MyClipper extends CustomClipper<Path> {
#override
Path getClip(Size size) {
final path = Path();
path.lineTo(0, size.height);
path.lineTo(0, size.height * 0.2);
path.lineTo(size.width * 0.2, 0);
path.lineTo(size.width * 0.8, 0);
path.lineTo(size.width, size.height * 0.2);
path.lineTo(size.width,size.height);
path.lineTo(0,size.height);
path.lineTo(0, size.height);
path.close();
return path;
}
#override
bool shouldReclip(CustomClipper<Path> oldClipper) => false;
}

How to create a hexagon clippath on 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

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

Exclude a shape from opacity widget flutter

How can I add an opacity widget in flutter and exclude a shape like a circle or a rectangle from it? (Something like image cropper overlay.)
Thanks to Thepeanut, I found an answer.
class HomePage extends StatefulWidget {
HomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
#override
Widget build(BuildContext context) {
return Stack(
children: <Widget>[
Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(),
),
ClipPath(
child: Opacity(
opacity: 0.5,
child: Container(color: Colors.red),
),
clipper: CustomClipPath(),
)
],
);
}
}
class CustomClipPath extends CustomClipper<Path> {
var radius = 96.0;
#override
Path getClip(Size size) {
Path path = Path();
path.lineTo(0.0, size.height);
path.lineTo(size.width, size.height);
path.lineTo(size.width, radius);
path.arcToPoint(
Offset(size.width - radius, 0.0),
clockwise: true,
radius: Radius.circular(radius),
);
return path;
}
#override
bool shouldReclip(CustomClipper<Path> oldClipper) => true;
}

in flutter, Need to use 'Login()' in my main.dart file. Check my dart code below:

Here's the snippet of little bit of the code
class Login extends StatefulWidget {
final double screenHeight, screenWidth;
const Login({Key key, this.screenHeight, this.screenWidth}) : super(key: key);
#override
_LoginState createState() => _LoginState();
}
class _LoginState extends State<Login> {
var _formkey = GlobalKey<FormState>();
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: PopBlue,
body: Stack(
children: <Widget>[
CustomPaint(
painter: MyCustomPainter(),
child: Container(
height: widget.screenHeight * 0.65,
),
),
...
Login class is in 1st line.
i have to use this Login class as home: Login() in my main.dart file.
i am not able to use this login class in my main code which is at main.dart file
help me finding a way to use this class in my main.dart file.
OK, I've played around a bit and come up with this which should help get you started, (tested and working on Chrome)
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) => MaterialApp(home: Login());
}
class Login extends StatefulWidget {
#override
_LoginState createState() => _LoginState();
}
class _LoginState extends State<Login> {
var _formkey = GlobalKey<FormState>();
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.blue, //PopBlue,
body: Stack(
children: <Widget>[
Container(
height: MediaQuery.of(context).size.height * 0.65, //widget.screenHeight * 0.65,
width: MediaQuery.of(context).size.width * 0.65,
color: Colors.red,
),
CustomPaint(
size: Size(300, 300),
painter: MyCustomPainter(),
// child: Container(
// height: MediaQuery.of(context).size.height * 0.65, //widget.screenHeight * 0.65,
// width: MediaQuery.of(context).size.width * 0.65,
// color: Colors.red, // ), ),
],
),
);
}
}
class MyCustomPainter extends CustomPainter {
#override
void paint(Canvas canvas, Size size) {
final left = 50.0;
final top = 100.0;
final right = 250.0;
final bottom = 200.0;
final rect = Rect.fromLTRB(left, top, right, bottom);
final paint = Paint()
..color = Colors.black
..style = PaintingStyle.stroke
..strokeWidth = 4;
canvas.drawRect(rect, paint);
}
#override
bool shouldRepaint(CustomPainter old) => false;
}
Your main.dart will be something like below
import 'package:flutter/material.dart';
import 'path-of-your-login-page';//import './login.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Login();
);
}
}
What do you mean by "i am not able to use this login class in my main code"?
Is the IDE reporting a problem? If so, what does it say?
Or is it failing at run time? If so, what is the error message?