tencentcloud_cls_sdk_dart 0.0.2 tencentcloud_cls_sdk_dart: ^0.0.2 copied to clipboard
tencent cloud cls dart sdk.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:tencentcloud_cls_sdk_dart/tencentcloud_cls_sdk_dart.dart';
Future<void> main() async {
await RustLib.init();
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
LogProducer? _logProducer;
String _consoleText = '';
@override
void initState() {
super.initState();
if (!mounted) return;
print("application started.");
}
void print(String message) {
setState(() {
_consoleText += message;
_consoleText += '\n';
});
}
void _initProducer() async {
_logProducer = LogProducer(
topicId: '',
accessKey: '',
accessSecret: '',
accessToken: '',
host: 'ap-guangzhou-open.cls.tencentcs.com',
waitSendLogQueue: 1000, addLogQueue: 1000, lingerMs: 2000);
print('init producer client success');
}
void _callback() async {
if (!check()) {
return;
}
_logProducer?.setCallback(dartCallback: (topicId, requestId, status, errorMessage) => print('$requestId!, $status, $topicId, $errorMessage'));
print('init callback success');
}
void _sendLog() async {
if (!check()) {
return;
}
_logProducer?.addLog(log: { 'hello': 'world' });
}
bool check() {
if (null == _logProducer) {
print('you should init producer first.');
return false;
}
return true;
}
@override
Widget build(BuildContext context) {
Color color = Theme.of(context).primaryColor;
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('tencent cloud cls flutter sdk demo')),
body: Column(
children: [
_buildConsoleText(),
_buildButton(color, 'init', _initProducer),
_buildButton(color, 'set callback', _callback),
_buildButton(color, 'send', _sendLog),
],
),
),
);
}
Widget _buildConsoleText() {
return Row(mainAxisSize: MainAxisSize.max, mainAxisAlignment: MainAxisAlignment.start, children: [
Expanded(
flex: 1,
child: Container(
margin: const EdgeInsets.only(bottom: 18),
padding: const EdgeInsets.all(6),
height: 140,
decoration: BoxDecoration(
border: Border.all(
color: Colors.grey,
width: 0.67,
),
color: Colors.black),
child: SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Text(
_consoleText,
style: const TextStyle(
color: Colors.white,
fontSize: 12,
letterSpacing: 2,
wordSpacing: 2,
fontFeatures: [FontFeature.tabularFigures()]),
),
),
))
]);
}
Widget _buildButton(Color color, String label, VoidCallback? onPressed) {
return Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(
flex: 1,
child: Container(
margin: const EdgeInsets.only(left: 16, top: 8, right: 16),
child: TextButton(
onPressed: onPressed,
style: ButtonStyle(
shape: WidgetStateProperty.all(RoundedRectangleBorder(borderRadius: BorderRadius.circular(12))),
side: WidgetStateProperty.all(BorderSide(color: color, width: 0.67)),
backgroundColor: WidgetStateProperty.all(Colors.transparent),
padding:
WidgetStateProperty.all(const EdgeInsets.only(left: 12, top: 6, right: 12, bottom: 6))),
child: Text(
label,
style: TextStyle(fontSize: 22, fontWeight: FontWeight.w400, color: color),
)),
)),
],
);
}
}