简体   繁体   English

如何在 Flutter 中设计这个自定义小部件形状

[英]How to design this custom widget shape in Flutter

I am designing a mobile app using Flutter, but I don't know how to design a custom button or listTile or any type of widget to look like the following image:我正在使用 Flutter 设计一个移动应用程序,但我不知道如何设计一个自定义按钮或listTile或任何类型的小部件,如下图所示:

在此处输入图片说明

You can create that design by composing Widgets, that's the beauty of Flutter, this is a sample I made:您可以通过组合 Widget 来创建该设计,这就是 Flutter 的美妙之处,这是我制作的示例:

            class MyCustomButton extends StatelessWidget {
              final String title;
              final Color color;
              final IconData icon;
              final radius = 35.0;
              final VoidCallback onTap;

              const MyCustomButton({
                Key key,
                this.title,
                this.color,
                this.icon,
                this.onTap,
              }) : super(key: key);

              @override
              Widget build(BuildContext context) {
                return Material(
                  color: Colors.transparent,
                  clipBehavior: Clip.hardEdge,
                  child: InkWell(
                    onTap: onTap,
                    child: SizedBox(
                      height: 2 * radius,
                      child: Stack(
                        fit: StackFit.expand,
                        children: [
                          Positioned(
                            top: 0.0,
                            left: radius,
                            right: 0.0,
                            bottom: 0.0,
                            child: Align(
                              child: Container(
                                height: radius + 10,
                                color: color,
                                alignment: Alignment.center,
                                child: Text(
                                  title,
                                  textAlign: TextAlign.center,
                                  style: TextStyle(
                                    fontSize: 22,
                                    color: Colors.white,
                                    fontWeight: FontWeight.w400,
                                  ),
                                ),
                              ),
                            ),
                          ),
                          Align(
                            alignment: Alignment.centerLeft,
                            child: Container(
                              height: 2 * radius,
                              width: 2 * radius,
                              decoration: BoxDecoration(
                                shape: BoxShape.circle,
                                color: color,
                              ),
                              child: Center(
                                child: Container(
                                  height: 2 * radius - 5,
                                  width: 2 * radius - 5,
                                  decoration: BoxDecoration(
                                    shape: BoxShape.circle,
                                    color: Colors.white,
                                  ),
                                  child: Icon(icon),
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                );
              }
            }

Usage用法

        SizedBox(
                  width: 300,
                  child: MyCustomButton(
                    title: "Click to Login",
                    color: Colors.orange,
                    icon: Icons.lock_open,
                    onTap: () {
                      print("Tapped here");
                    },
                  ),
                ),

Result结果

在此处输入图片说明

You can find more info here: https://flutter.dev/docs/development/ui/layout您可以在此处找到更多信息: https : //flutter.dev/docs/development/ui/layout

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM