Flutter轻量级提示框插件ftoast的使用

Flutter轻量级提示框插件ftoast的使用

ftoast 是一个简单且美观的 Toast 插件,帮助开发者创建灵活、简洁和漂亮的 Toast 提示框。本文将详细介绍如何在 Flutter 项目中使用 ftoast。

特性

  • 提供便捷的方式创建 Toast
  • 支持主信息、次信息,并可自定义它们的样式
  • 支持灵活的图标视图
  • 支持配置圆角大小和背景颜色
  • 支持队列显示
  • 支持自定义 Toast 样式

使用指南

添加依赖

首先,在项目的 pubspec.yaml 文件中添加 ftoast 依赖:

dependencies:
  ftoast: ^2.0.0

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

基本使用

以下是一个简单的示例,展示如何使用 ftoast 显示基本的 Toast 提示:

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('FToast Example')),
        body: Center(
          child: ElevatedButton(
            onPressed: () {
              FToast.toast(context, msg: "This is a simple Toast");
            },
            child: Text('Show Toast'),
          ),
        ),
      ),
    );
  }
}

参数说明

参数 类型 必需 默认值 描述
context BuildContext 是 null 页面环境
toast Widget 否 null 自定义的 Toast 视图
msg String 否 null 主标题
msgStyle TextStyle 否 null 主标题文本样式
subMsg String 否 null 副标题
subMsgStyle TextStyle 否 null 副标题文本样式
subMsgSpace double 否 12.0 副标题与主标题之间的间距
corner double 否 6.0 圆角大小
color Color 否 Colors.black54 背景颜色
duration double 否 1800 显示时长(毫秒)
padding EdgeInsets 否 EdgeInsets.only(left: 16.0, right: 16.0, top: 16.0, bottom: 16.0) 内部间距
image Widget 否 null 图标
imageDirection AxisDirection 否 AxisDirection.up 图标相对于文本的位置
imageSpace double 否 9.0 图标与文本之间的距离

示例代码

基本示例

FToast.toast(
  context,
  duration: 800,
  msg: "I'm FToast",
  msgStyle: TextStyle(color: Colors.white),
);

带副标题的示例

FToast.toast(
  context,
  msg: "This is Msg",
  subMsg: "Welcome to use FToast. This is subMsg!",
  subMsgStyle: TextStyle(color: Colors.white, fontSize: 13),
);

带图标的示例

FToast.toast(
  context,
  msg: "This is Msg",
  subMsg: "Welcome to use FToast. This is subMsg!",
  image: Icon(Icons.star, color: Colors.yellow),
  imageDirection: AxisDirection.up,
);

自定义样式的示例

FToast.toast(
  context,
  toast: FSuper(
    text: "Custom Toast",
    style: TextStyle(color: Colors.grey),
    padding: EdgeInsets.all(12),
    shadowColor: Colors.yellow,
    shadowBlur: 80,
  ),
);

完整示例 Demo

以下是一个完整的示例项目,展示了如何使用 ftoast 的各种功能:

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

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

