如何在Flutter中实现自定义形状的容器 [英] How to achieve a custom shaped container in Flutter

查看:417
本文介绍了如何在Flutter中实现自定义形状的容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个自定义形状的容器,如下图所示.有没有办法在Flutter中构建自定义形状的容器?

I want to achieve a custom shaped container as it is in the below image. Is there a way to build a custom shaped container in the Flutter?

推荐答案

Karol的回答有点含糊.我决定用一些实际代码对其进行扩展.

Karol's answer is a bit vague. I decided to expand upon it with some actual code.

我认为这就是您要寻找的.最后一课对于这个问题特别有趣:

I think this is what you're looking for. The last class is specifically interesting for the question:

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: CustomPaint(
          painter: Chevron(),
          child: Container(
            width: 100.0,
            height: 120.0,
            child: Padding(
              padding: EdgeInsets.only(top: 30.0),
              child: Align(
                alignment: Alignment.topCenter,
                child: Text("1", style: TextStyle(fontSize: 24.0)),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

class Chevron extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    final Gradient gradient = new LinearGradient(
      begin: Alignment.topCenter,
      end: Alignment.bottomCenter,
      colors: [Colors.orangeAccent, Colors.yellow],
      tileMode: TileMode.clamp,
    );

    final Rect colorBounds = Rect.fromLTRB(0, 0, size.width, size.height);
    final Paint paint = new Paint()
      ..shader = gradient.createShader(colorBounds);

    Path path = Path();
    path.moveTo(0, 0);
    path.lineTo(0, size.height);
    path.lineTo(size.width / 2, size.height - size.height / 3);
    path.lineTo(size.width, size.height);
    path.lineTo(size.width, 0);
    path.close();

    canvas.drawPath(path, paint);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) => false;
}

这篇关于如何在Flutter中实现自定义形状的容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