3

I want to achieve the scanner view as given in the picture. I've used BoxDecoration to design square with rounded corners.

            Center(
                  child: Container(
                    width: BarReaderSize.width,
                    height: BarReaderSize.height,
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(20),
                        border: Border.all(color: Colors.white, width: 3)),
                  ),
                )

enter image description here

Can someone help ?

1

1 Answer 1

6

You can try to use CustomPaint with some clipping with paths.

Please find the full example here in the DartPad.

The tricky part is to determine how to clip the rounded white rectangle borders. I just used custom Path for that. I created custom Rects and created a Path out of them:

final path = Path()
      ..addRect(clippingRect0)
      ..addRect(clippingRect1)
      ..addRect(clippingRect2)
      ..addRect(clippingRect3);

It may be not the most efficient approach, but sometimes it's faster to draw something with CustomPainter than to experiment with some already provided widgets. This way you will have always the same look no matter how the API of the material widget changes.

enter image description here

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatelessWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        color: Colors.grey,
        child: Stack(
          children: [
            Center(
              child: FlutterLogo(
                size: 800,
              ),
            ),
            Container(
              child: Center(
                child: CustomPaint(
                  painter: BorderPainter(),
                  child: Container(
                    width: BarReaderSize.width,
                    height: BarReaderSize.height,
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class BorderPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    final width = 3.0;
    final radius = 20.0;
    final tRadius = 2 * radius;
    final rect = Rect.fromLTWH(
      width,
      width,
      size.width - 2 * width,
      size.height - 2 * width,
    );
    final rrect = RRect.fromRectAndRadius(rect, Radius.circular(radius));
    final clippingRect0 = Rect.fromLTWH(
      0,
      0,
      tRadius,
      tRadius,
    );
    final clippingRect1 = Rect.fromLTWH(
      size.width - tRadius,
      0,
      tRadius,
      tRadius,
    );
    final clippingRect2 = Rect.fromLTWH(
      0,
      size.height - tRadius,
      tRadius,
      tRadius,
    );
    final clippingRect3 = Rect.fromLTWH(
      size.width - tRadius,
      size.height - tRadius,
      tRadius,
      tRadius,
    );

    final path = Path()
      ..addRect(clippingRect0)
      ..addRect(clippingRect1)
      ..addRect(clippingRect2)
      ..addRect(clippingRect3);

    canvas.clipPath(path);
    canvas.drawRRect(
      rrect,
      Paint()
        ..color = Colors.white
        ..style = PaintingStyle.stroke
        ..strokeWidth = width,
    );
  }

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

class BarReaderSize {
  static double width = 200;
  static double height = 200;
}

5
  • he already has Container so its a best job for Container.decoration (or foregroundDecoration) - all you need is a custom Decoration class - some examples here: gist.github.com/pskink/da43c327b75eec05d903fa1b4d0c4d3e
    – pskink
    Commented Apr 24, 2020 at 13:39
  • Worked like a charm! Thanks a lot
    – Darshan HK
    Commented Apr 24, 2020 at 14:06
  • @pskink it's relatively easy to convert CustomPainter to decoration. That's how the FlutterLogo is done actually Commented Apr 24, 2020 at 14:39
  • 1
    yes I know, that's why I said that decoration property is much concise than CustomPaint - I even gave you two sample decorations
    – pskink
    Commented Apr 24, 2020 at 14:43
  • I agree 100% @pskink Commented Apr 24, 2020 at 15:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.