How to code rectangle+ellipse shape in Flutter - flutter

I'm trying to achieve shape in the shown image. I can't find out how to do this. Last thing I tried was:
Container(
height: 175,
decoration: BoxDecoration(
borderRadius: BorderRadius.vertical(
bottom: Radius.elliptical(175, 45),
),
),
)
How can I create this shape?

You can use custom painter:
class MyWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
child: Container(
color: Colors.transparent,
height: 155,
width: 375,
child: CustomPaint(
painter: CurvePainter(),
),
),
);
}
}
class CurvePainter extends CustomPainter {
#override
void paint(Canvas canvas, Size size) {
var paint = Paint();
paint.color = Color(0XFF382b47);
paint.style = PaintingStyle.fill;
var path = Path();
path.moveTo(0, size.height * 0.26);
path.quadraticBezierTo(
size.width / 2, size.height, size.width, size.height * 0.26);
path.lineTo(size.width, 0);
path.lineTo(0, 0);
canvas.drawPath(path, paint);
}
#override
bool shouldRepaint(CustomPainter oldDelegate) {
return true;
}
}

View Image
If you want to add ovel shape on top borders:
class MyWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Transform(
alignment: Alignment.center,
transform: Matrix4.rotationX(math.pi),
child: Container(
color: Colors.red,
height: 120,
width: double.infinity,
child: CustomPaint(
painter: CurvePainter(),
),
),
),;
}
}
class CurvePainter extends CustomPainter {
#override
void paint(Canvas canvas, Size size) {
var paint = Paint();
paint.color = Color(0XFF382b47);
paint.style = PaintingStyle.fill;
var path = Path();
path.moveTo(0, size.height * 0.26);
path.quadraticBezierTo(
size.width / 2, size.height, size.width, size.height * 0.26);
path.lineTo(size.width, 0);
path.lineTo(0, 0);
canvas.drawPath(path, paint);
}
#override
bool shouldRepaint(CustomPainter oldDelegate) {
return true;
}
}

Related

how to create cove of top of Container and add image inside of CustomPainter in flutter app

how to add image inside of CustomPainter and manage.
like this:
import 'package:flutter/material.dart';
import 'package:hallo/appbar.dart';
class product1 extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body:Stack(
children: [
Column(
children: [
CustomPaint(
size: Size.fromHeight(500),
painter: RPSCustomPainter(),
),
Container(
)
],
)
],
)
);
}
}
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.0;
Path path0 = Path();
path0.moveTo(0,0);
path0.lineTo(0,size.height);
path0.quadraticBezierTo(size.width*0.1120000,size.height*0.7968571,size.width*0.3375000,size.height*0.8585714);
path0.cubicTo(size.width*0.5795625,size.height*0.9350714,size.width*0.8043750,size.height*1.0775000,size.width,size.height);
path0.quadraticBezierTo(size.width,size.height*0.7500000,size.width,0);
path0.lineTo(size.width*0.1252500,0);
path0.lineTo(0,0);
path0.close();
canvas.drawPath(path0, paint0);
}
#override
bool shouldRepaint(covariant CustomPainter oldDelegate) {
return true;
}
}
try this:
class HomeScreen extends StatelessWidget {
const HomeScreen({
super.key,
});
static const String route = "/homeScreen";
#override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: [
Container(
height: MediaQuery.of(context).size.height * 0.5,
child: Image.network(
"https://images.pexels.com/photos/14446269/pexels-photo-14446269.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1",
fit: BoxFit.cover,
width: double.infinity,
),
),
CustomPaint(
size: MediaQuery.of(context).size,
painter: RPSCustomPainter(),
),
],
),
);
}
}
class RPSCustomPainter extends CustomPainter {
#override
void paint(Canvas canvas, Size size) {
Paint paint0 = Paint()
..color = const Color.fromARGB(255, 33, 150, 243)
..style = PaintingStyle.fill
..strokeWidth = 5.0;
Path path1 = Path();
path1.moveTo(0, size.height);
path1.lineTo(0, size.height * 0.5);
path1.quadraticBezierTo(
size.width * 0.1120000,
size.height * 0.5 * 0.7968571,
size.width * 0.3375000,
size.height * 0.5 * 0.8585714);
path1.cubicTo(
size.width * 0.5795625,
size.height * 0.5 * 0.9350714,
size.width * 0.8043750,
size.height * 0.5 * 1.0775000,
size.width,
size.height * 0.5,
);
path1.lineTo(size.width, size.height);
path1.lineTo(0, size.height);
path1.close();
canvas.drawPath(path1, paint0);
}
#override
bool shouldRepaint(covariant CustomPainter oldDelegate) {
return true;
}
}
you will get this type of results:

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

How to create a Sine Wave Clipper in Flutter?

