有什么方法可以在顶部和底部显示偏斜边框吗?
我通过使用两个图像(top_layout和bottom_layout.png)提出了下面的解决方案。还有其他方法可以在不使用静态图像的情况下使那些带有阴影的颜色条吗?

return Container(
      color: const Color.fromARGB(255, 236, 0, 140),
      child: Container(
        padding: const EdgeInsets.all(8.0),
        child: Container(
          color: Colors.white,
          margin:
              EdgeInsets.only(top: 60.0, bottom: 20.0, left: 15.0, right: 15.0),
          child: Stack(
            children: <Widget>[
              Positioned.fill(
                child: Image.asset(
                  "assets/imgs/top_layout.png",
                  fit: BoxFit.fitWidth,
                  alignment: Alignment.topCenter,
                ),
              ),
              Positioned.fill(
                child: Image.asset(
                  "assets/imgs/xbottom_layout.png",
                  fit: BoxFit.fitWidth,
                  alignment: Alignment.bottomLeft,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

最佳答案

如何使用CustomPaint小部件在Flutter中绘制线条

dart - 用 flutter 画线-LMLPHP

要在Flutter中绘画,请使用 CustomPaint 小部件。 CustomPaint小部件将 CustomPainter 对象作为参数。在该类中,您必须重写paint方法,该方法为您提供了可以在其上绘画的 Canvas 。这是在上图中绘制线条的代码。

@override
void paint(Canvas canvas, Size size) {
  final p1 = Offset(50, 50);
  final p2 = Offset(250, 150);
  final paint = Paint()
    ..color = Colors.black
    ..strokeWidth = 4;
  canvas.drawLine(p1, p2, paint);
}

笔记:
  • drawLine方法绘制一条直线,将您给出的两点连接起来。
  • Offset是一对(dx, dy) double ,从CustomPaint小部件的左上角偏移。

  • 另外一个选项

    您可以使用drawPoints选项使用PointMode.polygon方法执行类似的操作。

    dart - 用 flutter 画线-LMLPHP
    @override
    void paint(Canvas canvas, Size size) {
      final pointMode = ui.PointMode.polygon;
      final points = [
        Offset(50, 100),
        Offset(150, 75),
        Offset(250, 250),
        Offset(130, 200),
        Offset(270, 100),
      ];
      final paint = Paint()
        ..color = Colors.black
        ..strokeWidth = 4
        ..strokeCap = StrokeCap.round;
      canvas.drawPoints(pointMode, points, paint);
    }
    

    语境

    这是main.dart代码,以便您可以在上下文中看到它。
    import 'dart:ui' as ui;
    import 'package:flutter/material.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            body: HomeWidget(),
          ),
        );
      }
    }
    
    class HomeWidget extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Center(
          child: CustomPaint( //                       <-- CustomPaint widget
            size: Size(300, 300),
            painter: MyPainter(),
          ),
        );
      }
    }
    
    class MyPainter extends CustomPainter { //         <-- CustomPainter class
      @override
      void paint(Canvas canvas, Size size) {
        //                                             <-- Insert your painting code here.
      }
    
      @override
      bool shouldRepaint(CustomPainter old) {
        return false;
      }
    }
    

    也可以看看

    有关我的完整答案,请参见this article

    10-01 21:21