gpt4 book ai didi

flutter - 如何像本例中那样在 flutter 中转换矩形?

转载 作者:IT王子 更新时间:2023-10-29 07:21:57 25 4
gpt4 key购买 nike

我是 Flutter 和 Dart 的初学者,正在设计一个自定义导航栏。我想知道的是如何使用 flutter 将矩形转换成这种形状?

enter image description here

非常感谢任何有关自定义绘制小部件的帮助或教程!

最佳答案

ClipPath 可以成为您的解决方案,您可以像这样创建自定义剪辑器:

class MyClipper extends CustomClipper<Path> {
@override
Path getClip(Size size) {
Path myPath = Path();
myPath.lineTo(0.0, size.height);

myPath.quadraticBezierTo(
size.width / 4,
size.height / 1.2,
size.width / 2,
size.height / 1.2
);
myPath.quadraticBezierTo(
size.width - (size.width / 4),
size.height / 1.2,
size.width,
size.height);
myPath.lineTo(size.width, 0.0);
return myPath;
}

@override
bool shouldReclip(CustomClipper<Path> oldClipper) {
return false;
}
}

我在下面发布了我的全部代码,您可以使用它并转换为您需要的:enter image description here

import 'package:flutter/material.dart';

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

class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Container(
child: Center(
child: ClipPath(
clipper: MyClipper(),
child: Container(
height: 200,
width: 300,
color: Colors.black26,
),
)),
),
),
);
}
}

class MyClipper extends CustomClipper<Path> {
@override
Path getClip(Size size) {
Path myPath = Path();
myPath.lineTo(0.0, size.height);

myPath.quadraticBezierTo(
size.width / 4,
size.height / 1.2,
size.width / 2,
size.height / 1.2
);
myPath.quadraticBezierTo(
size.width - (size.width / 4),
size.height / 1.2,
size.width,
size.height);
myPath.lineTo(size.width, 0.0);
return myPath;
}

@override
bool shouldReclip(CustomClipper<Path> oldClipper) {
return false;
}
}

关于flutter - 如何像本例中那样在 flutter 中转换矩形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55192525/

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