I want to create a Custom Clipper like the image below.
Finally figured it out.
Clip Widget
ClipPath(
child: Container(
height: 8,
width: double.infinity,
color: Colors.white,
),
clipper: WaveClipper(),
),
Wave Clipper
class WaveClipper extends CustomClipper<Path> {
#override
Path getClip(Size size) {
var path = Path();
path.lineTo(0, size.height);
var x = 0.0;
var numberOfWaves = 30;
var increment = size.width / numberOfWaves;
bool startFromTop = false;
while (x < size.width) {
if (startFromTop) {
path.lineTo(x, 0);
path.cubicTo(x + increment / 2, 0, x + increment / 2, size.height,
x + increment, size.height);
} else {
path.lineTo(x, size.height);
path.cubicTo(x + increment / 2, size.height, x + increment / 2, 0,
x + increment, 0);
}
x += increment;
startFromTop = !startFromTop;
}
path.lineTo(size.width, 0);
path.lineTo(0, 0);
path.close();
return path;
}
#override
bool shouldReclip(CustomClipper<Path> oldClipper) => false;
}
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: MyHomePage(title: 'Flutter'),
);
}
}
class MyHomePage extends StatefulWidget {
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(
appBar: AppBar(
title: Text(
widget.title,
style: TextStyle(color: Colors.white),
),
),
body: Padding(
padding: const EdgeInsets.all(30.0),
child: ClipPath(
child: Container(
width: MediaQuery.of(context).size.width,
height: 200,
color: Colors.grey,
),
clipper: CustomClipPath(),
),
),
);
}
}
class CustomClipPath extends CustomClipper<Path> {
Path getClip(Size size) {
var path = new Path();
path.lineTo(0.0, 40.0);
path.lineTo(0.0, size.height);
path.lineTo(size.width, size.height);
path.lineTo(size.width, 40.0);
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
path.quadraticBezierTo(
size.width - (size.width / 16) - (i * size.width / 8),
0.0,
size.width - ((i + 1) * size.width / 8),
size.height - 160);
} else {
path.quadraticBezierTo(
size.width - (size.width / 16) - (i * size.width / 8),
size.height - 120,
size.width - ((i + 1) * size.width / 8),
size.height - 160);
}
}
path.lineTo(0.0, 40.0);
path.close();
return path;
}
#override
bool shouldReclip(CustomClipper<Path> oldClipper) => false;
}

How to fit any curved to screen? - Flutter

I am new to flutter and would like to learn how to fit any screen curved from photo.
I'am not able to fit curved to the left, how to fit this shape to every possible screen?
I don't know why but the left side won't fit
I needs help or hints.
class StartAfterRegister extends StatelessWidget {
static Route route() {
return MaterialPageRoute<void>(builder: (_) => StartAfterRegister());
}
#override
Widget build(BuildContext context) {
return Scaffold(body: _profilePage(context));
}
}
Widget _profilePage(BuildContext context) {
return SafeArea(
child: Center(
child: Column(
children: [
const SizedBox(height: 452),
_curved(),
],
),
),
);
// });
}
Widget _curved() {
return Container(
// padding: const EdgeInsets.all(8.0),
// child: Padding(
// margin: const EdgeInsets.only(left: 0.0, right: 28.0),
// const EdgeInsets.fromLTRB(0, 10, 25, 10),
child: CustomPaint(
size: Size(600, (320* 1.1617600000000001).toDouble()),
//You can Replace [WIDTH] with your desired width for
// Custom Paint and height will be calculated automatically
painter: RPSCustomPainter(),
),
);
}
Code was generated with this page :https://fluttershapemaker.com/
I don't know where I made a mistake, wrong portions?
import 'dart:ui' as ui;
import 'package:flutter/cupertino.dart';
import 'package:flutter_login/components/theme/colors.dart';
class RPSCustomPainter extends CustomPainter {
#override
void paint(Canvas canvas, Size size) {
Path path_0 = Path();
path_0.moveTo(size.width*1.071853,size.height*1.035738);
path_0.lineTo(size.width*0.07250032,size.height*1.035738);
path_0.lineTo(size.width*0.07250032,size.height*0.3868996);
path_0.cubicTo(size.width*0.07241850,size.height*0.3325917,size.width*0.09748933,size.height*0.2804976,size.width*0.1421620,size.height*0.2421464);
path_0.cubicTo(size.width*0.1969324,size.height*0.1950013,size.width*0.2692701,size.height*0.1829006,size.width*0.3079311,size.height*0.1816967);
path_0.cubicTo(size.width*0.5894098,size.height*0.1729377,size.width*0.8344350,size.height*0.1919213,size.width*0.9302565,size.height*0.1649115);
path_0.cubicTo(size.width*0.9696772,size.height*0.1538054,size.width*1.040485,size.height*0.1192928,size.width*1.071848,size.height*0.03573744);
path_0.cubicTo(size.width*1.069587,size.height*0.2428419,size.width*1.074102,size.height*0.8286341,size.width*1.071853,size.height*1.035738);
path_0.close();
Paint paint_0_fill = Paint()..style=PaintingStyle.fill;
paint_0_fill.color = teal ;
canvas.drawPath(path_0,paint_0_fill);
}
#override
bool shouldRepaint(covariant CustomPainter oldDelegate) {
return true;
}
**There is a problem in your custom shape if you want to create more shape be sure you set in widht and size **
like this
simply add this code to custom painter
class RPSCustomPainter extends CustomPainter {
#override
void paint(Canvas canvas, Size size) {
Paint paint_0 = new Paint()
..color = Color.fromARGB(255, 33, 150, 243)
..style = PaintingStyle.fill
..strokeWidth = 1;
Path path_0 = Path();
path_0.moveTo(size.width, size.height);
path_0.lineTo(size.width * 0.0012500, size.height * 0.9942857);
path_0.lineTo(size.width * 0.0012500, size.height * 0.4271429);
path_0.quadraticBezierTo(size.width * 0.0740625, size.height * 0.2807143,
size.width * 0.1950000, size.height * 0.2800000);
path_0.quadraticBezierTo(size.width * 0.8515625, size.height * 0.3560714,
size.width * 0.9962500, size.height * 0.2828571);
path_0.quadraticBezierTo(size.width * 0.9990625, size.height * 0.2850000,
size.width, size.height * 0.2857143);
path_0.quadraticBezierTo(size.width * 1.0084375, size.height * 0.4992857,
size.width, size.height);
path_0.close();
canvas.drawPath(path_0, paint_0);
}
#override
bool shouldRepaint(covariant CustomPainter oldDelegate) {
return true;
}
}
**add this in your child **
import 'package:flutter/material.dart';
class Demo extends StatefulWidget {
const Demo({Key? key}) : super(key: key);
#override
_DemoState createState() => _DemoState();
}
class _DemoState extends State<Demo> {
#override
Widget build(BuildContext context) {
/// this is the width of screen
var width = MediaQuery.of(context).size.width;
return Scaffold(
appBar: AppBar(),
body: Stack(
children: [
Align(
alignment: Alignment.bottomLeft,
child: Container(
color: Colors.red,
width: width,
child: CustomPaint(
size: Size((width).toDouble(), (width * 0.875).toDouble()),
painter: RPSCustomPainter(),
),
))
],
),
);
}
}

I'm trying to draw a custom shape in flutter, but unfortunately the shape doesn't appear, I just see the white container

this is the custom shape code
class SignInPageCustomShape extends CustomPainter {
#override
void paint(Canvas canvas, Size size) {
final paint = Paint();
final Color myColor = Colors.black12;
paint.color = myColor;
paint.style = PaintingStyle.fill;
final path = Path();
path.moveTo(0, size.height * 0.9167);
path.quadraticBezierTo(
size.width * 0.25,
size.height * 0.875,
size.width * 0.5,
size.height * 0.9167,
);
path.quadraticBezierTo(
size.width * 0.75,
size.height * 0.9584,
size.width * 1.0,
size.height * 0.9167,
);
path.lineTo(size.width, size.height);
path.lineTo(0, size.height);
canvas.drawPath(path, paint);
}
#override
bool shouldRepaint(CustomPainter oldDelegate) {
return true;
}
}
and this is my widget tree,I don't know what is wrong, I tried to put my container in different places, but the same result is gotten, stack overflow is asking me to explain more, but I haven't anything else to explain .
class Paint extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Container(
color: Colors.white,
child: CustomPaint(
painter: SignInPageCustomShape(),
),
),
),
);
}
}
The issue that you are facing might be because you haven't defined a child in CustomPaint
I added an empty Container and run the code and got this results
Screenshot
Naming your StatelessWidget Paint is not recommended also
class Paint extends StatelessWidget {
The following method accepts two parameters one of type Paint because you defined a custom widget with the same name it was passing the wrong parameter in my case
canvas.drawPath(path, paint);
Code I used to get the results attached in this response:
Paint Widget:
class PaintWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Container(
color: Colors.white,
child: CustomPaint(
painter: SignInPageCustomShape(),
child: Container(),
),
),
),
);
}
}
SignInPageCustomShape Widget:
class SignInPageCustomShape extends CustomPainter {
#override
void paint(Canvas canvas, Size size) {
final paint = Paint();
final Color myColor = Colors.black12;
paint.color = myColor;
paint.style = PaintingStyle.fill;
final path = Path();
path.moveTo(0, size.height * 0.9167);
path.quadraticBezierTo(
size.width * 0.25,
size.height * 0.875,
size.width * 0.5,
size.height * 0.9167,
);
path.quadraticBezierTo(
size.width * 0.75,
size.height * 0.9584,
size.width * 1.0,
size.height * 0.9167,
);
path.lineTo(size.width, size.height);
path.lineTo(0, size.height);
canvas.drawPath(path, paint);
}
#override
bool shouldRepaint(CustomPainter oldDelegate) {
return true;
}
}