Reputation: 8461
How can I create this bevelled shape in Flutter? It should support a child which can be centered and scale when the child size changes.
Upvotes: 3
Views: 1234
Reputation: 2246
Custom clipping path
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ClipPath(
clipper: Sky(),
child: Container(
width: 400,
height: 200,
color: Colors.red,
child: Center(
child: Text('80',
style: TextStyle(color: Colors.white, fontSize: 85.0)),
),
),
),
),
);
}
}
class Sky extends CustomClipper<Path> {
@override
getClip(Size size) {
double h = size.height;
double w = size.width;
double xOffset = 0.1;
Path path = Path()
..lineTo(w * xOffset, h)
..lineTo(w - w * xOffset, h)
..lineTo(w, h / 2)
..lineTo(w - w * xOffset, 0)
..lineTo(w * xOffset, 0.0)
..lineTo(0.0, h / 2)
..lineTo(w * xOffset, h);
return path;
}
@override
bool shouldReclip(CustomClipper oldClipper) {
return true;
}
}
Upvotes: 1
Reputation: 53327
You can achieve this by using ClipPath
with BeveledRectangleBorder
Example
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Custom Shape',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: CustomShapeDemo(),
);
}
}
class CustomShapeDemo extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Custom Shape Demo'),
),
body: Center(
child: ClipPath(
clipper: ShapeBorderClipper(
shape: BeveledRectangleBorder(
borderRadius: BorderRadius.circular(100.0)),
),
child: Container(
height: 200.0,
width: 400.0,
color: Colors.red,
child: Center(
child: Text('80',
style: TextStyle(color: Colors.white, fontSize: 85.0)),
),
),
),
));
}
}
Result
Upvotes: 4