cc_uni_api 1.0.3
cc_uni_api: ^1.0.3 copied to clipboard
A simple api for TVOS.
example/lib/main.dart
import 'dart:convert';
import 'package:cc_uni_api/cc_uni_api.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const UniApiExampleApp());
}
class UniApiExampleApp extends StatelessWidget {
const UniApiExampleApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'cc_uni_api Example',
theme: ThemeData(primarySwatch: Colors.blue),
home: const DeviceInfoPage(),
);
}
}
class DeviceInfoPage extends StatefulWidget {
const DeviceInfoPage({super.key});
@override
State<DeviceInfoPage> createState() => _DeviceInfoPageState();
}
class _DeviceInfoPageState extends State<DeviceInfoPage> {
bool _isLoading = false;
String _response = 'No request has been sent.';
Future<void> _getDeviceInfo() async {
setState(() {
_isLoading = true;
_response = 'Requesting device information...';
});
try {
final deviceInfo = await UniApi.system.getDeviceInfo();
if (!mounted) {
return;
}
setState(() {
_response = const JsonEncoder.withIndent(' ').convert(deviceInfo);
});
} catch (error) {
if (!mounted) {
return;
}
setState(() {
_response = 'Request failed: $error';
});
} finally {
if (mounted) {
setState(() {
_isLoading = false;
});
}
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('cc_uni_api Example')),
body: Padding(
padding: const EdgeInsets.all(24),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Text(
'System API',
style: Theme.of(context).textTheme.headlineSmall,
),
const SizedBox(height: 8),
const Text('Read device information through UniApi.system.'),
const SizedBox(height: 20),
ElevatedButton.icon(
onPressed: _isLoading ? null : _getDeviceInfo,
icon: _isLoading
? const SizedBox(
width: 18,
height: 18,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Icon(Icons.devices),
label: const Text('Get Device Info'),
),
const SizedBox(height: 24),
Text('Response', style: Theme.of(context).textTheme.titleMedium),
const SizedBox(height: 8),
Expanded(
child: Container(
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
border: Border.all(color: Theme.of(context).dividerColor),
),
child: SingleChildScrollView(
child: SelectableText(_response),
),
),
),
],
),
),
);
}
}