Home > OS >  Flutter : How to create a border like this
Flutter : How to create a border like this

Time:01-05

enter image description here

I need a border like this . somebody please help me. Thank you

CodePudding user response:

enter image description here

You can achieve a similar clipped border using CustomClipper. Here is a simple CustomClipper I have created for you.

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.

     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 enter image description here

CodePudding user response:

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;
  }
}

CodePudding user response:

I Think You can use CustomPaint Widget

  •  Tags:  
  • Related