Flutter弹窗提示插件alert_box_flutter的使用

Flutter弹窗提示插件alert_box_flutter的使用

自定义弹窗 alert_box_flutter 可以帮助你创建自定义弹窗。

特性

该插件非常灵活且易于实现。

开始使用

无需任何特殊操作即可开始使用。

使用方法

只需导入包并使用它。

await CustomAlertBox.showCustomAlertBox(
  context: context,
  headingWidget: const Text('Alert'),
  descWidget: const Text('My custom alert box, used from example!!'),
  firstButtonClick: () {},
  secondButtonClick: () {},
  secondButtonColor: Colors.orange,
);

示例代码

以下是完整的示例代码:

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

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

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

  // 这个小部件是你的应用程序的根。它是无状态的,意味着
  // 它的状态由一个包含影响其外观的字段的状态对象定义。
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // 这是你的应用程序的主题。
        //
        // 尝试运行你的应用程序并使用 "flutter run"。你会看到
        // 应用程序有一个蓝色的工具栏。然后,在不退出应用的情况下,尝试
        // 将下面的主色调更改为 Colors.green 并调用
        // "热重载"(在运行 "flutter run" 的控制台中按 "r",
        // 或者只是保存更改以在 Flutter IDE 中进行 "热重载")。
        // 注意计数器没有重置为零;应用程序
        // 没有重启。
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  // 这个小部件是你的应用程序的主页。它是有状态的,意味着
  // 它有一个状态对象(定义在下面),该对象包含影响
  // 它的外观的字段。
  //
  // 这个类是状态的配置。它保存了由父级(在这个例子中是App小部件)提供的值
  // (在这种情况下是标题)并用于状态的构建方法。
  // 小部件子类中的字段总是被标记为 "final"。
  
  final String title;

  [@override](/user/override)
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final int _counter = 0;

  [@override](/user/override)
  Widget build(BuildContext context) {
    // 每次调用setState时,此方法都会重新运行,例如通过上面的_incrementCounter方法。
    //
    // Flutter框架经过优化,可以快速重新运行构建方法,
    // 因此你可以重新构建需要更新的任何内容,而不是逐个改变小部件实例。
    return Scaffold(
      appBar: AppBar(
        // 这里我们从App.build方法创建的MyHomePage对象中获取值,
        // 并将其用于设置我们的appbar标题。
        title: Text(widget.title),
      ),
      body: Center(
        // Center 是一个小部件布局。它接受一个子项并将其定位
        // 在父项的中间。
        child: Column(
          // Column 也是一个布局小部件。它接受一个子项列表
          // 并垂直排列它们。默认情况下,它根据其
          // 子项水平调整自身大小,并试图与父项一样高。
          //
          // 调用 "调试绘制"(在控制台中按 "p",选择Android Studio中的
          // "切换调试绘制" 动作,或Visual Studio Code中的
          // "切换调试绘制" 命令)可以看到每个小部件的线框图。
          //
          // Column 有几个属性可以控制它的大小和
          // 如何定位其子项。在这里,我们使用mainAxisAlignment将子项垂直居中;
          // 主轴是垂直的(交叉轴是水平的)。
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () async {
          await CustomAlertBox.showCustomAlertBox(
            context: context,
            headingWidget: const Text('Alert'),
            descWidget: const Text('My custom alert box, used from example!!'),
            firstButtonClick: () {},
            secondButtonClick: () {},
            secondButtonColor: Colors.orange,
          );
        },
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}

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

1 回复

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


alert_box_flutter 是一个用于在 Flutter 应用中显示自定义弹窗的插件。它提供了灵活的配置选项,允许你创建各种类型的弹窗,包括消息提示、确认对话框、输入对话框等。

安装

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

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

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

基本使用

1. 显示一个简单的消息弹窗

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Alert Box Example'),
        ),
        body: Center(
          child: ElevatedButton(
            onPressed: () {
              AlertBox(
                context: context,
                title: '提示',
                message: '这是一个简单的消息弹窗',
                buttonText: '确定',
              ).show();
            },
            child: Text('显示弹窗'),
          ),
        ),
      ),
    );
  }
}

2. 显示一个确认弹窗

ElevatedButton(
  onPressed: () {
    AlertBox(
      context: context,
      title: '确认',
      message: '你确定要执行此操作吗?',
      buttonText: '确定',
      cancelButtonText: '取消',
      onConfirm: () {
        // 用户点击确定按钮后的操作
        print('用户点击了确定');
      },
      onCancel: () {
        // 用户点击取消按钮后的操作
        print('用户点击了取消');
      },
    ).show();
  },
  child: Text('显示确认弹窗'),
);

3. 显示一个带输入框的弹窗

ElevatedButton(
  onPressed: () {
    AlertBox(
      context: context,
      title: '输入',
      message: '请输入你的名字',
      inputHint: '名字',
      buttonText: '提交',
      onConfirm: (String value) {
        // 用户提交输入后的操作
        print('用户输入了: $value');
      },
    ).show();
  },
  child: Text('显示输入弹窗'),
);

自定义样式

alert_box_flutter 允许你通过传递自定义的 AlertBoxStyle 来调整弹窗的外观:

AlertBox(
  context: context,
  title: '自定义样式',
  message: '这是一个自定义样式的弹窗',
  buttonText: '确定',
  style: AlertBoxStyle(
    titleStyle: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
    messageStyle: TextStyle(fontSize: 16, color: Colors.grey),
    backgroundColor: Colors.white,
    buttonColor: Colors.blue,
    buttonTextStyle: TextStyle(color: Colors.white),
  ),
).show();
回到顶部