简体   繁体   English

如何在 Flutter 中创建这样的边框?

[英]How to create a border like this in Flutter?

How to create a border like this:如何创建这样的边框:

在此处输入图像描述

Try below code hope its helpful to you.试试下面的代码希望它对你有帮助。 Used flutter_custom_clippers package here and used PointsClipper() for your expected design In this package you used more shapes see documentation in given link.在这里使用flutter_custom_clippers package 并使用 PointsClipper PointsClipper()进行您的预期设计在这个 package 中,您使用了更多形状,请参阅给定链接中的文档。 add flutter_custom_clippers: ^2.0.0 dependency in your pubspec.yaml file在您的pubspec.yaml文件中添加flutter_custom_clippers: ^2.0.0依赖项

        ClipPath(
                    clipper: PointsClipper(),
                    child: Container(
                      height: 100,
                      color: Colors.grey[300],
                      child: Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Row(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisAlignment:
                              MainAxisAlignment.spaceBetween,
                          children: [
                            Text(
                              'Total',
                              style: TextStyle(
                                color: Colors.pink,
                                fontSize: 20,
                                fontWeight: FontWeight.bold,
                              ),
                            ),
                            Text(
                              'QAR 130.00',
                              style: TextStyle(
                                color: Colors.pink,
                                fontSize: 15,
                                fontWeight: FontWeight.bold,
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                  ),

Your result screen->您的结果屏幕-> 在此处输入图像描述

在此处输入图像描述

You can achieve a similar clipped border using CustomClipper .您可以使用CustomClipper实现类似的裁剪边框。 Here is a simple CustomClipper I have created for you.这是我为您创建的一个简单的CustomClipper

First Create a custom clipper.首先创建一个自定义剪辑器。

class MyClipper extends CustomClipper<Path> {
  @override
  Path getClip(Size size) {
    var smallLineLength = size.width / 20;
    const  smallLineHeight = 20;
    var path = Path();

    path.lineTo(0, size.height);
    for (int i = 1; i <= 20; i++) {
      if (i % 2 == 0) {
        path.lineTo(smallLineLength * i, size.height);
      } else {
        path.lineTo(smallLineLength * i, size.height - smallLineHeight);
        
      }
    }
    path.lineTo(size.width, 0);
    path.close();

    return path;
  }

  @override
  bool shouldReclip(CustomClipper old) => false;
}

And wrap the created CustomClipper with ClipPath .并用ClipPath CustomClipper

     SizedBox(
      height: 200,
      width: 500,
      child: ClipPath(
          clipper: MyClipper(),
          child: Container(
            height: 200,
            width: 500,
            alignment: Alignment.center,
            color: Colors.red,
            child: const Text("abc"),
          )),
    ),

Full Code:完整代码:

import 'package:flutter/material.dart';

const Color darkBlue = Color.fromARGB(255, 18, 32, 47);

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: MyWidget(),
      ),
    );
  }
}

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(crossAxisAlignment: CrossAxisAlignment.center, children: [
         SizedBox(
          height: 200,
          width: 500,
          child: ClipPath(
              clipper: MyClipper(),
              child: Container(
                height: 200,
                width: 500,
                alignment: Alignment.center,
                color: Colors.red,
                child: const Text("abc"),
              )),
        ),
      ]),
    );
  }
}

class MyClipper extends CustomClipper<Path> {
  @override
  Path getClip(Size size) {
    var smallLineLength = size.width / 20;
    const  smallLineHeight = 20;
    var path = Path();

    path.lineTo(0, size.height);
    for (int i = 1; i <= 20; i++) {
      if (i % 2 == 0) {
        path.lineTo(smallLineLength * i, size.height);
      } else {
        path.lineTo(smallLineLength * i, size.height - smallLineHeight);
        
      }
    }
    path.lineTo(size.width, 0);
    path.close();

    return path;
  }

  @override
  bool shouldReclip(CustomClipper old) => false;
}

You can run this code by copying/pasting in dartpad .您可以通过在dartpad复制/粘贴来运行此代码。 You can learn more about CustomClipper from here , medium article您可以从这里了解有关CustomClipper的更多信息, 中等文章

import 'package:flutter/material.dart';

import 'a.dart';

void main() => runApp(VideoPlayerApp());

class VideoPlayerApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Zigzag',
      home: SafeArea(
        child: Scaffold(
          body: ZigzagApp(),
        ),
      ),
    );
  }
}

class ZigzagApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      height: 200,
      color: Colors.pinkAccent,
      child: CustomPaint(
        size: MediaQuery.of(context).size,
        painter: MyPainter(),
      ),
    );
  }

}

//paint widget for zigzag //为之字形绘制小部件

    import 'package:flutter/material.dart';
import 'dart:math' as math;

class MyPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    var paint = Paint();
    paint.color = Colors.white;
    paint.style = PaintingStyle.fill;

    paintZigZag(canvas, paint, Offset(0, 200), Offset(400, 200), 35, 10);
  }

  void paintZigZag(Canvas canvas, Paint paint, Offset start, Offset end,
      int zigs, double width) {
    assert(zigs.isFinite);
    assert(zigs > 0);
    canvas.save();
    canvas.translate(start.dx, start.dy);
    end = end - start;
    canvas.rotate(math.atan2(end.dy, end.dx));
    final double length = end.distance;
    final double spacing = length / (zigs * 2.0);
    final Path path = Path()..moveTo(0.0, 0.0);
    for (int index = 0; index < zigs; index += 1) {
      final double x = (index * 2.0 + 1.0) * spacing;
      final double y = width * ((index % 2.0) * 2.0 - 1.0);
      path.lineTo(x, y);
    }
    path.lineTo(length, 0.0);
    canvas.drawPath(path, paint);
    canvas.restore();
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }
}

I Think You can use CustomPaint Widget我认为您可以使用 CustomPaint 小部件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM