Flutter对话框插件dialog_box的使用

Flutter对话框插件dialog_box的使用

特性

dialog_box 插件可以帮助你快速创建具有更好UI的对话框。通过该插件,你可以使用简短的代码来实现不同的对话框样式。

示例代码

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Dialog Box Example'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              ElevatedButton(
                onPressed: () {
                  // 显示带有错误信息的对话框
                  customShowDialog(
                    context,
                    "Do you want to Logout the app?",
                    "Oops..",
                    ok: () {
                      print("Ok button pressed");
                    },
                    cancel: () {
                      print("Cancel button pressed");
                    },
                  );
                },
                child: Text('Oops.. Dialog'),
              ),
              ElevatedButton(
                onPressed: () {
                  // 显示带有成功信息的对话框
                  customShowDialog(
                    context,
                    "Do you want to Logout the app?",
                    "Success",
                    ok: () {
                      print("Ok button pressed");
                    },
                    cancel: () {
                      print("Cancel button pressed");
                    },
                  );
                },
                child: Text('Success Dialog'),
              ),
              ElevatedButton(
                onPressed: () {
                  // 显示带有警告信息的对话框
                  customShowDialog(
                    context,
                    "Do you want to Logout the app?",
                    "Warning",
                    ok: () {
                      print("Ok button pressed");
                    },
                    cancel: () {
                      print("Cancel button pressed");
                    },
                  );
                },
                child: Text('Warning Dialog'),
              ),
              ElevatedButton(
                onPressed: () {
                  // 显示带有信息提示的对话框
                  customShowDialog(
                    context,
                    "Do you want to Logout the app?",
                    "Info",
                    ok: () {
                      print("Ok button pressed");
                    },
                    cancel: () {
                      print("Cancel button pressed");
                    },
                  );
                },
                child: Text('Info Dialog'),
              ),
              ElevatedButton(
                onPressed: () {
                  // 显示带有确认信息的对话框
                  customShowDialog(
                    context,
                    "Do you want to Logout the app?",
                    "Sure",
                    ok: () {
                      print("Ok button pressed");
                    },
                    cancel: () {
                      print("Cancel button pressed");
                    },
                  );
                },
                child: Text('Sure Dialog'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

使用说明

安装

pubspec.yaml 文件中添加依赖:

dependencies:
  dialog_box: ^0.1.0

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

1 回复

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


dialog_box 是 Flutter 中一个用于创建自定义对话框的插件。它提供了多种对话框样式,包括基本对话框、确认对话框、输入对话框等,并且支持高度自定义。下面是 dialog_box 插件的基本使用方法。

1. 添加依赖

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

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

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

2. 基本使用

2.1 导入包

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

import 'package:dialog_box/dialog_box.dart';

2.2 显示一个基本对话框

你可以使用 DialogBox 类来显示一个简单的对话框:

DialogBox.show(
  context,
  title: "提示",
  content: "这是一个基本对话框",
  positiveButtonText: "确定",
  negativeButtonText: "取消",
  onPositivePressed: () {
    print("确定按钮被点击");
  },
  onNegativePressed: () {
    print("取消按钮被点击");
  },
);

2.3 显示一个确认对话框

确认对话框通常用于获取用户的确认操作:

DialogBox.confirm(
  context,
  title: "确认",
  content: "你确定要执行此操作吗?",
  positiveButtonText: "确定",
  negativeButtonText: "取消",
  onPositivePressed: () {
    print("用户确认了操作");
  },
  onNegativePressed: () {
    print("用户取消了操作");
  },
);

2.4 显示一个输入对话框

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

DialogBox.input(
  context,
  title: "输入",
  hintText: "请输入内容",
  positiveButtonText: "提交",
  negativeButtonText: "取消",
  onPositivePressed: (String value) {
    print("用户输入的内容: $value");
  },
  onNegativePressed: () {
    print("用户取消了输入");
  },
);

3. 自定义对话框

dialog_box 插件允许你自定义对话框的外观和行为。你可以通过传递 DialogBoxOptions 对象来定制对话框的样式:

DialogBox.show(
  context,
  options: DialogBoxOptions(
    title: "自定义对话框",
    content: "这是一个自定义样式的对话框",
    positiveButtonText: "确定",
    negativeButtonText: "取消",
    titleStyle: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
    contentStyle: TextStyle(fontSize: 16),
    positiveButtonStyle: ButtonStyle(
      backgroundColor: MaterialStateProperty.all(Colors.green),
    ),
    negativeButtonStyle: ButtonStyle(
      backgroundColor: MaterialStateProperty.all(Colors.red),
    ),
  ),
  onPositivePressed: () {
    print("确定按钮被点击");
  },
  onNegativePressed: () {
    print("取消按钮被点击");
  },
);
回到顶部