Flutter自定义弹窗插件custom_popup_dialog的使用

Flutter自定义弹窗插件custom_popup_dialog的使用

custom_popup_dialog 是一个 Flutter 包,它提供了一个可定制的对话框模板。此包允许你创建带有可选 Lottie 动画、背景模糊效果等的美丽且可定制的对话框。

特性

  • 带有 Lottie 动画的可定制对话框
  • 可选的背景模糊效果或暗化背景
  • 可定制的按钮文本和颜色
  • 易于集成到任何 Flutter 应用程序中

开始使用

安装

在你的 pubspec.yaml 文件中添加 custom_popup_dialog

dependencies:
  custom_popup_dialog: ^0.0.3

安装依赖

运行以下命令安装依赖项:

flutter pub get

使用

下面是一个如何在你的 Flutter 应用程序中使用 CustomDialog 小部件的例子:

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

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: const HomeScreen(),
    );
  }
}

class HomeScreen extends StatelessWidget {
  const HomeScreen({super.key});

  // 显示对话框的方法
  void showDialogTemplate(BuildContext context, String? animationPath, String message, {bool blurBackground = true}) {
    showDialog(
      context: context,
      builder: (context) => CustomDialog(
        message: message,
        animationWidth: 120,
        animationHeight: 120,
        headerAnimation: animationPath,
        btnText: '继续',
        bgColor: Colors.white,
        textColor: Colors.black,
        btnColor: Colors.blue,
        btnTextColor: Colors.white,
        blurBackground: blurBackground,
        alternativeBgColor: Colors.black.withOpacity(0.7),
        onBtnPressed: () {
          Navigator.of(context).pop();
        },
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text(
          '自定义警告对话框',
          style: TextStyle(color: Colors.white),
        ),
        centerTitle: true,
        backgroundColor: Colors.blueAccent,
      ),
      body: SafeArea(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            CustomButton(
              bgColor: Colors.green,
              fgColor: Colors.white,
              text: '显示成功对话框',
              onClick: () {
                showDialogTemplate(context, 'assets/lottie/success.json', '操作成功!', blurBackground: true);
              },
            ),
            const SizedBox(height: 20),
            CustomButton(
              bgColor: Colors.red,
              fgColor: Colors.white,
              text: '显示错误对话框',
              onClick: () {
                showDialogTemplate(context, 'assets/lottie/error.json', '发生错误!', blurBackground: false);
              },
            ),
            const SizedBox(height: 20),
            CustomButton(
              bgColor: Colors.orange,
              fgColor: Colors.white,
              text: '显示警告对话框',
              onClick: () {
                showDialogTemplate(context, 'assets/lottie/alert.json', '这是一个警告!', blurBackground: true);
              },
            ),
            const SizedBox(height: 20),
            CustomButton(
              bgColor: Colors.blueAccent,
              fgColor: Colors.white,
              text: '显示信息对话框',
              onClick: () {
                showDialogTemplate(context, 'assets/lottie/info.json', '这里是一些信息。', blurBackground: false);
              },
            )
          ],
        ),
      ),
    );
  }
}

// 自定义按钮小部件
class CustomButton extends StatelessWidget {
  const CustomButton({
    super.key,
    required this.bgColor,
    required this.fgColor,
    required this.text,
    required this.onClick,
  });

  final Color bgColor;
  final Color fgColor;
  final String text;
  final VoidCallback onClick;

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        SizedBox(
          width: 200,
          child: ElevatedButton(
            onPressed: onClick,
            style: ElevatedButton.styleFrom(
              backgroundColor: bgColor,
              foregroundColor: fgColor,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(5),
              ),
              elevation: 5,
            ),
            child: Text(text),
          ),
        ),
      ],
    );
  }
}

自定义选项

CustomDialog 小部件通过其构造函数参数提供了各种自定义选项:

  • message: 在对话框中显示的消息。
  • btnText: 按钮上的文本。
  • maxLines: 消息文本的最大行数。
  • textAlign: 消息文本的对齐方式。
  • animationWidth: Lottie 动画的宽度。
  • animationHeight: Lottie 动画的高度。
  • bgColor: 对话框的背景色。
  • textColor: 消息文本的颜色。
  • btnColor: 按钮的背景色。
  • btnTextColor: 按钮文本的颜色。
  • animationDuration: Lottie 动画的持续时间。
  • headerAnimation: Lottie 动画文件的路径。
  • onBtnPressed: 按钮被按下时的回调函数。
  • blurBackground: 是否应用背景模糊效果。
  • blurAmount: 如果 blurBackground 为 true,则应用的模糊量。
  • alternativeBgColor: 如果 blurBackground 为 false,则使用的背景色。

示例用法

要显示一个带有成功消息和 Lottie 动画的自定义对话框:

showDialogTemplate(context, 'assets/lottie/success.json', '操作成功!', blurBackground: true);

要显示一个带有错误消息且不带背景模糊的自定义对话框:

showDialogTemplate(context, 'assets/lottie/error.json', '发生错误!', blurBackground: false);

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

1 回复

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


custom_popup_dialog 是一个用于在 Flutter 中创建自定义弹窗的插件。它允许你轻松地创建和显示自定义的弹窗,而无需手动管理弹窗的状态和样式。以下是如何使用 custom_popup_dialog 插件的基本步骤:

1. 添加依赖

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

dependencies:
  flutter:
    sdk: flutter
  custom_popup_dialog: ^latest_version

然后运行 flutter pub get 来获取依赖。

2. 导入插件

在你的 Dart 文件中导入 custom_popup_dialog 插件:

import 'package:custom_popup_dialog/custom_popup_dialog.dart';

3. 创建自定义弹窗

你可以使用 CustomPopupDialog 类来创建和显示自定义弹窗。以下是一个简单的示例:

class CustomPopupExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Custom Popup Dialog Example'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            showCustomPopupDialog(context);
          },
          child: Text('Show Custom Popup'),
        ),
      ),
    );
  }

  void showCustomPopupDialog(BuildContext context) {
    CustomPopupDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text('Custom Popup'),
          content: Text('This is a custom popup dialog.'),
          actions: <Widget>[
            TextButton(
              onPressed: () {
                Navigator.of(context).pop();
              },
              child: Text('Close'),
            ),
          ],
        );
      },
    ).show();
  }
}

4. 解释

  • CustomPopupDialog 是一个可以自定义的弹窗组件。
  • context 参数是当前 BuildContext,用于显示弹窗。
  • builder 参数是一个函数,返回你想要显示的弹窗内容。在这个例子中,我们返回了一个 AlertDialog 组件。
  • show() 方法用于显示弹窗。

5. 自定义弹窗样式

你可以通过修改 builder 返回的组件来自定义弹窗的样式。例如,你可以使用 ContainerColumnRow 等布局组件来创建更复杂的弹窗。

void showCustomPopupDialog(BuildContext context) {
  CustomPopupDialog(
    context: context,
    builder: (BuildContext context) {
      return Container(
        width: 300,
        height: 200,
        padding: EdgeInsets.all(20),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(10),
        ),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text('Custom Popup', style: TextStyle(fontSize: 20)),
            SizedBox(height: 20),
            Text('This is a fully custom popup dialog.'),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: () {
                Navigator.of(context).pop();
              },
              child: Text('Close'),
            ),
          ],
        ),
      );
    },
  ).show();
}
回到顶部