简体   繁体   English

如何在 flutter 中制作自定义按钮形状

[英]How to make a custom button shape in flutter

I am working on a project and i want to achieve a button我正在做一个项目,我想实现一个按钮像这样

How can i easily make this shape.我怎样才能轻松地做出这个形状。

You can use a CustomPainter .您可以使用CustomPainter Have a look at this great example on how to use this.看看这个关于如何使用它的好例子

Here is a small example of what you want to achieve (though I didn't do the rounded border but you can easily expand:这是你想要实现的一个小例子(虽然我没有做圆角边框但你可以轻松扩展:

import 'package:flutter/material.dart';

main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Drawing Paths',
      home: Scaffold(
        body: Center(
          child: GestureDetector(
            onTap: () => print('Do Something'),
            child: CustomPaint(
              size: Size(200, 50),
              painter: CurvePainter(),
            ),
          ),
        ),
      ),
    );
  }
}

class CurvePainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    var paint = Paint()
      ..color = Color(0xFFE32087)
      ..style = PaintingStyle.fill;

    var path = Path()
      ..moveTo(size.width*0.2, 0)
      ..lineTo(size.width, size.height*0.2)
      ..lineTo(size.width, size.height)
      ..lineTo(0, size.height);

    canvas.drawPath(path, paint);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }
}

The result :结果

按钮图片

Either use an image which represents this button and wrap it with GestureDetector (not recommended but sometimes the UI is so complex that making it purely in Flutter might be overkill, so keeping this option in mind is not bad) or play around with CustomClipper !要么使用代表此按钮的图像并用GestureDetector包裹它(不推荐,但有时 UI 非常复杂,以至于纯粹在 Flutter 中制作它可能有点矫枉过正,因此记住这个选项也不错)或使用CustomClipper

What you want to do is creating a Container or FlatButton and wrap it with ClipPath where you provide a CustomClipper as the clipper property of ClipPath .你想要做的是创建一个ContainerFlatButton并用ClipPath包装它,你提供一个CustomClipper作为ClipPathclipper属性。 In your implementation of CustomClipper you shape the widget just ike you want it to.在您的CustomClipper实现中,您可以按照自己的意愿塑造小部件。 Check out this great Medium post (not written by me, credits go to Kinjal Dhamat) where she explains various ways how to use CustomClipper and shape everything you want:查看这篇很棒的 Medium 帖子(不是我写的,go 归功于 Kinjal Dhamat),她在其中解释了如何使用CustomClipper和塑造你想要的一切的各种方式:

https://medium.com/flutter-community/flutter-custom-clipper-28c6d380fdd6 https://medium.com/flutter-community/flutter-custom-clipper-28c6d380fdd6

As suggested by pskink, creating a custom ShapeBorder class, which will be set as the shape property of FlatButton for example, has quite some benefits but need some additional knowledge, take a look at this post:正如 pskink 所建议的,创建一个自定义ShapeBorder class,例如,它将被设置为FlatButtonshape属性,有很多好处,但需要一些额外的知识,看看这篇文章:

Flutter - ClipPath Flutter - 剪辑路径

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM