flutter - 如何在不使用 border-radius 的情况下创建弯曲的小部件

标签 flutter dart

我是 Flutter 开发的初学者。我正在尝试使用 border-radius 组件创建一个弯曲的小部件,但我无法创建一个精确的模型屏幕。请指导我如何绘制弯曲的小部件。在这里我附上了我的模型样本。

提前致谢。

enter image description here

最佳答案

这是一个示例,您可以如何使用 CustomClipper

class ClippingApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: ClipPath(
        clipper: CurvedBottomClipper(),
        child: Container(
          color: Colors.lightGreen,
          height: 250.0,
          child: Center(
              child: Padding(
            padding: EdgeInsets.only(bottom: 50),
            child: Text(
              "Curved View",
              style: TextStyle(
                fontSize: 25,
                color: Colors.white,
              ),
            ),
          )),
        ),
      ),
    );
  }
}

class CurvedBottomClipper extends CustomClipper<Path> {
  @override
  Path getClip(Size size) {
    // I've taken approximate height of curved part of view
    // Change it if you have exact spec for it
    final roundingHeight = size.height * 3 / 5;

    // this is top part of path, rectangle without any rounding
    final filledRectangle =
        Rect.fromLTRB(0, 0, size.width, size.height - roundingHeight);

    // this is rectangle that will be used to draw arc
    // arc is drawn from center of this rectangle, so it's height has to be twice roundingHeight
    // also I made it to go 5 units out of screen on left and right, so curve will have some incline there
    final roundingRectangle = Rect.fromLTRB(
        -5, size.height - roundingHeight * 2, size.width + 5, size.height);

    final path = Path();
    path.addRect(filledRectangle);

    // so as I wrote before: arc is drawn from center of roundingRectangle
    // 2nd and 3rd arguments are angles from center to arc start and end points
    // 4th argument is set to true to move path to rectangle center, so we don't have to move it manually
    path.arcTo(roundingRectangle, pi, -pi, true);
    path.close();

    return path;
  }

  @override
  bool shouldReclip(CustomClipper<Path> oldClipper) {
    // returning fixed 'true' value here for simplicity, it's not the part of actual question, please read docs if you want to dig into it
    // basically that means that clipping will be redrawn on any changes
    return true;
  }
}

这就是您将使用此代码获得的结果:Curved rectangle screenshot

关于flutter - 如何在不使用 border-radius 的情况下创建弯曲的小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57197298/

相关文章:

sockets - 如何在 Dart 中通过套接字发送文件

android - Flutter - AppBar 上的填充

dart - Flutter:约束不约束小部件(需要解释)

android - Android Profiler工具可用于Android Studio中的flutter应用程序吗?

dart - 在dart、flutter中使用gcloud speech api进行实时语音识别

flutter - Flutter Firestore返回Future <List <Data >>返回空

android - 如何模拟/ stub Flutter 平台 channel /插件?

ios - 启动屏幕 Storyboard未显示我的图像

dart - 选择另一个按钮时取消选择一个按钮

android - Flutter android flavor 生成apk