Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to give a "Dashed Border" in flutter?

Tags:

flutter

dart

I try to give dashed border in flutter but there is no option for dashed border in flutter. so any another way to create dashed border in futter.

  new Container(
          decoration: new BoxDecoration(
              border: Border(
                  left: BorderSide(color: Color(0XFFFF6D64), width: 2.0))),
          height: 20.0,
          margin: const EdgeInsets.only(left: 35.0),
          child: new Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              new DecoratedBox(
                decoration: new BoxDecoration(
                    border: Border(
                        left:
                            BorderSide(color: Color(0XFFFF6D64), width: 2.0,style: BorderStyle.))),

              )
            ],
          ),
        ),
like image 684
Ravindra Bhanderi Avatar asked Jul 19 '18 09:07

Ravindra Bhanderi


People also ask

How do you add a dash in Flutter?

Just one line and you should have a dash :D.

How do you style a border in Flutter?

Steps to add border to container in Flutter: Step 1: Go to the Container in which you want to add a border. Step 2: Add the decoration parameter and assign the BoxDecoration class. Inside the BoxDecoration add the parameter border and set it to Border. all().


Video Answer


3 Answers

As long as it is a Rectangular dashed border you want, you can now use dotted_border package which I have uploaded to Pub.

Usage

DottedBorder(
  color: Colors.black,
  strokeWidth: 1,
  child: FlutterLogo(size: 148),
)

enter image description here

like image 190
Ajil O. Avatar answered Oct 19 '22 01:10

Ajil O.


Image

Use this component:

import 'dart:math';

import 'package:flutter/material.dart';

class CircularBorder extends StatelessWidget {

  final Color color;
  final double size;
  final double width;
  final Widget icon;

  const CircularBorder({Key key, this.color = Colors.blue, this.size = 70, this.width = 7.0, this.icon}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: size,
      width: size,
      alignment: Alignment.center,
      child: Stack(
        alignment: Alignment.center,
        children: <Widget>[
          icon == null ? Container() : icon,
          CustomPaint(
            size: Size(size, size),
            foregroundPainter: new MyPainter(
                completeColor: color,
                width: width),
          ),
        ],
      ),
    );
  }
}

class MyPainter extends CustomPainter {
  Color lineColor =  Colors.transparent;
  Color completeColor;
  double width;
  MyPainter(
      { this.completeColor, this.width});
  @override
  void paint(Canvas canvas, Size size) {
    Paint complete = new Paint()
      ..color = completeColor
      ..strokeCap = StrokeCap.round
      ..style = PaintingStyle.stroke
      ..strokeWidth = width;

    Offset center = new Offset(size.width / 2, size.height / 2);
    double radius = min(size.width / 2, size.height / 2);
    var percent = (size.width *0.001) / 2;

    double arcAngle = 2 * pi * percent;
    print("$radius - radius");
    print("$arcAngle - arcAngle");
    print("${radius / arcAngle} - divider");

    for (var i = 0; i < 8; i++) {
      var init = (-pi / 2)*(i/2);
      
      canvas.drawArc(new Rect.fromCircle(center: center, radius: radius),
          init, arcAngle, false, complete);
    }

 
  }

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

Using:

CircularBorder(
          width: 6,
          size: 55,
          color: Colors.grey,
          icon: Icon(Icons.access_alarm, color: Colors.grey),
        ),
like image 26
David Araujo Avatar answered Oct 19 '22 00:10

David Araujo


For rounded corners using this, you can use

    CardRadius = 10.0;
    return DottedBorder(
      color: Colors.black,
      strokeWidth: 3,
      radius: Radius.circular(CardRadius),
      dashPattern: [10, 5],
      customPath: (size) {
        return Path()
          ..moveTo(CardRadius, 0)
          ..lineTo(size.width - CardRadius, 0)
          ..arcToPoint(Offset(size.width, CardRadius), radius: Radius.circular(CardRadius))
          ..lineTo(size.width, size.height - CardRadius)
          ..arcToPoint(Offset(size.width - CardRadius, size.height), radius: Radius.circular(CardRadius))
          ..lineTo(CardRadius, size.height)
          ..arcToPoint(Offset(0, size.height - CardRadius), radius: Radius.circular(CardRadius))
          ..lineTo(0, CardRadius)
          ..arcToPoint(Offset(CardRadius, 0), radius: Radius.circular(CardRadius));
      },
      child: Container(...)
    }

This is what it looks like

enter image description here

like image 9
Bubunyo Nyavor Avatar answered Oct 18 '22 23:10

Bubunyo Nyavor