首页 > 解决方案 > 如何将文本添加到我的 floatingActionButton?

问题描述

我正在尝试向我添加文本,floatingActionButton但我无法这样做。如何将文本添加到它没有找到任何方法。

@override
Widget build(BuildContext context) {
  return Scaffold(
    floatingActionButton: FloatingActionButton(
      child: Icon(Icons.add),
      onPressed: () async {
        // when clicked on floating action button prompt to create user
        Navigator.pushReplacement(
          context,
          MaterialPageRoute(
            builder: (BuildContext context) => CreateUser(),
          ),
        );
      },
    ),
  );
}

标签: flutterdart

解决方案


您可以使用FloatingActionButton.extended一次在 FAB 中显示文本和图标。

FloatingActionButton.extended(
  onPressed: () {
    // when clicked on floating action button prompt to create user
    Navigator.pushReplacement(
      context,
      MaterialPageRoute(
        builder: (BuildContext context) => CreateUser(),
      ),
    );
  },
  label: Text('Label'),
  icon: Icon(Icons.add),
),

推荐阅读