Flutter对话框插件f_dialog的使用

Flutter对话框插件f_dialog的使用

特性

  • 信息对话框(Info Dialog)
  • 进度对话框(Progress Dialog)
  • 成功对话框(Success Dialog)
  • 失败对话框(Fail Dialog)
  • 错误对话框(Error Dialog)
  • 自定义操作对话框(Custom Action Dialog)

这些对话框可以在不同的屏幕中使用,并根据对话框类型进行关闭。

对话框内容支持本地化。

注意事项

当你使用自定义对话框时,无需在MaterialApp中添加本地化。你可以在使用自定义对话框的地方从你的本地化变量或文件中添加这些字段。

图片

图片1
图片2
图片3
图片4
图片5
图片6

使用方法

main.dart文件中:

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return FDialog(
      theme: ThemeData(),
      child: MaterialApp(
        builder: (context, child) {
          FDialog.of(context)
              .setLocalization(const Localization(
            errorContent: 'Something went wrong',
            errorTitle: 'Opps!',
            failedContent: 'Your process failed.',
            failedTitle: 'Failed',
            infoContent: 'This is a info body',
            onCancelText: 'Cancel',
            infoTitle: 'Info',
            onConfirmText: 'OK',
            processingContent: 'Processing...',
            processingTitle: 'Process',
            successTitle: 'Success',
            successContent:
                'Your process has been successfully completed',
          ));
          return const Scaffold(
            body: MyHomePage(),
          );
        },
      ),
    );
  }
}

创建对话框

要创建一个对话框,可以使用以下代码:

FDialog.of(context)?.show(
  context,
  dialogType: DialogTypeEnums.success,
  icon: const Icon(
    Icons.safety_check_rounded,
  ),
  customPadding:
      const EdgeInsets.all(20),
);

关闭最后一个显示的对话框

要关闭屏幕上最后显示的对话框,可以使用以下代码:

FDialog.close(context);

关闭特定的对话框

要关闭屏幕上显示的特定对话框,可以使用以下代码:

FDialog.close(context, DialogTypeEnums.progress);

创建自定义对话框

要创建自定义对话框,可以使用以下代码:

FDialog.of(context).custom(
  context,
  icon: const Icon(
    Icons.info_outline_rounded,
    color: Colors.red,
  ),
  backgroundColor: Colors.white,
  isDismissible: false,
  customDialogTitle: 'Custom Dialog Title',
  customDialogBody: 'Custom Dialog Body',
  onConfirmText: 'OK',
  onCancelText: 'NO',
  onConfirmButtonStyle: ButtonStyle(
    backgroundColor: MaterialStateProperty.all(Colors.green)),
  onCancelButtonStyle: ButtonStyle(
    backgroundColor: MaterialStateProperty.all(Colors.red)),
  contentTextStyle: const TextStyle(
    fontWeight: FontWeight.bold,
    color: Colors.blue,
    fontSize: 20),
  titleTextStyle: const TextStyle(
    fontWeight: FontWeight.bold,
    color: Colors.red,
    fontSize: 20),
  onCancel: () {
    Navigator.pop(context);
  },
  onConfirm: () {
    Navigator.pop(context);
  },
);

更多关于Flutter对话框插件f_dialog的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter对话框插件f_dialog的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


f_dialog 是一个用于 Flutter 的对话框插件,它提供了多种预定义的对话框样式,使得在 Flutter 应用中快速创建和显示对话框变得非常方便。以下是使用 f_dialog 插件的基本步骤和示例。

1. 添加依赖

首先,在你的 pubspec.yaml 文件中添加 f_dialog 插件的依赖:

dependencies:
  flutter:
    sdk: flutter
  f_dialog: ^0.1.6  # 请使用最新版本

然后运行 flutter pub get 来安装依赖。

2. 导入包

在你的 Dart 文件中导入 f_dialog 包:

import 'package:f_dialog/f_dialog.dart';

3. 使用 f_dialog 显示对话框

f_dialog 提供了多种对话框类型,包括消息对话框、确认对话框、输入对话框等。以下是一些常见的使用示例。

3.1 显示消息对话框

消息对话框用于显示一条信息给用户,通常只有一个“确定”按钮。

FDialog.info(
  context: context,
  info: '这是一个消息对话框',
);

3.2 显示确认对话框

确认对话框通常用于让用户确认某个操作,它包含“确认”和“取消”两个按钮。

FDialog.confirm(
  context: context,
  title: '确认',
  info: '你确定要执行此操作吗?',
  onConfirm: () {
    // 用户点击确认后的操作
    print('用户确认了操作');
  },
  onCancel: () {
    // 用户点击取消后的操作
    print('用户取消了操作');
  },
);

3.3 显示输入对话框

输入对话框允许用户输入一些文本。

FDialog.input(
  context: context,
  title: '输入',
  hintText: '请输入内容',
  onConfirm: (value) {
    // 用户点击确认后的操作,value 是用户输入的内容
    print('用户输入的内容: $value');
  },
);

3.4 显示自定义对话框

f_dialog 还允许你自定义对话框的内容。

FDialog.custom(
  context: context,
  child: Container(
    width: 200,
    height: 200,
    color: Colors.white,
    child: Center(
      child: Text('这是一个自定义对话框'),
    ),
  ),
);

4. 其他功能

f_dialog 还提供了其他一些功能,例如:

  • FDialog.success:显示成功提示对话框。
  • FDialog.error:显示错误提示对话框。
  • FDialog.warning:显示警告提示对话框。

你可以根据具体需求选择合适的对话框类型。

5. 自定义样式

f_dialog 允许你通过传递不同的参数来自定义对话框的样式,例如颜色、字体、按钮文本等。

FDialog.info(
  context: context,
  info: '这是一个自定义样式的消息对话框',
  confirmText: '好的',
  confirmTextColor: Colors.blue,
  backgroundColor: Colors.white,
);
回到顶部