Flutter平台通用弹窗插件flutter_platform_alertbox的使用

Flutter平台通用弹窗插件flutter_platform_alertbox的使用

特性

  • 信息提示弹窗
  • 确认提示弹窗
  • 删除项提示弹窗

支持的平台

  • Flutter Android
  • Flutter iOS
  • Flutter Web
  • Flutter 桌面端

安装

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

dependencies:
  flutter_platform_alertbox: <最新版本>

然后导入该库:

import 'package:flutter_platform_alertbox/flutter_platform_alertbox.dart';

如何使用

信息提示弹窗

FlutterPlatFormAlertBox().informationDialogBox(
  context: context,
  title: "提示标题",
  body: "这是提示内容",
  heading: "这是提示头部",
);

确认提示弹窗

FlutterPlatFormAlertBox().confirmationDialogBox(
  context: context,
  title: "确认标题",
  content: "这是确认内容",
  cancelText: "取消",
  submitText: "提交",
  isCancelable: true,
  onSubmit: () {
    debugPrint("提交点击");
  },
);

删除项提示弹窗

FlutterPlatFormAlertBox().deleteItemDialogBox(
  context: context,
  title: "你确定要删除此项目吗?",
  cancelText: "取消",
  submitText: "删除",
  isCancelable: true,
  onSubmit: () {
    debugPrint("提交点击");
  },
);

示例代码

以下是一个完整的示例代码,展示了如何在应用中使用上述三种弹窗。

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

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

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

  [@override](/user/override)
  Widget build(BuildContext context) {
    return const MaterialApp(
      debugShowCheckedModeBanner: false,
      home: AlertBoxExample(),
    );
  }
}

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

  [@override](/user/override)
  State<AlertBoxExample> createState() => _AlertBoxExampleState();
}

class _AlertBoxExampleState extends State<AlertBoxExample> {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("弹窗示例")),
      body: SizedBox(
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            TextButton(
                onPressed: () {
                  FlutterPlatFormAlertBox().informationDialogBox(
                      context: context,
                      title: "提示标题",
                      body: "这是提示内容",
                      heading: "这是提示头部");
                },
                child: Container(
                  color: Colors.yellow,
                  padding: const EdgeInsets.all(8),
                  child: const Text("信息提示弹窗"),
                )),
            SizedBox(
              height: 10,
            ),
            TextButton(
                onPressed: () {
                  FlutterPlatFormAlertBox().confirmationDialogBox(
                      context: context,
                      title: "确认标题",
                      content: "这是确认内容",
                      cancelText: "取消",
                      submitText: "提交",
                      isCancelable: true,
                      onSubmit: () {
                        debugPrint("提交点击");
                      });
                },
                child: Container(
                  color: Colors.yellow,
                  padding: const EdgeInsets.all(8),
                  child: const Text("确认提示弹窗"),
                )),
            SizedBox(
              height: 10,
            ),
            TextButton(
                onPressed: () {
                  FlutterPlatFormAlertBox().deleteItemDialogBox(
                      context: context,
                      title: "你确定要删除此项目吗?",
                      cancelText: "取消",
                      submitText: "删除",
                      isCancelable: true,
                      onSubmit: () {
                        debugPrint("提交点击");
                      });
                },
                child: Container(
                  color: Colors.yellow,
                  padding: const EdgeInsets.all(8),
                  child: const Text("删除项提示弹窗"),
                )),
          ],
        ),
      ),
    );
  }
}

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

1 回复

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


flutter_platform_alertbox 是一个用于在 Flutter 应用中显示平台原生风格弹窗的插件。它允许你在 Android 和 iOS 上使用各自的原生弹窗风格,而不是使用 Flutter 自带的 Material Design 或 Cupertino 风格的弹窗。

安装插件

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

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

然后运行 flutter pub get 来安装插件。

使用插件

  1. 导入插件
    在你的 Dart 文件中导入插件:

    import 'package:flutter_platform_alertbox/flutter_platform_alertbox.dart';
    
  2. 显示弹窗
    你可以使用 FlutterPlatformAlertbox.showAlert 方法来显示一个弹窗。以下是一个简单的示例:

    void showAlert() async {
      final result = await FlutterPlatformAlertbox.showAlert(
        title: '提示',
        message: '这是一个平台原生风格的弹窗',
        positiveButtonText: '确定',
        negativeButtonText: '取消',
      );
    
      if (result == AlertBoxResult.positive) {
        print('用户点击了确定');
      } else if (result == AlertBoxResult.negative) {
        print('用户点击了取消');
      }
    }
    
  3. 处理弹窗结果
    FlutterPlatformAlertbox.showAlert 方法返回一个 AlertBoxResult,你可以根据用户的选择执行不同的操作。

    • AlertBoxResult.positive:用户点击了“确定”按钮。
    • AlertBoxResult.negative:用户点击了“取消”按钮。
    • AlertBoxResult.neutral:用户点击了“中立”按钮(如果有的话)。
  4. 自定义弹窗
    你可以通过传递不同的参数来自定义弹窗的标题、消息、按钮文本等。

    void showCustomAlert() async {
      final result = await FlutterPlatformAlertbox.showAlert(
        title: '自定义弹窗',
        message: '这是一个自定义的弹窗',
        positiveButtonText: '好的',
        negativeButtonText: '不要',
        neutralButtonText: '稍后',
      );
    
      if (result == AlertBoxResult.positive) {
        print('用户点击了好的');
      } else if (result == AlertBoxResult.negative) {
        print('用户点击了不要');
      } else if (result == AlertBoxResult.neutral) {
        print('用户点击了稍后');
      }
    }
    

注意事项

  • flutter_platform_alertbox 插件会自动根据当前平台(Android 或 iOS)显示相应的原生风格弹窗。
  • 如果你在桌面平台上使用该插件,它可能会回退到 Flutter 默认的 Material Design 或 Cupertino 风格的弹窗。

示例代码

以下是一个完整的示例,展示了如何在 Flutter 应用中使用 flutter_platform_alertbox 插件:

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Flutter Platform AlertBox Example'),
        ),
        body: Center(
          child: ElevatedButton(
            onPressed: showAlert,
            child: Text('显示弹窗'),
          ),
        ),
      ),
    );
  }

  void showAlert() async {
    final result = await FlutterPlatformAlertbox.showAlert(
      title: '提示',
      message: '这是一个平台原生风格的弹窗',
      positiveButtonText: '确定',
      negativeButtonText: '取消',
    );

    if (result == AlertBoxResult.positive) {
      print('用户点击了确定');
    } else if (result == AlertBoxResult.negative) {
      print('用户点击了取消');
    }
  }
}
回到顶部