Flutter弹窗提示插件the_flutter_alert的使用

Flutter弹窗提示插件the_flutter_alert的使用

本README描述了该插件的用法。如果您将此插件发布到pub.dev,则此README的内容将出现在您的插件页面上。

使用

首先,在您的项目中添加以下导入语句:

import 'package:the_flutter_alert/the_flutter_alert.dart';

然后,您可以使用showFlutterAlertNotification函数来显示弹窗提示。以下是一个简单的示例:

FlutterAlert.showFlutterAlertNotification(
  context,
  'Hello World',
  type: FlutterAlertType.info,
);

完整示例

下面是一个完整的示例,展示了如何在应用中使用the_flutter_alert插件。

示例代码

import 'package:flutter/material.dart';
import 'package:the_flutter_alert/the_flutter_alert.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(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

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

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  // 显示通知的方法
  _showNotification(String content,
      {FlutterAlertType type = FlutterAlertType.info}) {
    FlutterAlert.showFlutterAlertNotification(
      context,
      content,
      type: type,
    );
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            // 显示信息弹窗
            ElevatedButton(
                onPressed: () {
                  _showNotification('Showing info alert');
                },
                child: const Text('显示信息弹窗')),

            // 显示成功弹窗
            ElevatedButton(
                onPressed: () {
                  _showNotification('Showing success alert',
                      type: FlutterAlertType.success);
                },
                child: const Text('显示成功弹窗')),

            // 显示错误弹窗
            ElevatedButton(
                onPressed: () {
                  _showNotification('Showing error alert',
                      type: FlutterAlertType.error);
                },
                child: const Text('显示错误弹窗')),

            // 显示警告弹窗
            ElevatedButton(
                onPressed: () {
                  _showNotification('Showing warning alert',
                      type: FlutterAlertType.warning);
                },
                child: const Text('显示警告弹窗')),
          ],
        ),
      ),
    );
  }
}

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

1 回复

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


the_flutter_alert 是一个用于在 Flutter 应用中显示弹窗提示的插件。它提供了简单易用的 API,可以快速创建各种类型的弹窗,如警告、确认、输入框等。

安装

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

dependencies:
  flutter:
    sdk: flutter
  the_flutter_alert: ^1.0.0

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

基本使用

1. 显示基本弹窗

你可以使用 Alert 类来显示一个简单的弹窗:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Flutter Alert Example'),
        ),
        body: Center(
          child: ElevatedButton(
            onPressed: () {
              Alert(
                context: context,
                type: AlertType.info,
                title: "提示",
                desc: "这是一个简单的提示弹窗。",
                buttons: [
                  DialogButton(
                    child: Text(
                      "确定",
                      style: TextStyle(color: Colors.white, fontSize: 20),
                    ),
                    onPressed: () => Navigator.pop(context),
                    width: 120,
                  )
                ],
              ).show();
            },
            child: Text('显示弹窗'),
          ),
        ),
      ),
    );
  }
}

2. 显示不同类型的弹窗

AlertType 支持多种类型的弹窗,如 info, warning, error, success 等。

Alert(
  context: context,
  type: AlertType.warning,
  title: "警告",
  desc: "这是一个警告弹窗。",
  buttons: [
    DialogButton(
      child: Text(
        "确定",
        style: TextStyle(color: Colors.white, fontSize: 20),
      ),
      onPressed: () => Navigator.pop(context),
      width: 120,
    )
  ],
).show();

3. 显示带输入框的弹窗

你可以使用 Alert 类来显示一个带输入框的弹窗:

Alert(
  context: context,
  type: AlertType.input,
  title: "输入框",
  desc: "请输入一些内容:",
  buttons: [
    DialogButton(
      child: Text(
        "提交",
        style: TextStyle(color: Colors.white, fontSize: 20),
      ),
      onPressed: () {
        // 处理输入内容
        Navigator.pop(context);
      },
      width: 120,
    )
  ],
).show();

4. 自定义弹窗

你可以通过 Alert 类的 style 参数来自定义弹窗的样式:

Alert(
  context: context,
  style: AlertStyle(
    isCloseButton: false,
    isOverlayTapDismiss: false,
    titleStyle: TextStyle(color: Colors.red),
    descStyle: TextStyle(color: Colors.blue),
    animationType: AnimationType.fromTop,
    animationDuration: Duration(milliseconds: 500),
  ),
  type: AlertType.info,
  title: "自定义样式",
  desc: "这是一个自定义样式的弹窗。",
  buttons: [
    DialogButton(
      child: Text(
        "确定",
        style: TextStyle(color: Colors.white, fontSize: 20),
      ),
      onPressed: () => Navigator.pop(context),
      width: 120,
    )
  ],
).show();
回到顶部