class MyApp extends StatefulWidget {
  [@override](/user/override)
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  int count = 0;

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Builder(builder: (context) {
        return Scaffold(
          appBar: AppBar(
            title: const Text('FToast Example'),
            centerTitle: true,
          ),
          body: SingleChildScrollView(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                buildTitle("FToast"),
                buildMiddleMargin(),
                buildFToast(context),
                const SizedBox(height: 500),

                buildTitle("SubMsg"),
                buildMiddleMargin(),
                buildSubMsg(context),
                const SizedBox(height: 500),

                buildTitle("Image"),
                buildMiddleMargin(),
                buildImage(context),
                const SizedBox(height: 500),

                buildTitle("Custom Toast"),
                buildMiddleMargin(),
                buildCustomToast(context),
                const SizedBox(height: 500),
              ],
            ),
          ),
        );
      }),
    );
  }

  Widget buildTitle(String title) {
    return Text(
      title,
      style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
    );
  }

  Widget buildMiddleMargin() {
    return SizedBox(height: 20);
  }

  Widget buildFToast(BuildContext context) {
    return ElevatedButton(
      onPressed: () {
        FToast.toast(
          context,
          duration: 800,
          msg: "I'm FToast: ${count++}",
          msgStyle: TextStyle(color: Colors.white),
        );
      },
      child: Text('Show Basic Toast'),
    );
  }

  Widget buildSubMsg(BuildContext context) {
    return ElevatedButton(
      onPressed: () {
        FToast.toast(
          context,
          msg: "This is Msg",
          subMsg: "Welcome to use FToast. This is subMsg!",
          subMsgStyle: TextStyle(color: Colors.white, fontSize: 13),
        );
      },
      child: Text('Show Toast with SubMsg'),
    );
  }

  AxisDirection up = AxisDirection.up;

  Widget buildImage(BuildContext context) {
    return ElevatedButton(
      onPressed: () {
        switch (up) {
          case AxisDirection.up:
            up = AxisDirection.left;
            break;
          case AxisDirection.left:
            up = AxisDirection.right;
            break;
          case AxisDirection.right:
            up = AxisDirection.down;
            break;
          case AxisDirection.down:
            up = AxisDirection.up;
            break;
        }
        FToast.toast(
          context,
          msg: "This is Msg",
          subMsg: "Welcome to use FToast. This is subMsg!",
          image: Icon(Icons.star, color: Colors.yellow),
          imageDirection: up,
        );
      },
      child: Text('Show Toast with Image'),
    );
  }

  Widget buildCustomToast(BuildContext context) {
    return ElevatedButton(
      onPressed: () {
        FToast.toast(
          context,
          toast: FSuper(
            text: "Custom Toast",
            style: TextStyle(color: Colors.grey),
            padding: EdgeInsets.all(12),
            shadowColor: Colors.yellow,
            shadowBlur: 80,
          ),
        );
      },
      child: Text('Show Custom Toast'),
    );
  }
}

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

1 回复

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


当然,下面是一个关于如何在Flutter中使用轻量级提示框插件ftoast的示例代码。这个示例将展示如何安装ftoast插件,并在Flutter应用中创建一个简单的提示框。

步骤 1: 安装 ftoast 插件

首先,在你的Flutter项目的根目录下打开终端,并运行以下命令来添加ftoast依赖:

flutter pub add ftoast

步骤 2: 导入 ftoast 并在应用中使用

在你的Dart文件中(例如main.dart),导入ftoast包,并配置和使用提示框。

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

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

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

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  FToast fToast;

  @override
  void initState() {
    super.initState();
    // 初始化FToast
    fToast = FToast();
    fToast.init(context);
  }

  @override
  void dispose() {
    // 释放FToast资源
    fToast?.dispose();
    super.dispose();
  }

  void showToast() {
    fToast.showToast(
      msg: "这是一个提示框",
      toastLength: 2, // 显示时长,单位为秒
      gravity: ToastGravity.BOTTOM, // 显示位置
      backgroundColor: Colors.black.withOpacity(0.7), // 背景颜色
      textColor: Colors.white, // 文字颜色
      isCenter: true, // 是否居中显示
      icon: Icon(Icons.info_outline), // 可选图标
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter Demo'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: showToast,
          child: Text('显示提示框'),
        ),
      ),
    );
  }
}

解释

  1. 依赖添加: 通过flutter pub add ftoast命令将ftoast插件添加到你的项目中。
  2. 初始化FToast: 在initState方法中初始化FToast实例,并调用init(context)方法。
  3. 释放资源: 在dispose方法中调用fToast?.dispose()以释放资源,防止内存泄漏。
  4. 显示提示框: 创建一个showToast方法,该方法使用fToast.showToast来显示一个自定义的提示框。你可以根据需要配置提示框的文本、显示时长、位置、背景颜色、文字颜色、是否居中以及图标等属性。
  5. UI布局: 使用ElevatedButton来触发提示框的显示。

这个示例展示了如何使用ftoast插件在Flutter应用中创建一个简单的提示框。你可以根据需要进一步自定义和扩展提示框的样式和行为。

回到顶部