gpt4 book ai didi

flutter - 在 Flutter 中绘制六边形

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

如何在 Flutter 中绘制与图片中相同的六边形

enter image description here

最佳答案

您可以使用ClipPath

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Hexagon',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Hexagon'),
);
}
}

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

final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Padding(
padding: const EdgeInsets.all(50.0),
child: ClipPath(
child: Container(
color: Colors.amber,
),
clipper: _MyClipper(),
),
),
);
}
}

class _MyClipper extends CustomClipper<Path> {
@override
Path getClip(Size size) {
final path = Path();
path.lineTo(0, 0);
path.lineTo(size.width, 0);
path.lineTo(size.width, size.height * 0.8);
path.lineTo(size.width * 0.8, size.height);
path.lineTo(size.width * 0.2, size.height);
path.lineTo(0, size.height * 0.8);
path.lineTo(0, 0);

path.close();
return path;
}

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

关于flutter - 在 Flutter 中绘制六边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54815089/

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