Flutter自定义对话框插件custom_flutter_dialog_plus的使用

Flutter自定义对话框插件custom_flutter_dialog_plus的使用

简介

custom_flutter_dialog_plus 是一个功能强大的 Flutter 插件,用于创建自定义对话框。它支持模糊背景、弹出动画以及原生样式的进度对话框。通过这个插件,你可以轻松实现具有动态效果的对话框,而无需手动编写复杂的代码。

以下将详细介绍如何使用该插件及其不同类型的对话框。


使用步骤

1. 添加依赖

pubspec.yaml 文件中添加以下依赖:

dependencies:
  custom_flutter_dialog_plus: ^版本号

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


不同类型的对话框

DDialog

DDialog 是一个基础对话框,可以直接显示而无需额外包装。

示例代码

await DDialog(
  dialogStyle: DialogStyle(titleDivider: true),
  title: Text("Hi, This is d_dialog"),
  content: Text("And here is your content, hoho... "),
  actions: [
    FlatButton(child: Text("You"), onPressed: () {}),
    FlatButton(child: Text("Are"), onPressed: () {}),
    FlatButton(child: Text("Awesome"), onPressed: () {}),
  ],
).show(context);

DAlertDialog

DAlertDialog 是一个可以直接设置背景属性的对话框。

示例代码

await DAlertDialog(
  dialogStyle: DialogStyle(titleDivider: true),
  title: Text("Hi, This is DAlertDialog"),
  content: Text("And here is your content, hoho... "),
  actions: [
    FlatButton(child: Text("You"), onPressed: () {}),
    FlatButton(child: Text("Are"), onPressed: () {}),
    FlatButton(child: Text("Awesome"), onPressed: () {}),
  ],
).show(context);

ProgressDialog

ProgressDialog 显示带有原生风格的进度对话框。

示例代码

ProgressDialog progressDialog = ProgressDialog(
  context,
  message: Text("This is the message"),
  title: Text("This is the title"),
);

// 设置消息和标题
progressDialog.setTitle(Text("Loading"));
progressDialog.setMessage(Text("Please Wait, Injecting your phone with my virus"));
progressDialog.show();

await Future.delayed(Duration(seconds: 5));

// 更改消息
progressDialog.setTitle(Text("Just Kidding"));
progressDialog.setMessage(Text("I mean, virus of love :*"));

await Future.delayed(Duration(seconds: 5));

progressDialog.dismiss();

CustomProgressDialog

CustomProgressDialog 允许自定义加载小部件。

示例代码

CustomProgressDialog progressDialog = CustomProgressDialog(context, blur: 10);

// 设置加载小部件
progressDialog.setLoadingWidget(CircularProgressIndicator(valueColor: AlwaysStoppedAnimation(Colors.red)));
progressDialog.show(useSafeArea: false);

await Future.delayed(Duration(seconds: 5));

// 更改加载小部件
progressDialog.setLoadingWidget(NewLoadingWidget());

await Future.delayed(Duration(seconds: 5));

progressDialog.dismiss();

ZoomDialog

ZoomDialog 提供了一个可以缩放的对话框。

示例代码

await ZoomDialog(
  zoomScale: 5,
  child: Container(
    child: Text("Zoom me!"),
    color: Colors.white,
    padding: EdgeInsets.all(20),
  ),
).show(context);

对话框扩展

你可以直接调用 .show(context) 来显示 Flutter 内置的对话框。

示例代码

DAlertDialog(...).show(context);
SimpleDialog(...).show(context);
Dialog(...).show(context);
CupertinoDialog(...).show(context);
CupertinoAlertDialog(...).show(context);

DialogBackground

DialogBackground 可以用来创建自定义对话框,并轻松显示它们。此外,你还可以更改背景颜色并添加模糊效果。

示例代码

await DialogBackground(
  dialog: DAlertDialog(
    title: Text("Alert Dialog"),
    content: Text("Wohoo.. This is ordinary AlertDialog with Blur background"),
    actions: [
      FlatButton(child: Text("You"), onPressed: () {}),
      FlatButton(child: Text("Are"), onPressed: () {}),
      FlatButton(child: Text("Awesome"), onPressed: () {}),
    ],
  ),
).show(context);

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

1 回复

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


custom_flutter_dialog_plus 是一个用于 Flutter 的自定义对话框插件,它允许开发者轻松创建和显示自定义对话框。以下是如何使用 custom_flutter_dialog_plus 插件的详细步骤。

1. 添加依赖

首先,你需要在 pubspec.yaml 文件中添加 custom_flutter_dialog_plus 插件的依赖。

dependencies:
  flutter:
    sdk: flutter
  custom_flutter_dialog_plus: ^1.0.0  # 请使用最新版本

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

2. 导入插件

在你的 Dart 文件中导入 custom_flutter_dialog_plus 插件。

import 'package:custom_flutter_dialog_plus/custom_flutter_dialog_plus.dart';

3. 创建自定义对话框

你可以使用 CustomDialog 类来创建自定义对话框。以下是一个简单的示例:

void showCustomDialog(BuildContext context) {
  CustomDialog(
    context: context,
    title: '自定义对话框',
    content: '这是一个自定义对话框的示例内容。',
    positiveButtonText: '确定',
    negativeButtonText: '取消',
    onPositivePressed: () {
      // 点击确定按钮后的操作
      Navigator.of(context).pop();
    },
    onNegativePressed: () {
      // 点击取消按钮后的操作
      Navigator.of(context).pop();
    },
  ).show();
}

4. 显示对话框

你可以在需要显示对话框的地方调用 showCustomDialog 方法。

ElevatedButton(
  onPressed: () {
    showCustomDialog(context);
  },
  child: Text('显示对话框'),
);

5. 自定义对话框样式

CustomDialog 类提供了多个参数来自定义对话框的外观和行为。以下是一些常用的参数:

  • title: 对话框的标题。
  • content: 对话框的内容。
  • positiveButtonText: 确定按钮的文本。
  • negativeButtonText: 取消按钮的文本。
  • onPositivePressed: 确定按钮的点击事件。
  • onNegativePressed: 取消按钮的点击事件。
  • backgroundColor: 对话框的背景颜色。
  • titleTextStyle: 标题的文本样式。
  • contentTextStyle: 内容的文本样式。
  • buttonTextStyle: 按钮的文本样式。

6. 完整示例

以下是一个完整的示例,展示了如何使用 custom_flutter_dialog_plus 插件创建和显示自定义对话框。

import 'package:flutter/material.dart';
import 'package:custom_flutter_dialog_plus/custom_flutter_dialog_plus.dart';

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Custom Dialog Example'),
        ),
        body: Center(
          child: ElevatedButton(
            onPressed: () {
              showCustomDialog(context);
            },
            child: Text('显示对话框'),
          ),
        ),
      ),
    );
  }
}

void showCustomDialog(BuildContext context) {
  CustomDialog(
    context: context,
    title: '自定义对话框',
    content: '这是一个自定义对话框的示例内容。',
    positiveButtonText: '确定',
    negativeButtonText: '取消',
    onPositivePressed: () {
      // 点击确定按钮后的操作
      Navigator.of(context).pop();
    },
    onNegativePressed: () {
      // 点击取消按钮后的操作
      Navigator.of(context).pop();
    },
    backgroundColor: Colors.white,
    titleTextStyle: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
    contentTextStyle: TextStyle(fontSize: 16),
    buttonTextStyle: TextStyle(fontSize: 16, color: Colors.blue),
  ).show();
}
回到顶部