【问题标题】:Clip a Container in Flutter在 Flutter 中裁剪容器
【发布时间】:2020-01-11 09:00:57
【问题描述】:

我想剪辑一个容器,如附图所示。 Image

谢谢。

【问题讨论】:

  • 使用 ClipRect 并进行计算! api.flutter.dev/flutter/widgets/ClipRect-class.html
  • 会是底栏吗?也许你需要api.flutter.dev/flutter/painting/…
  • @KirillMatrosov Yaa 有点但不完全是因为不想使用 BottomAppBar,我知道它有 shape 参数。我想做一个自定义小部件。
  • @diegoveloper 我试着做数学但没能得到确切的形状。
  • 我同意其他 cmets。首先创建 CustomPainter (您可以复制粘贴),然后从 1 个角开始...和线到 ..line 到..line 到.. 直到到达第一个点。

标签: flutter dart clipping


【解决方案1】:

您可以使用CustomPaint 绘制round rectangleClipRect 以仅将其一半渲染为“缺口”。

ClipRect(
  child: Container(
    color: Colors.lightBlueAccent,
    height: 50,
    width: 200,
    child: CustomPaint(
      painter: CustomPaintNotch(),
    ),
  ),
),

...

class CustomPaintNotch extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    var paint = Paint()
      ..color = Colors.green;

    Offset center = Offset(size.width / 2, 0);
    // As a notch, RRect will be drawn with offset
    // With ClipRect used, paint outside the area won't be rendered
    RRect roundRectangle = RRect.fromRectAndRadius(
        Rect.fromCenter(center: center, width: 100.0, height: 40.0),
        Radius.circular(20.0));
    canvas.drawRRect(roundRectangle, paint);
  }

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

CustomPaintNotch 在 Container 小部件的顶部中心部分的边缘绘制一个圆形矩形。由于使用了 ClipRect,因此不会渲染超出该区域的绘画。这将是矩形的缺口。

这是小部件在渲染时的样子。

完整示例

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',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, 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),
      ),
      body: Center(
        // ClipRect was used so paint beyond ClipRect area won't be rendered
        child: ClipRect(
          child: Container(
            color: Colors.lightBlueAccent,
            height: 50,
            width: 200,
            child: CustomPaint(
              painter: CustomPaintNotch(),
            ),
          ),
        ),
      ),
    );
  }
}

class CustomPaintNotch extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    var paint = Paint()
      ..color = Colors.green;

    Offset center = Offset(size.width / 2, 0);
    // As a notch, RRect will be drawn with offset
    // With ClipRect used, paint outside the area won't be rendered
    RRect roundRectangle = RRect.fromRectAndRadius(
        Rect.fromCenter(center: center, width: 100.0, height: 40.0),
        Radius.circular(20.0));
    canvas.drawRRect(roundRectangle, paint);
  }

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

【讨论】:

    猜你喜欢
    • 2020-09-22
    • 2020-12-15
    • 2021-06-24
    • 2019-05-25
    • 1970-01-01
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    • 2021-10-05
    相关资源
    最近更新 更多