flutter 曲线条

标签 flutter dart widget

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

enter image description here

这是我的 flutter 代码:

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 类(关键方法是 _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),
        ],
      ),
    ),

结果:

enter image description here

关于 flutter 曲线条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56723258/

相关文章:

android - flutter blue 示例无法连接到 gatt 服务器

firebase-realtime-database - flutter 如何让 gridview 像按钮一样显示 firebase 数据

flutter 行和列

dart - 尝试对自定义对象列表进行排序时出现 TypeError

flutter - 如何在 Google 表格中接受可变数量的参数

c++ - Qt 在布局中自动排列小部件

widget - CKEditor,如何删除小部件之前和之间的空段落

firebase - 没有创建 Firebase App '[DEFAULT]' - 在 Flutter 和 Firebase 中调用 Firebase.initializeApp()

list - 在Flutter中点击其中一个元素时,如何更改列表中其他元素的属性

c++ - 从自定义小部件继承