系统功能组件

系统功能组件用于访问设备的系统功能,如通知、分享、文件系统等。

提示:这些组件通常需要引入第三方包,并在平台配置中添加必要的权限。
📱 渲染效果预览
🔔
新消息
您有一条新消息
📱 渲染效果预览

Notifications

Notifications 用于显示本地通知。

import 'package:flutter_local_notifications/flutter_local_notifications.dart';

final notifications = FlutterLocalNotificationsPlugin();

// 初始化通知
const AndroidInitializationSettings initializationSettingsAndroid =
    AndroidInitializationSettings('@mipmap/ic_launcher');

final InitializationSettings initializationSettings = InitializationSettings(
  android: initializationSettingsAndroid,
);

await notifications.initialize(initializationSettings);

// 显示通知
const AndroidNotificationDetails androidPlatformChannelSpecifics =
    AndroidNotificationDetails(
  'your channel id',
  'your channel name',
  channelDescription: 'your channel description',
  importance: Importance.max,
  priority: Priority.high,
);

const NotificationDetails platformChannelSpecifics =
    NotificationDetails(android: androidPlatformChannelSpecifics);

await notifications.show(
  0,
  '新消息',
  '您有一条新消息',
  platformChannelSpecifics,
);
📱 渲染效果预览

Share

Share 用于分享内容到其他应用。

import 'package:share_plus/share_plus.dart';

// 分享文本
Share.share('这是一段要分享的文本');

// 分享文件
Share.shareFiles(
  ['path/to/file.jpg'],
  text: '查看这张图片',
);

// 分享链接
Share.shareUri(Uri.parse('https://example.com'));
📱 渲染效果预览
📁 /data/app/ 📄 config.json 📄 data.txt 📁 cache/ 📄 image.jpg 📁 /storage/ 📁 downloads/ 📄 file.pdf

Url Launcher

Url Launcher 用于打开 URL、发送邮件、拨打电话等。

import 'package:url_launcher/url_launcher.dart';

// 打开网页
await launchUrl(Uri.parse('https://example.com'));

// 发送邮件
await launchUrl(Uri.parse('mailto:example@example.com'));

// 拨打电话
await launchUrl(Uri.parse('tel:+1234567890'));

// 发送短信
await launchUrl(Uri.parse('sms:+1234567890'));

// 打开应用商店
await launchUrl(Uri.parse(
  'https://apps.apple.com/app/id1234567890'
));
📱 渲染效果预览
📱 系统目录 📁 /data/user/0/com.app/cache/ (临时) 📁 /data/user/0/com.app/files/ (文档) 📁 /storage/emulated/0/Android/data/ (外部)

File System

File System 用于读写文件。

import 'dart:io';

// 读取文件
final file = File('path/to/file.txt');
final contents = await file.readAsString();

// 写入文件
await file.writeAsString('Hello, World!');

// 检查文件是否存在
final exists = await file.exists();

// 删除文件
await file.delete();

// 列出目录内容
final directory = Directory('path/to/directory');
final files = directory.listSync();
📱 渲染效果预览
username john
age 25
isLoggedIn true

Path Provider

Path Provider 用于获取设备上的常用目录路径。

import 'package:path_provider/path_provider.dart';

// 获取临时目录
final tempDir = await getTemporaryDirectory();

// 获取应用文档目录
final appDocDir = await getApplicationDocumentsDirectory();

// 获取应用支持目录
final appSupportDir = await getApplicationSupportDirectory();

// 获取外部存储目录(仅 Android)
final externalDir = await getExternalStorageDirectory();

// 获取下载目录
final downloadsDir = await getDownloadsDirectory();
📱 渲染效果预览
username john
age 25
isLoggedIn true

Shared Preferences

Shared Preferences 用于存储简单的键值对数据。

import 'package:shared_preferences/shared_preferences.dart';

// 获取实例
final prefs = await SharedPreferences.getInstance();

// 存储数据
await prefs.setString('username', 'john');
await prefs.setInt('age', 25);
await prefs.setBool('isLoggedIn', true);

// 读取数据
final username = prefs.getString('username');
final age = prefs.getInt('age');
final isLoggedIn = prefs.getBool('isLoggedIn');

// 删除数据
await prefs.remove('username');

// 清空所有数据
await prefs.clear();
使用建议:
  • Shared Preferences: 存储简单的配置信息
  • File System: 存储大量数据或二进制文件
  • Secure Storage: 存储敏感信息(密码、令牌等)
  • Path Provider: 获取系统目录路径