Flutter轻量级弹窗插件tiny_alert的使用

发布于 1周前 作者 zlyuanteng 来自 Flutter

Flutter轻量级弹窗插件tiny_alert的使用

tiny_alert 是一个用于在 Flutter 应用中显示轻量级弹窗的插件。它提供了多种类型的弹窗,包括成功、信息、警告、错误、确认和加载提示等。

安装

首先,在 pubspec.yaml 文件中添加 tiny_alert 依赖:

dependencies:
  tiny_alert: <version>

然后运行以下命令以获取包:

$ flutter pub get

使用示例

成功弹窗

TinyAlert.success(
  context,
  title: "Success!",
  message: "This is a success message!",
);

信息弹窗

TinyAlert.info(
  context,
  title: "Info!",
  message: "This is a info message!",
);

警告弹窗

TinyAlert.warning(
  context,
  title: "Warning!",
  message: "This is a warning message!",
);

错误弹窗

TinyAlert.error(
  context,
  title: "Error!",
  message: "This is a error message!",
);

确认弹窗

TinyAlert.confirm(
  context,
  title: "Confirmation!",
  message: "This is a confirmation message!",
  onConfirm: () {
    Navigator.pop(context);
  },
);

加载弹窗

TinyAlert.progress(
  context,
  label: "Loading...",
  barrierDismissible: true, // 是否可以通过点击背景关闭弹窗,默认为false
);

Snackbar 弹窗

TinyAlert.snackbar(
  context,
  "This is snackbar message!",
);

完整示例代码

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

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

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

  [@override](/user/override)
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  [@override](/user/override)
  void initState() {
    super.initState();
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      darkTheme: ThemeData.dark(useMaterial3: true),
      home: const HomePage(),
    );
  }
}

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

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Tiny Alert Demo'),
      ),
      body: SizedBox(
        width: double.infinity,
        child: Column(
          mainAxisSize: MainAxisSize.max,
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            ElevatedButton(
              onPressed: () {
                TinyAlert.success(
                  context,
                  title: "Success!",
                  message:
                      "This is a success message! \nAute nostrud Lorem pariatur aliqua ut reprehenderit esse reprehenderit aliqua officia pariatur dolore aliqua cupidatat.",
                );
              },
              style: ElevatedButton.styleFrom(
                backgroundColor: Colors.green,
                foregroundColor: Colors.white,
              ),
              child: const Text('Success!'),
            ),
            ElevatedButton(
              onPressed: () {
                TinyAlert.info(
                  context,
                  title: "Info!",
                  message: "This is a info message!",
                );
              },
              style: ElevatedButton.styleFrom(
                backgroundColor: Colors.blue,
                foregroundColor: Colors.white,
              ),
              child: const Text('Info!'),
            ),
            ElevatedButton(
              onPressed: () {
                TinyAlert.warning(
                  context,
                  title: "Warning!",
                  message: "This is a warning message!",
                );
              },
              style: ElevatedButton.styleFrom(
                backgroundColor: Colors.amber[700],
                foregroundColor: Colors.white,
              ),
              child: const Text('Warning!'),
            ),
            ElevatedButton(
              onPressed: () {
                TinyAlert.error(
                  context,
                  title: "Error!",
                  message: "This is a error message!",
                );
              },
              style: ElevatedButton.styleFrom(
                backgroundColor: Colors.red,
                foregroundColor: Colors.white,
              ),
              child: const Text('Error!'),
            ),
            ElevatedButton(
              onPressed: () {
                TinyAlert.confirm(
                  context,
                  title: "Confirmation?",
                  message: "This is a confirmation message!",
                  onConfirm: () {
                    Navigator.pop(context);
                  },
                );
              },
              style: ElevatedButton.styleFrom(
                backgroundColor: Colors.tealAccent[700],
                foregroundColor: Colors.white,
              ),
              child: const Text('Confirm!'),
            ),
            ElevatedButton(
              onPressed: () {
                TinyAlert.progress(context, label: "Loading...", barrierDismissible: true);
              },
              style: ElevatedButton.styleFrom(
                backgroundColor: Colors.blue,
                foregroundColor: Colors.white,
              ),
              child: const Text('Loading!'),
            ),
            ElevatedButton(
              onPressed: () {
                TinyAlert.snackbar(
                  context,
                  "This is snackbar message!",
                );
              },
              style: ElevatedButton.styleFrom(
                backgroundColor: Colors.indigo[400],
                foregroundColor: Colors.white,
              ),
              child: const Text('Snackbar!'),
            ),
          ],
        ),
      ),
    );
  }
}

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

1 回复

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


当然,以下是关于如何在Flutter中使用tiny_alert插件来创建轻量级弹窗的示例代码。首先,确保你已经在pubspec.yaml文件中添加了tiny_alert依赖:

dependencies:
  flutter:
    sdk: flutter
  tiny_alert: ^latest_version  # 请替换为最新版本号

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

接下来是一个简单的示例代码,展示如何使用tiny_alert插件来显示一个弹窗:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Tiny Alert Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Tiny Alert Demo'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '0',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          _showTinyAlert(context);
        },
        tooltip: 'Show Alert',
        child: Icon(Icons.alert),
      ),
    );
  }

  void _showTinyAlert(BuildContext context) {
    TinyAlert.show(
      context: context,
      type: TinyAlertType.success, // 可以是 success, warning, error, info
      title: 'Success!',
      message: 'This is a success alert.',
      onConfirm: () {
        // 用户点击确认按钮后的回调
        print('Alert confirmed!');
      },
      onCancel: () {
        // 用户点击取消按钮后的回调(如果弹窗有取消按钮)
        print('Alert cancelled!');
      },
      // 可选参数,用于配置弹窗的样式和行为
      config: TinyAlertConfig(
        // 是否显示取消按钮
        showCancelButton: false,
        // 取消按钮文本
        cancelButtonText: 'Cancel',
        // 确认按钮文本
        confirmButtonText: 'OK',
        // 弹窗动画持续时间
        animationDuration: Duration(milliseconds: 300),
        // 弹窗圆角大小
        borderRadius: BorderRadius.circular(12),
        // 弹窗背景颜色
        backgroundColor: Colors.white,
        // 弹窗标题文本样式
        titleTextStyle: TextStyle(
          color: Colors.black,
          fontSize: 18,
          fontWeight: FontWeight.bold,
        ),
        // 弹窗内容文本样式
        messageTextStyle: TextStyle(
          color: Colors.black54,
          fontSize: 16,
        ),
        // 弹窗按钮文本样式
        buttonTextStyle: TextStyle(
          color: Colors.white,
          fontSize: 16,
        ),
        // 确认按钮颜色
        confirmButtonColor: Colors.blue,
        // 取消按钮颜色(如果显示取消按钮)
        cancelButtonColor: Colors.grey,
      ),
    );
  }
}

这个示例代码展示了如何在Flutter应用中使用tiny_alert插件来显示一个成功类型的弹窗。你可以根据需要调整TinyAlertConfig中的参数来定制弹窗的样式和行为。

注意:由于插件版本可能会更新,请确保参考最新的tiny_alert文档来获取最新的API和使用方法。

回到顶部