Flutter---Notification(2)

效果图

大图通知

进度条通知

大图通知的实现方法

Dart 复制代码
  // 方法2: 大图通知
  Future<void> _showBigPictureNotification() async {

    _notificationId++; //确保每个通知有唯一的标识符

    final BigPictureStyleInformation bigPictureStyle =
    const BigPictureStyleInformation(
      //DrawableResourceAndroidBitmap:从资源文件处加载;FilePathAndroidBitmap:从文件路径加载;
      // ByteArrayAndroidBitmap:从字节数据加载;NetworkBitmap:从网络URL加载
      DrawableResourceAndroidBitmap('@mipmap/ic_launcher'),//大图片
      largeIcon: DrawableResourceAndroidBitmap('@mipmap/ic_launcher'),//大图标
      contentTitle: '大图通知',//内容标题
      htmlFormatContentTitle: true, //标题支持HTML格式
      summaryText: '这是一个带大图的通知', //摘要文本
      htmlFormatSummaryText: true, //摘要支持HTML格式
    );

    final AndroidNotificationDetails androidDetails =
    AndroidNotificationDetails(
      'important_channel', //通道ID
      '重要通知', //通道名称
      //其他参数
      // BigTextStyleInformation:大文本样式
      // InboxStyleInformation:收件箱样式
      // MediaStyleInformation:媒体播放样式
      // MessagingStyleInformation:消息样式
      styleInformation: bigPictureStyle, //应用大图样式*****
      importance: Importance.max, //重要级别
      priority: Priority.high, //优先级
    );

    final NotificationDetails details = NotificationDetails(
      android: androidDetails,
      iOS: const DarwinNotificationDetails(),
    );

    await flutterLocalNotificationsPlugin.show(
      _notificationId,
      '🖼️ 大图通知',//ID
      '点击查看大图', //标题
      details,//样式
      payload: 'big_picture',
    );
  }


         // 大图通知
          _buildNotificationButton(
            '大图通知',
            Icons.image,
            _showBigPictureNotification,
            color: Colors.purple,
          ),
          const SizedBox(height: 12),

进度条通知的实现方法

Dart 复制代码
Future<void> _showProgressNotification() async {

    _notificationId++; //递增全局通知ID

    final int progressId = _notificationId; //保存当前进度通知的ID

    // 初始化进度为0
    final AndroidNotificationDetails initialDetails =
    const AndroidNotificationDetails(
      'progress_channel', //ID
      '进度通知', //名称
      importance: Importance.high, //重要性
      priority: Priority.high, //优先级
      onlyAlertOnce: true, //只提醒一次***
      showProgress: true,//显示进度条***
      maxProgress: 100,//最大进度值***
      progress: 0, //当前进度值***
    );

    await flutterLocalNotificationsPlugin.show(
      progressId, //ID
      '📥 下载中...', //标题
      '进度: 0%',//通知内容(显示当前进度)
      NotificationDetails(android: initialDetails),
    );

    // 模拟进度更新
    for (int progress = 10; progress <= 100; progress += 10) {
      await Future.delayed(const Duration(milliseconds: 500));

      final AndroidNotificationDetails updatedDetails =
      AndroidNotificationDetails(
        'progress_channel',
        '进度通知',
        importance: Importance.high,
        priority: Priority.high,
        onlyAlertOnce: true,
        showProgress: true,
        maxProgress: 100,
        progress: progress,
      );

      await flutterLocalNotificationsPlugin.show(
        progressId,
        '📥 下载中...',
        '进度: $progress%',
        NotificationDetails(android: updatedDetails),
      );
    }

    // 下载完成
    final AndroidNotificationDetails completedDetails =
    const AndroidNotificationDetails(
      'progress_channel',
      '进度通知',
      importance: Importance.high,
      priority: Priority.high,
    );

    await flutterLocalNotificationsPlugin.show(
      progressId,
      '✅ 下载完成',
      '文件下载成功',
      NotificationDetails(android: completedDetails),
    );
  }


         // 进度条通知
          _buildNotificationButton(
            '进度条通知',
            Icons.download,
            _showProgressNotification,
            color: Colors.orange,
          ),
          const SizedBox(height: 12),
相关推荐
火柴就是我2 分钟前
学习一些常用的混合模式之BlendMode. SRC_OVER
android·flutter
程序员Agions27 分钟前
Flutter 邪修秘籍:那些官方文档不会告诉你的骚操作
前端·flutter
CodeOfCC3 小时前
flutter-elinux 编译linux arm64程序
linux·flutter
kirk_wang4 小时前
Flutter艺术探索-StatelessWidget与StatefulWidget区别与使用场景
flutter·移动开发·跨平台
搬砖的kk4 小时前
Flutter OH 3.35.7 Dev 版本发布 | 快速体验指南
flutter
未来猫咪花4 小时前
tp_router: 再也不用手动写路由表了
flutter
程序员老刘·4 小时前
ArkUI-X 6.0 跨平台框架能否取代 Flutter?
flutter·鸿蒙系统·跨平台开发·客户端开发
哈__5 小时前
Flutter For OpenHarmony 鸿蒙 PC 开发入门:环境搭建 + 工程初始化(附 PC 端专属配置)
flutter·华为·harmonyos
前端不太难6 小时前
Flutter 列表 rebuild 的真正边界在哪里
flutter·状态模式