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),
相关推荐
恋猫de小郭2 天前
Android 限制侧载新进展,谷歌联合国内厂商推验证计划
android·前端·flutter
恋猫de小郭2 天前
解读 Android 17 全新内存限制,有没有“豁免”后门?
android·前端·flutter
程序员老刘4 天前
跨平台开发地图 | 2026年6月
flutter·ai编程·客户端
悟空瞎说5 天前
Flutter 架构详解:新手必懂底层原理
flutter
SoaringHeart5 天前
Flutter最佳实践:IM聊天文字链接自动识别跳转
前端·flutter
恋猫de小郭5 天前
KMP / CMP 鸿蒙版本 Beta 发布,他有什么特别之处?
android·前端·flutter
风华圆舞6 天前
Flutter + 鸿蒙 Intents Kit:页面直达能力的完整接入方案
flutter·ui·华为·harmonyos
韩曙亮6 天前
【Flutter】Flutter 组件 ④ ( 组件渲染 的 三棵树理论 | Widget 树 → Element 树 → RenderObject 树 )
flutter·element·widget·renderobject
恋猫de小郭6 天前
Android 17 正式版发布,全新 AI 和各种破坏性更新
android·前端·flutter
kingbal6 天前
Windows:flutter环境搭建
windows·flutter