Flutter消息提示插件toast_ease的使用

Flutter消息提示插件toast_ease的使用

ToastEase 是一个用于在 Flutter 应用中显示可自定义的消息提示(如成功、错误、警告和信息)的插件。

功能

  • 可定制的消息提示:轻松显示带有可定制颜色和图标的成功、错误、警告和信息提示。
  • 无外部依赖:此插件不依赖任何外部库。
  • 灵活的设计:可以修改提示的外观和行为以适应应用的设计。

开始使用

安装

pubspec.yaml 文件中添加以下内容:

dependencies:
  toast_ease: ^1.0.0

然后运行 flutter pub get 来安装该包。

导入包

在 Dart 文件中导入包:

import 'package:toast_ease/toast_ease.dart';

使用

显示成功提示

成功提示

这是一个带有可定制消息和样式的成功提示。

ToastEase.successToast(
  context: context,
  msg: "这是成功提示",
);

显示错误提示

错误提示

这是一个带有可定制错误消息和样式的错误提示。

ToastEase.errorToast(
  context: context,
  msg: "这是错误提示",
);

显示警告提示

警告提示

这是一个带有可定制警告消息和样式的警告提示。

ToastEase.warningToast(
  context: context,
  msg: "这是警告提示",
);

显示信息提示

信息提示

这是一个带有可定制信息消息和样式的信息提示。

ToastEase.infoToast(
  context: context,
  msg: "这是信息提示",
);

额外信息

自定义

你可以自定义提示的颜色和标题:

ToastEase.warningToast(
  context: context,
  msg: "自定义警告消息",
  color: Colors.orange,
  title: "自定义警告",
);

许可证

该包遵循 MIT 许可证。更多详情请参阅 LICENSE 文件。


完整示例代码

import 'package:flutter/material.dart';
import 'package:toast_ease/toast_ease.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)
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: HomeScreen(),
    );
  }
}

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

  [@override](/user/override)
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Toast Ease"),
        centerTitle: true,
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              ElevatedButton(
                  onPressed: () {
                    ToastEase.successToast(
                        context: context, msg: "这是成功提示");
                  },
                  child: const Text("成功提示")),
            ],
          ),
          ElevatedButton(
              onPressed: () {
                ToastEase.warningToast(
                    context: context, msg: "这是警告提示");
              },
              child: const Text("警告提示")),
          ElevatedButton(
              onPressed: () {
                ToastEase.infoToast(
                    context: context, msg: "这是信息提示");
              },
              child: const Text("信息提示")),
          ElevatedButton(
              onPressed: () {
                ToastEase.errorToast(
                    context: context, msg: "这是错误提示");
              },
              child: const Text("错误提示"))
        ],
      ),
    );
  }
}

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

1 回复

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


toast_ease 是一个 Flutter 插件,用于在应用中显示简单、轻量级的消息提示(Toast)。它提供了一个简单的 API,可以方便地在应用的任何地方显示短时间的消息提示。

安装 toast_ease

首先,你需要将 toast_ease 添加到你的 pubspec.yaml 文件的 dependencies 部分。

dependencies:
  flutter:
    sdk: flutter
  toast_ease: ^1.0.0  # 请检查最新版本

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

使用 toast_ease

  1. 导入包:

    在你的 Dart 文件中导入 toast_ease 包。

    import 'package:toast_ease/toast_ease.dart';
    
  2. 显示 Toast:

    使用 ToastEase.showToast 方法来显示 Toast。你可以指定消息内容、持续时间、位置等参数。

    ToastEase.showToast(
      msg: 'Hello, Toast!',
      duration: ToastEase.lengthShort,
      position: ToastEase.bottom,
    );
    
  3. 参数说明:

    • msg: 要显示的消息内容(String 类型)。
    • duration: Toast 显示的持续时间。默认为 ToastEase.lengthShort,可选 ToastEase.lengthLong
    • position: Toast 显示的位置。默认为 ToastEase.bottom,可选 ToastEase.centerToastEase.top
    • bgcolor: Toast 的背景颜色(Color 类型)。
    • textColor: Toast 的文本颜色(Color 类型)。
    • textSize: Toast 的文本大小(double 类型)。
  4. 示例代码:

    以下是一个完整的示例,展示如何在按钮点击时显示 Toast。

    import 'package:flutter/material.dart';
    import 'package:toast_ease/toast_ease.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'ToastEase Example',
          home: ToastExample(),
        );
      }
    }
    
    class ToastExample extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('ToastEase Example'),
          ),
          body: Center(
            child: ElevatedButton(
              onPressed: () {
                ToastEase.showToast(
                  msg: 'Button Clicked!',
                  duration: ToastEase.lengthShort,
                  position: ToastEase.bottom,
                  bgcolor: Colors.black54,
                  textColor: Colors.white,
                  textSize: 16.0,
                );
              },
              child: Text('Show Toast'),
            ),
          ),
        );
      }
    }
回到顶部