flutter 中的自定义形状,如 OvalShape

问题描述 投票:0回答:1

我正在开发一个 Flutter 项目,我需要创建一个如下所示的自定义形状:

我尝试使用 CustomShapeClipper,但无法完全实现我需要的确切形状。

我听说过 Flutter 中的 CustomPainter 小部件,但我不熟悉它。有人可以提供有关如何使用 CustomPainter 创建此特定形状的指导或建议替代方法来实现它吗?

任何帮助或建议将不胜感激。预先感谢您!

flutter shapes custom-ui flutter-ui customclipper
1个回答
0
投票

实现椭圆形的简单方法是在容器中使用

ShapeDecoration
并将
shape
设置为
StadiumBorder
,如下所示:

代码:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: Container(
            height: 50,
            width: 150,
            decoration: ShapeDecoration(
              color: Colors.blue,
              shape: StadiumBorder(),
            ),
          ),
        ),
      ),
    );
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.