gpt4 book ai didi

flutter - 使正方形透明的一部分-Flutter

转载 作者:行者123 更新时间:2023-12-03 03:22:48 27 4
gpt4 key购买 nike

我想实现图中给出的扫描仪 View 。我已经使用BoxDecoration设计带有圆角的正方形。

            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

有人可以帮忙吗?

最佳答案

您可以尝试将CustomPaint与路径一起使用。

请找到完整的示例here in the DartPad.

棘手的部分是确定如何裁剪圆形的白色矩形边框。我只是使用自定义路径。我创建了自定义矩形,并从中创建了路径:

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

它可能不是最有效的方法,但是有时使用CustomPainter绘制某些东西要比尝试一些已经提供的小部件要快。这样,无论 Material 小部件的API如何更改,您都将始终拥有相同的外观。

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

关于flutter - 使正方形透明的一部分-Flutter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61407889/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com