gpt4 book ai didi

user-interface - 如何在 flutter 中有这样的圆形?

转载 作者:IT王子 更新时间:2023-10-29 06:39:22 28 4
gpt4 key购买 nike

我正在尝试在我的 flutter 项目中创建这样的圆形。我尝试在容器中使用 border-radius 。但它并没有像我预期的那样工作。

那么,我怎样才能得到像给定图片这样的形状呢?

image

我试过使用这样的容器。

    Container(
height: 72,
width: 211,
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.only(
topRight: Radius.circular(50),
topLeft: Radius.circular(30))
),
)

最佳答案

您几乎可以使用 CustomPaint 绘制任何东西,看看:

https://api.flutter.dev/flutter/rendering/CustomPainter-class.html

在下面的代码中,我画了类似那个圆圈的东西:

enter image description here

import "package:flutter/material.dart";

void main() {
runApp(MaterialApp(title: "", home: Home()));
}

class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(),
body: Container(
width: double.infinity,
height: double.infinity,
child: CustomPaint(
painter: CirclePainter(),
)),
),
);
}
}

class CirclePainter extends CustomPainter {
final Paint lightBluePaint = Paint()..color = Color(0xFFbde5fa);
final Paint bluePaint = Paint()..color = Color(0xFF5abef2);
final TextPainter textPainter = TextPainter(
textDirection: TextDirection.ltr,
);

final TextStyle textStyle = TextStyle(
color: Colors.white.withAlpha(240),
fontSize: 18,
letterSpacing: 1.2,
fontWeight: FontWeight.w900);

@override
void paint(Canvas canvas, Size size) {
var rect = Rect.fromLTRB(
-100, size.height - 120, size.width * 0.7, size.height + 250);

final Path circle = Path()..addOval(rect);
final Path smallCircle = Path()..addOval(rect.inflate(50));

canvas.drawPath(smallCircle, lightBluePaint);
canvas.drawPath(circle, bluePaint);

drawText(canvas, size, 'Write now');
}

void drawText(Canvas canvas, Size size, String text) {
textPainter.text = TextSpan(style: textStyle, text: text);
textPainter.layout();
textPainter.paint(canvas, Offset(50, size.height - 60));
}

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

关于user-interface - 如何在 flutter 中有这样的圆形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56626824/

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