我不知道你是否还有问题,但我刚刚发布了一个包来解析纯 dart 中的 iCalendar 格式。
https://pub.dev/packages/icalendar_parser
它仍处于早期开发阶段,但可能会帮助您解决问题。
这是一个关于如何使用该包的示例:
import 'package:icalendar_parser/icalendar_parser.dart';
// Parsing from an ICS String (ex: if you are getting data from an API)
final iCalParsed = ICalendar.fromString(yourIcsString);
// Parsing from an ICS List<String> (ex: if you are reading data from a file)
final iCalParsed2 = ICalendar.fromLines(icsFileLines);
这是我在 Flutter 中制作的完整示例:
import 'dart:core';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart' show rootBundle;
import 'package:icalendar_parser/icalendar_parser.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart' as p;
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
ICalendar _iCalendar;
bool _isLoading = false;
Future<void> _getAssetsFile(String assetName) async {
setState(() => _isLoading = true);
try {
final directory = await getTemporaryDirectory();
final path = p.join(directory.path, assetName);
final data = await rootBundle.load('assets/$assetName');
final bytes =
data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
final file = await File(path).writeAsBytes(bytes);
final lines = await file.readAsLines();
setState(() {
_iCalendar = ICalendar.fromLines(lines);
_isLoading = false;
});
} catch (e) {
setState(() => _isLoading = false);
throw 'Error: $e';
}
}
Widget _generateTextContent() {
final style = const TextStyle(color: Colors.black);
return RichText(
text: TextSpan(
children: [
TextSpan(
text: 'VERSION: ${_iCalendar.version}\n',
style: style.copyWith(fontWeight: FontWeight.bold)),
TextSpan(
text: 'PRODID: ${_iCalendar.prodid}\n',
style: style.copyWith(fontWeight: FontWeight.bold)),
TextSpan(
children: _iCalendar.data
.map((e) => TextSpan(
children: e.keys
.map((f) => TextSpan(children: [
TextSpan(
text: '${f.toUpperCase()}: ',
style: style.copyWith(
fontWeight: FontWeight.bold)),
TextSpan(text: '${e[f]}\n')
]))
.toList(),
))
.toList()),
],
style: style,
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: Column(
children: [
if (_isLoading || _iCalendar == null)
const Center(child: CircularProgressIndicator())
else
_generateTextContent(),
RaisedButton(
child: const Text('Load File 1'),
onPressed: () => _getAssetsFile('calendar.ics'),
),
RaisedButton(
child: const Text('Load File 2'),
onPressed: () => _getAssetsFile('calendar2.ics'),
),
],
),
),
);
}
}