【问题标题】:Asking how to achieve the design problem in flutter问如何在flutter中实现设计问题
【发布时间】:2021-03-19 09:31:00
【问题描述】:

大家好,请问可以用代码实现这种设计吗?我真的被卡住了,这是我想要实现的设计

但真正的问题是我只能做到这一点

这是我的示例代码:

在我的主要:

return Scaffold(
      backgroundColor: Color(0xff012a60),
      body: Stack(children: <Widget>[
        /*_AnimatedCircle(
          outerController: outerController,
          innerController: innerController,
        ),*/
        _OuterCircle(circleColor: Colors.blueGrey[900].withOpacity(.8)),
        Column(children: <Widget>[
          const Spacer(flex: 50),
          Expanded(flex: 30, child: Container(color: Colors.white)),
          const Spacer(flex: 20),
        ]),
        Row(children: <Widget>[
          const Spacer(flex: 80),
          Transform.scale(
            scale: 2.5,
            child: Container(
                width: MediaQuery.of(context).size.width * 0.2,
                decoration: const BoxDecoration(
                    color: Colors.red, shape: BoxShape.circle)),
          ),
        ]),
        // _OuterCircle(circleColor: Colors.blueAccent.withOpacity(.25)),
        Row(children: <Widget>[
          const Spacer(flex: 80),
          Column(children: [
            Spacer(flex: 60),
            Transform.scale(
              scale: 2.5,
              child: Container(
                  width: MediaQuery.of(context).size.width * 0.2,
                  height: MediaQuery.of(context).size.width * 0.05,
                  decoration: const BoxDecoration(color: Colors.green)),
            ),
            Spacer(flex: 24),
          ]),
        ]),
      ]),
    );

这是我的另一堂课

class _OuterCircle extends StatelessWidget {
  const _OuterCircle({Key key, this.circleColor}) : super(key: key);

  final Color circleColor;

  @override
  Widget build(BuildContext context) {
    return Transform.scale(
        scale: 2,
        child: Container(
          width: MediaQuery.of(context).size.width * 0.47,
          decoration: BoxDecoration(shape: BoxShape.circle, color: circleColor),
        ));
  }
}

请帮助我真的坚持如何在 dart 代码中实现这种设计

【问题讨论】:

    标签: flutter user-interface dart flutter-layout


    【解决方案1】:

    我认为将几何问题从最大区域分解为小区域更容易。

    实现这一点的关键是使用Clip(ClipOval, ClipRect...)

    1. 上半部分可以通过右圆夹左圆来绘制
    2. 下半部分可以通过右圆裁剪内矩形来绘制

    1. 缩放堆栈区域
    2. 画左大圆(区域 1)
    3. 绘制中间白色矩形(区域 2)
    4. 用矩形画右圆(区域 3)
    5. 绘制中间交叉部分(区域 4)

    使用的小部件:ClipOvalFractionallySizedBoxAspectRatio

    左大圆圈

    class BigCircle extends StatelessWidget {
      const BigCircle({this.color,Key key}):super(key: key);
    
      final Color color;
    
      @override
      Widget build(BuildContext context) {
        return AspectRatio(
          aspectRatio: 1,
          child: Container(
            decoration: BoxDecoration(
              color: color,
              shape: BoxShape.circle,
            ),
          ),
        );
      }
    }
    

    自定义椭圆剪裁器(剪裁右白圈区域)

    class CustomClipOval extends CustomClipper<Rect> {
      @override
      Rect getClip(Size size) {
        final width = size.width * 0.42;
        return Rect.fromLTRB(
          size.width - width,
          (size.height - width) * 0.5,
          size.width,
          (size.height + width) * 0.5,
        );
      }
    
      @override
      bool shouldReclip(covariant CustomClipper<Rect> oldClipper) {
        return false;
      }
    }
    

    最后是堆栈

    return Scaffold(
      backgroundColor: const Color(0xff004471),
      body: Transform.scale(
        scale: 1.19,
        child: Stack(children: <Widget>[
          // Area 1
          const Align(
            alignment: Alignment.centerLeft,
            child: BigCircle(
              color: Color(0xff022c66),
            ),
          ),
    
          // Area 2
          Align(
            alignment: const Alignment(0, 0.4),
            child: FractionallySizedBox(
              heightFactor: 0.23,
              child: Container(
                color: Colors.white,
              ),
            ),
          ),
    
          // Area 3
          ClipOval(
            clipper: CustomClipOval(),
            child: Container(
              alignment: const Alignment(0, 0.46),
              color: Colors.white,
              child: FractionallySizedBox(
                heightFactor: 0.14,
                child: Container(
                  color: const Color(0xff335f86),
                ),
              ),
            ),
          ),
    
          // Area 4
          ClipOval(
            clipper: CustomClipOval(),
            child: const Align(
              alignment: Alignment.centerLeft,
              child: BigCircle(color: Color(0xff647294),),
            ),
          ),
        ]),
      ),
    );
    

    【讨论】:

    • 你好,谢谢回复,我想问一下矩形有曲线的部分怎么样?我在图片中有圆圈?我需要曲线边缘之一,我该如何实现?
    • 我将整个矩形包裹在区域 3 的ClipOval 内。圆外的矩形将被剪切。
    • 非常感谢您的帮助,真的帮助了我的一天,再加上一点调整就完成了
    猜你喜欢
    • 1970-01-01
    • 2012-05-29
    • 2021-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多