Ryan R
Ryan R

Reputation: 8461

Create pointed/bevelled widget in Flutter

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.

enter image description here

Upvotes: 3

Views: 1234

Answers (2)

januw a
januw a

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

enter image description here

Upvotes: 1

Shady Aziza
Shady Aziza

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

enter image description here

Upvotes: 4

Related Questions