【问题标题】:Flutter Curve Bar颤振曲线条
【发布时间】:2019-11-05 11:23:44
【问题描述】:

我想知道是否有更好的解决方案来制作如下图所示的弯曲条。

这是我的颤振代码:

import 'package:flutter_web/material.dart';

class CurvedBar extends StatelessWidget {
    const CurvedBar({
      Key key,
    }) : super(key: key);

    @override
    Widget build(BuildContext context) {
      return Container(height: 50,
      color: Colors.orange,
      child:  Column(
        children: <Widget>[
          ClipRRect(
            borderRadius: BorderRadius.only(
                bottomLeft: Radius.circular(20.0),
                bottomRight: Radius.circular(20.0)),
            child: Container(
              height: 20.0,
              width: double.infinity,
              color: Colors.white,
            ),
          ),
         Container(
           color: Colors.white,
           child:  Row( 
            children: <Widget>[
              Flexible(
                  flex: 1,
                  child: ClipRRect(
                    borderRadius:
                        BorderRadius.only(topRight: Radius.circular(20.0)),
                    child: Container(
                      height: 20.0,
                      color: Colors.orange,
                    ),
                  )),
              Flexible(
                  flex: 1,
                  child: ClipRRect(
                    borderRadius:
                        BorderRadius.only(topLeft: Radius.circular(20.0)),
                    child: Container(
                      height: 20.0,
                      color: Colors.orange,
                    ),
                  ))
            ],
          ))
        ],
      ));
    }
  }

【问题讨论】:

  • 使用自定义ShapeBorder(扩展ShapeBorder的类)?
  • @pskink 我检查了这个类的样本,但我仍然不知道如何获得相同的结果

标签: flutter dart widget


【解决方案1】:

制作一个像这样的自定义ShapeBorder 类(关键方法是_getPath,它返回您的形状的Path):

class CustomShapeBorder extends ShapeBorder {
  const CustomShapeBorder();

  @override
  Path getInnerPath(Rect rect, {TextDirection textDirection}) => _getPath(rect);

  @override
  Path getOuterPath(Rect rect, {TextDirection textDirection}) => _getPath(rect);

  _getPath(Rect rect) {
    final r = rect.height / 2;
    final radius = Radius.circular(r);
    final offset = Rect.fromCircle(center: Offset.zero, radius: r);
    return Path()
      ..moveTo(rect.topLeft.dx, rect.topLeft.dy)
      ..relativeArcToPoint(offset.bottomRight, clockwise: false, radius: radius)
      ..lineTo(rect.center.dx - r, rect.center.dy)
      ..relativeArcToPoint(offset.bottomRight, clockwise: true, radius: radius)
      ..relativeArcToPoint(offset.topRight, clockwise: true, radius: radius)
      ..lineTo(rect.centerRight.dx - r, rect.centerRight.dy)
      ..relativeArcToPoint(offset.topRight, clockwise: false, radius: radius)
      ..addRect(rect);
  }

  @override
  EdgeInsetsGeometry get dimensions {
    return EdgeInsets.all(0);
  }

  @override
  ShapeBorder scale(double t) {
    return CustomShapeBorder();
  }

  @override
  void paint(Canvas canvas, Rect rect, {TextDirection textDirection}) {
  }
}

现在你可以像这样使用它了:

    Container(
      margin: EdgeInsets.only(top: 80),
      height: 50,
      width: double.infinity,
      decoration: ShapeDecoration(
        shape: CustomShapeBorder(),
        //color: Colors.orange,
        gradient:
            LinearGradient(colors: [Colors.blue, Colors.orange]),
        shadows: [
          BoxShadow(
              color: Colors.black, offset: Offset(3, -3), blurRadius: 3),
        ],
      ),
    ),

结果:

【讨论】:

  • @pspkink 你是如何找到解决方案的...我的意思是我需要学习什么才能制作我想象的任何自定义形状?
  • 干得好!! - 似乎你实现了其余的方法......结果看起来很不错,不是吗?顺便说一句,我不明白ShapeBorder 中的paint 方法,因为看起来即使是空的paint 方法体,形状也会被绘制!!以及我如何找出解决方案?这很简单Path 操作... ;-)
  • 好的,看来我现在明白了:paint() 在绘制形状后调用,以便您可以在其上绘制一些额外的东西
  • 是的,我有一个更好的主意..顺便说一句,我的原始截图是flutter web
  • CustomShapeBorder 在 Flutter Web 上工作吗?我在颤振(Linux)桌面上测试了它;-)
猜你喜欢
  • 2019-11-11
  • 2020-11-10
  • 2020-10-12
  • 2019-07-10
  • 2021-07-28
  • 2021-04-25
  • 2022-01-08
  • 2019-05-31
  • 1970-01-01
相关资源
最近更新 更多