Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw the line with sharp ends /gradient line/ in flutter?

Tags:

flutter

dart

Using stroke method, how to create gradient line with sharp end in flutter? I want to draw the line as below in flutter.

enter image description here

like image 773
zemunkh Avatar asked Aug 20 '20 02:08

zemunkh


People also ask

What are stops in gradient flutter?

stops. A list of values from 0.0 to 1.0 that denote fractions along the gradient. If non-null, this list must have the same length as colors. If the first value is not 0.0, then a stop with position 0.0 and a color equal to the first color in colors is implied.


2 Answers

Use CustomPainter to draw:

import 'package:flutter/material.dart';

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

class Example extends StatefulWidget {
  @override
  _ExampleState createState() => _ExampleState();
}

class _ExampleState extends State<Example> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
            body: Center(
      child: CustomPaint(
        size: Size(200, 5),
        painter: CurvePainter(),
      ),
    )));
  }

  @override
  void dispose() {
    super.dispose();
  }
}

class CurvePainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    var paint = Paint();
    paint.color = Colors.black;
    paint.style = PaintingStyle.fill; // Change this to fill

    var path = Path();

    path.moveTo(0, 0);
    path.quadraticBezierTo(size.width / 2, size.height / 2, size.width, 0);
    path.quadraticBezierTo(size.width / 2, -size.height / 2, 0, 0);

    canvas.drawPath(path, paint);
  }

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

like image 107
sleepingkit Avatar answered Nov 15 '22 11:11

sleepingkit


modified sleepingkit answer:

import 'package:flutter/material.dart';

class PointedLinePainter extends CustomPainter {
  final double width;

  PointedLinePainter(this.width);

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

    var path = Path();

    path.moveTo(0, 0);
    path.quadraticBezierTo(width / 3, 0.5, width, 0);
    path.quadraticBezierTo(width / 3, -0.5, 0, 0);

    canvas.drawPath(path, paint);
  }

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

somewhere in code:

Container(
      margin: EdgeInsets.symmetric(horizontal: 10.0),
      alignment: Alignment.centerLeft,
      width: MediaQuery.of(context).size.width,
      height: 2,
      child: CustomPaint(
        painter:
            PointedLinePainter(width - 2 * horizontalPointedLineMargin),
      ),
    )

enter image description here

like image 29
Vladyslav Ulianytskyi Avatar answered Nov 15 '22 09:11

Vladyslav Ulianytskyi