想要复制一些用户界面.我可以用内置的Flutter 小部件做this吗?我试过使用芯片,但没能成功.使用CustomPainter是正确的吗?

推荐答案

我赚了TestPage,把它放到你的应用程序里,看看结果.更改宽度以保持形状完整.它是静态的,中间有三个项目,左边有一个图标.

如果你想让它动态,你必须以各种方式修改形状,并需要优化

enter image description here

import 'package:flutter/material.dart';

class TestPage extends StatelessWidget {
  const TestPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    double width = 300;
    return Center(
      child: SizedBox(
          width: width,
          height: width * 0.2442,
          child: CustomPaint(painter: CustomShape())),
    );
  }
}

class CustomShape extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    Path path_0 = Path();
    path_0.moveTo(size.width * 0.8778848, 0);
    path_0.lineTo(size.width * 0.4401651, 0);
    path_0.arcToPoint(Offset(size.width * 0.3433113, size.height * 0.1957085),
        radius:
            Radius.elliptical(size.width * 0.1218096, size.height * 0.4987930),
        rotation: 0,
        largeArc: false,
        clockwise: false);
    path_0.lineTo(size.width * 0.3433113, size.height * 0.1957085);
    path_0.lineTo(size.width * 0.3109976, size.height * 0.3582477);
    path_0.cubicTo(
        size.width * 0.2960634,
        size.height * 0.4332588,
        size.width * 0.2690989,
        size.height * 0.4360304,
        size.width * 0.2538154,
        size.height * 0.3555655);
    path_0.arcToPoint(Offset(size.width * 0.2030523, size.height * 0.1251676),
        radius:
            Radius.elliptical(size.width * 0.2818497, size.height * 1.154135),
        rotation: 0,
        largeArc: false,
        clockwise: false);
    path_0.arcToPoint(Offset(size.width * 0.1860221, size.height * 0.07393831),
        radius:
            Radius.elliptical(size.width * 0.1240584, size.height * 0.5080018),
        rotation: 0,
        largeArc: false,
        clockwise: false);
    path_0.lineTo(size.width * 0.1858475, size.height * 0.07393831);
    path_0.lineTo(size.width * 0.1858475, size.height * 0.07393831);
    path_0.arcToPoint(Offset(size.width * 0.1221152, size.height),
        radius:
            Radius.elliptical(size.width * 0.1221152, size.height * 0.5000447),
        rotation: 0,
        largeArc: true,
        clockwise: false);
    path_0.arcToPoint(Offset(size.width * 0.1858475, size.height * 0.9265087),
        radius:
            Radius.elliptical(size.width * 0.1213292, size.height * 0.4968261),
        rotation: 0,
        largeArc: false,
        clockwise: false);
    path_0.lineTo(size.width * 0.1858475, size.height * 0.9265087);
    path_0.cubicTo(
        size.width * 0.2068950,
        size.height * 0.8824318,
        size.width * 0.2298203,
        size.height * 0.7975861,
        size.width * 0.2549071,
        size.height * 0.6654448);
    path_0.cubicTo(
        size.width * 0.2707364,
        size.height * 0.5833706,
        size.width * 0.2936617,
        size.height * 0.5869468,
        size.width * 0.3087269,
        size.height * 0.6526598);
    path_0.lineTo(size.width * 0.3433113, size.height * 0.8046491);
    path_0.lineTo(size.width * 0.3433113, size.height * 0.8046491);
    path_0.arcToPoint(Offset(size.width * 0.4401651, size.height * 1.000268),
        radius:
            Radius.elliptical(size.width * 0.1218532, size.height * 0.4989718),
        rotation: 0,
        largeArc: false,
        clockwise: false);
    path_0.lineTo(size.width * 0.8778848, size.height * 1);
    path_0.arcToPoint(Offset(size.width * 0.9999782, size.height * 0.5000447),
        radius:
            Radius.elliptical(size.width * 0.1220934, size.height * 0.4999553),
        rotation: 0,
        largeArc: false,
        clockwise: false);
    path_0.lineTo(size.width * 1, size.height * 0.5000447);
    path_0.arcToPoint(Offset(size.width * 0.8778848, 0),
        radius:
            Radius.elliptical(size.width * 0.1221152, size.height * 0.5000447),
        rotation: 0,
        largeArc: false,
        clockwise: false);
    path_0.close();

    Paint paint_0_fill = Paint()..style = PaintingStyle.fill;
    paint_0_fill.color = Colors.grey[500]!;
    canvas.drawPath(path_0, paint_0_fill);
  }

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

Flutter相关问答推荐

为什么我需要等待重新访问Firestore数据库,即使它已经做了之前?

从Flutter应用程序解释蓝牙BLE数据:了解Sylvania压力值

Firebase动态链接已弃用,不应在新项目中使用.Flutter 替代方案

关闭模式,但再次打开时微件条件相同

在LinkedIn上分享Fighter和Web的链接会产生不同的结果

框中的Flutter 适配图像

Flutter:无法构建iOS应用程序-找不到PigeonParser.h文件

Flutter卡内容未在水平列表视图中居中

运行调试flutter应用程序时出错:B/BL超出范围(最大+/-128MB)到'';

如何对齐 AppBar Actions 中的文本?

Flutter中不使用Container是否可以做margin

参数类型void Function()无法分配给参数类型void Function(LongPressEndDetails)?

Flutter:如何设置 PersistentFooterButtons 背景?

Flutter记录返回类型问题

Riverpod 2.3.6:AutoDisposeNotifier和ref.onDispose自动处理

有什么办法可以消除Flutter 中的传递依赖?

容器在发布模式下显示灰色框(抖动)

使用 onPressed 切换到另一个屏幕无法正常工作

Android Studio 模拟器屏幕闪烁

Flutter 评级栏包选中和未选中的容器