Flutter消息提示插件pm_toast的使用

Flutter消息提示插件pm_toast的使用

自定义消息提示库,适用于Flutter。

该插件用于在Flutter应用中显示消息。

支持平台

  • Android
  • IOS

如何使用

pubspec.yaml文件中添加以下依赖项:

dependencies:
  pm_toast: ^1.0.8

然后导入插件:

import 'package:pm_toast/pm_toast.dart';

Flutter PM Toast (Android & iOS)

示例代码如下:

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: HomeScreen(),
    );
  }
}

class HomeScreen extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          title: const Center(child: Text(' PM Toast Package ')),
          backgroundColor: Colors.purple[100]),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
              onPressed: () {
                // 显示简单消息提示
                PMToast(
                  context: context,
                  message: 'This is a simple toast!',
                ).show();
              },
              child: const Text('Show Simple Toast'),
            ),
            const SizedBox(height: 20),
            ElevatedButton(
              onPressed: () {
                // 显示带有按钮的消息提示
                PMToast(
                  context: context,
                  message: 'This is a toast with a button!',
                  backgroundColor: Colors.purple,
                  textColor: Colors.white,
                  fontSize: 18.0,
                  borderRadius: const BorderRadius.all(Radius.circular(20.0)),
                  position: ToastPosition.bottom,
                  leftImage: const Icon(Icons.info, color: Colors.white),
                  rightButton: const Icon(Icons.close, color: Colors.white),
                  onRightButtonPressed: () {
                    print('Right button pressed');
                  },
                ).show();
              },
              child: const Text('Show Toast with Button'),
            ),
          ],
        ),
      ),
    );
  }
}

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

1 回复

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


pm_toast 是一个用于在 Flutter 应用中显示轻量级消息提示(Toast)的插件。它可以帮助你在应用中快速显示短暂的提示信息,例如成功消息、错误消息或其他通知。

安装 pm_toast 插件

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

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

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

使用 pm_toast

1. 显示简单的 Toast 消息

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('pm_toast Example'),
        ),
        body: Center(
          child: ElevatedButton(
            onPressed: () {
              PMToast.showToast(context, 'Hello, this is a toast message!');
            },
            child: Text('Show Toast'),
          ),
        ),
      ),
    );
  }
}

2. 自定义 Toast 样式

pm_toast 允许你自定义 Toast 的样式,例如背景颜色、文字颜色、显示时长等。

PMToast.showToast(
  context,
  'Custom Toast',
  backgroundColor: Colors.blue,
  textColor: Colors.white,
  duration: Duration(seconds: 3),
  position: PMToastPosition.bottom, // 可选:Toast 显示位置(top, center, bottom)
);

3. 显示带图标的 Toast

你还可以在 Toast 中显示图标:

PMToast.showToastWithIcon(
  context,
  'Success!',
  Icons.check_circle,
  backgroundColor: Colors.green,
  textColor: Colors.white,
  iconColor: Colors.white,
);

4. 显示自定义 Widget 的 Toast

如果你想要显示更复杂的 Toast 内容,可以使用 showCustomToast 方法:

PMToast.showCustomToast(
  context,
  Container(
    padding: EdgeInsets.all(16),
    decoration: BoxDecoration(
      color: Colors.blue,
      borderRadius: BorderRadius.circular(8),
    ),
    child: Row(
      children: [
        Icon(Icons.info, color: Colors.white),
        SizedBox(width: 8),
        Text(
          'Custom Widget Toast',
          style: TextStyle(color: Colors.white),
        ),
      ],
    ),
  ),
  duration: Duration(seconds: 3),
);
回到顶部