Skip to content

Button 按钮

按钮用于开始一个即时操作。

何时使用

标记了一个(或封装一组)操作命令,响应用户点击行为,触发相应的业务逻辑。

代码演示

import 'package:antd_flutter_example/demo_block.dart';
import 'package:flutter/material.dart';
import 'package:trionesdev_antd_mobile/trionesdev_antd_mobile.dart';

class ButtonPage extends StatefulWidget {
  const ButtonPage({super.key});

  @override
  State<StatefulWidget> createState() => _ButtonPageState();
}

class _ButtonPageState extends State<ButtonPage> {
  @override
  Widget build(BuildContext context) {
    return AntScaffold(
        appBar: AntAppBar(
          title: Text('Button'),
        ),
        body: ListView(children: [
          DemoBlock(
            title: "普通使用",
            child: Wrap(
              direction: Axis.horizontal,
              alignment: WrapAlignment.start,
              spacing: 4.0,
              runSpacing: 4.0,
              children: [
                AntButton(
                  type: AntButtonType.primary,
                  text: 'Primary',
                  onPressed: () {
                    print('ElevatedButton');
                  },
                ),
                AntButton(
                  text: 'Default',
                  onPressed: () {
                    print('ElevatedButton');
                  },
                ),
                AntButton(
                  type: AntButtonType.text,
                  text: 'Text',
                  onPressed: () {
                    print('ElevatedButton');
                  },
                ),
                AntButton(
                  type: AntButtonType.link,
                  text: 'Link',
                  onPressed: () {
                    print('ElevatedButton');
                  },
                ),
              ],
            ),
          ),
          DemoBlock(
            title: "类型",
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              spacing: 10,
              children: [
                Wrap(spacing: 4, runSpacing: 4, children: [
                  AntButton(
                    text: 'Primary',
                    color: Color(0xff000000),
                    variant: AntButtonVariant.solid,
                    onPressed: () {
                      print('ElevatedButton');
                    },
                  ),
                  AntButton(
                    variant: AntButtonVariant.outlined,
                    color: Color(0xff000000),
                    text: 'Outlined',
                    onPressed: () {
                      print('ElevatedButton');
                    },
                  ),
                  AntButton(
                    variant: AntButtonVariant.filled,
                    color: Color(0xff000000),
                    text: 'Filled',
                    onPressed: () {
                      print('ElevatedButton');
                    },
                  ),
                  AntButton(
                    variant: AntButtonVariant.text,
                    color: Color(0xff000000),
                    text: 'Text',
                    onPressed: () {
                      print('ElevatedButton');
                    },
                  ),
                  AntButton(
                    variant: AntButtonVariant.link,
                    color: Color(0xff000000),
                    text: 'Link',
                    onPressed: () {
                      print('ElevatedButton');
                    },
                  ),
                ]),
                Wrap(spacing: 4, runSpacing: 4, children: [
                  AntButton(
                    text: 'Primary',
                    variant: AntButtonVariant.solid,
                    color: Color(0xff1677FF),
                    onPressed: () {
                      print('ElevatedButton');
                    },
                  ),
                  AntButton(
                    variant: AntButtonVariant.outlined,
                    color: Color(0xff1677FF),
                    text: 'Outlined',
                    onPressed: () {
                      print('ElevatedButton');
                    },
                  ),
                  AntButton(
                    variant: AntButtonVariant.filled,
                    color: Color(0xff1677FF),
                    text: 'Filled',
                    onPressed: () {
                      print('ElevatedButton');
                    },
                  ),
                  AntButton(
                    variant: AntButtonVariant.text,
                    color: Color(0xff1677FF),
                    text: 'Text',
                    onPressed: () {
                      print('ElevatedButton');
                    },
                  ),
                  AntButton(
                    variant: AntButtonVariant.link,
                    color: Color(0xff1677FF),
                    text: 'Link',
                    onPressed: () {
                      print('ElevatedButton');
                    },
                  ),
                ]),
                Wrap(spacing: 4, runSpacing: 4, children: [
                  AntButton(
                    text: 'Primary',
                    variant: AntButtonVariant.solid,
                    color: Color(0xffFF4D4F),
                    onPressed: () {
                      print('ElevatedButton');
                    },
                  ),
                  AntButton(
                    variant: AntButtonVariant.outlined,
                    color: Color(0xffFF4D4F),
                    text: 'Outlined',
                    onPressed: () {
                      print('ElevatedButton');
                    },
                  ),
                  AntButton(
                    variant: AntButtonVariant.filled,
                    color: Color(0xffFF4D4F),
                    text: 'Filled',
                    onPressed: () {
                      print('ElevatedButton');
                    },
                  ),
                  AntButton(
                    variant: AntButtonVariant.text,
                    color: Color(0xffFF4D4F),
                    text: 'Text',
                    onPressed: () {
                      print('ElevatedButton');
                    },
                  ),
                  AntButton(
                    variant: AntButtonVariant.link,
                    color: Color(0xffFF4D4F),
                    text: 'Link',
                    onPressed: () {
                      print('ElevatedButton');
                    },
                  ),
                ]),
                Wrap(spacing: 4, runSpacing: 4, children: [
                  AntButton(
                    text: 'Primary',
                    variant: AntButtonVariant.solid,
                    color: Color(0xffEB2F96),
                    onPressed: () {
                      print('ElevatedButton');
                    },
                  ),
                  AntButton(
                    variant: AntButtonVariant.outlined,
                    color: Color(0xffEB2F96),
                    text: 'Outlined',
                    onPressed: () {
                      print('ElevatedButton');
                    },
                  ),
                  AntButton(
                    variant: AntButtonVariant.filled,
                    color: Color(0xffEB2F96),
                    text: 'Filled',
                    onPressed: () {
                      print('ElevatedButton');
                    },
                  ),
                  AntButton(
                    variant: AntButtonVariant.text,
                    color: Color(0xffEB2F96),
                    text: 'Text',
                    onPressed: () {
                      print('ElevatedButton');
                    },
                  ),
                  AntButton(
                    variant: AntButtonVariant.link,
                    color: Color(0xffEB2F96),
                    text: 'Link',
                    onPressed: () {
                      print('ElevatedButton');
                    },
                  ),
                ]),
              ],
            ),
          ),
          DemoBlock(
            title: "大小",
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Wrap(
                  spacing: 4,
                  runSpacing: 4,
                  children: [
                    AntButton(
                      text: 'Large',
                      size: AntSize.large,
                      color: Color(0xffEB2F96),
                      onPressed: () {
                        print('ElevatedButton');
                      },
                    ),
                    AntButton(
                      size: AntSize.large,
                      color: Color(0xffEB2F96),
                      icon: Icon(Icons.add),
                      onPressed: () {
                        print('ElevatedButton');
                      },
                    ),
                    AntButton(
                      shape: AntButtonShape.circle,
                      size: AntSize.large,
                      color: Color(0xffEB2F96),
                      icon: Icon(Icons.add),
                      onPressed: () {
                        print('ElevatedButton');
                      },
                    ),
                    AntButton(
                      text: 'Middle',
                      color: Color(0xffEB2F96),
                      onPressed: () {
                        print('ElevatedButton');
                      },
                    ),
                    AntButton(
                      text: 'Middle',
                      icon: Icon(Icons.add),
                      color: Color(0xffEB2F96),
                      onPressed: () {
                        print('ElevatedButton');
                      },
                    ),
                    AntButton(
                      color: Color(0xffEB2F96),
                      icon: Icon(Icons.add),
                      onPressed: () {
                        print('ElevatedButton');
                      },
                    ),
                    AntButton(
                      color: Color(0xffEB2F96),
                      shape: AntButtonShape.circle,
                      icon: Icon(Icons.add),
                      onPressed: () {
                        print('ElevatedButton');
                      },
                    ),
                    AntButton(
                      text: 'Small',
                      size: AntSize.small,
                      color: Color(0xffEB2F96),
                      onPressed: () {
                        print('ElevatedButton');
                      },
                    ),
                    AntButton(
                      size: AntSize.small,
                      color: Color(0xffEB2F96),
                      icon: Icon(Icons.add),
                      onPressed: () {
                        print('ElevatedButton');
                      },
                    ),
                    AntButton(
                      size: AntSize.small,
                      shape: AntButtonShape.circle,
                      color: Color(0xffEB2F96),
                      icon: Icon(Icons.add),
                      onPressed: () {
                        print('ElevatedButton');
                      },
                    ),
                  ],
                ),
              ],
            ),
          ),
          DemoBlock(
            title: "图形",
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Wrap(spacing: 4, runSpacing: 4, children: [
                  AntButton(
                    shape: AntButtonShape.circle,
                    icon: Icon(Icons.add),
                    onPressed: () {
                      print('ElevatedButton');
                    },
                  ),
                  AntButton(
                    shape: AntButtonShape.round,
                    icon: Icon(Icons.add),
                  )
                ]),
                Wrap(
                  spacing: 4,
                  runSpacing: 4,
                  children: [
                    AntButton(
                      text: 'Middle',
                      shape: AntButtonShape.circle,
                      color: Color(0xffEB2F96),
                      onPressed: () {
                        print('ElevatedButton');
                      },
                    ),
                    AntButton(
                      shape: AntButtonShape.round,
                      color: Color(0xffEB2F96),
                      icon: Icon(Icons.add),
                      onPressed: () {
                        print('ElevatedButton');
                      },
                    ),
                    AntButton(
                      shape: AntButtonShape.circle,
                      size: AntSize.small,
                      color: Color(0xffEB2F96),
                      icon: Icon(Icons.add),
                      onPressed: () {
                        print('ElevatedButton');
                      },
                    ),
                    AntButton(
                      shape: AntButtonShape.circle,
                      size: AntSize.middle,
                      color: Color(0xffEB2F96),
                      icon: Icon(Icons.add),
                      onPressed: () {
                        print('ElevatedButton');
                      },
                    ),
                    AntButton(
                      shape: AntButtonShape.circle,
                      size: AntSize.large,
                      color: Color(0xffEB2F96),
                      icon: Icon(Icons.add),
                      onPressed: () {
                        print('ElevatedButton');
                      },
                    ),
                    AntButton(
                      text: 'Middle',
                      icon: Icon(Icons.add),
                      shape: AntButtonShape.circle,
                      color: Color(0xffEB2F96),
                      onPressed: () {
                        print('ElevatedButton');
                      },
                    ),
                  ],
                ),
              ],

            ),
          ),
          DemoBlock(title: "自定义样式",child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Wrap(
                children: [
                  AntButton(
                    style: StateStyle(
                      style: Style(borderColor: Colors.amber)
                    ),
                    text: "Button",
                  )
                ],
              )
            ],),)
        ]));
  }
}

API

按钮的属性说明如下:

属性说明类型默认值
style按钮样式StateStyle?null
type按钮类型AntButtonType?null
variant设置按钮的变体AntButtonVariant?null
text按钮文字String?null
icon按钮图标Widget?null
color按钮颜色Color?null
shape按钮形状AntButtonShaperound
size按钮大小AntSizemiddle
onPressed点击事件VoidCallback?null
disabled是否禁用bool?false
block是否块级元素bool?false
danger是否危险按钮bool?false
child子组件Widget?null

AntButtonType 枚举值

说明
primary主要按钮
text文本按钮
link链接按钮

AntButtonVariant 枚举值

说明
outlined描边按钮
solid实心按钮
filled填充按钮
text文本按钮
link链接按钮

AntButtonShape 枚举值

说明
circle圆形
round圆角矩形

AntSize 枚举值

说明尺寸
small小尺寸高24px
middle中尺寸高32px
large大尺寸高48px

优先级说明

  1. child 和 text 属性中,优先使用 child 属性作为按钮内容。
  2. 当 icon 和 text 都存在时,按钮会同时显示图标和文字。
  3. 当只有 icon 且没有 text 和 child 时,按钮表现为图标按钮。