techpro_carplay 0.0.2
techpro_carplay: ^0.0.2 copied to clipboard
Build Apple CarPlay and Android Auto screens from Flutter: one Dart API for lists, grids and messages on both car platforms.
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:techpro_carplay/techpro_carplay.dart';
void main() {
runApp(const ExampleApp());
}
/// A small store finder. The same screens appear on CarPlay and Android
/// Auto, and the phone shows which screens the car currently has.
class ExampleApp extends StatefulWidget {
const ExampleApp({super.key});
@override
State<ExampleApp> createState() => _ExampleAppState();
}
class _ExampleAppState extends State<ExampleApp> {
final car = TechproCar.instance;
@override
void initState() {
super.initState();
unawaited(_showHome());
}
/// Shows a loading list first, then the stores once they are "loaded".
Future<void> _showHome() async {
await car
.setRoot(CarListTemplate(id: 'home', title: 'Stores', loading: true));
await Future<void>.delayed(const Duration(seconds: 1));
await car.update(CarListTemplate(
id: 'home',
title: 'Stores',
sections: [
CarListSection(header: 'Nearby', items: [
for (final store in _stores)
CarListItem(
title: store.name,
subtitle: store.distance,
image: CarImage.asset(store.icon),
showsChevron: true,
onSelect: () => _showStore(store),
),
]),
CarListSection(header: 'More', items: [
CarListItem(
title: 'Categories',
showsChevron: true,
onSelect: _showCategories),
CarListItem(title: 'Clear history', onSelect: _confirmClear),
]),
],
));
}
Future<void> _showStore(_Store store) => car.push(CarInfoTemplate(
title: store.name,
rows: [
CarInfoRow(label: 'Distance', value: store.distance),
CarInfoRow(label: 'Open', value: store.hours),
],
actions: [
CarAction(
title: 'Call',
style: CarActionStyle.primary,
onPressed: () => _notify('Calling ${store.name}…'),
),
CarAction(title: 'Back', onPressed: car.pop),
],
));
Future<void> _showCategories() => car.push(CarGridTemplate(
title: 'Categories',
items: [
for (final (name, icon) in const [
('Food', 'assets/green.png'),
('Fuel', 'assets/orange.png'),
('Pharmacy', 'assets/blue.png'),
('Shops', 'assets/purple.png'),
])
CarGridItem(
title: name,
image: CarImage.asset(icon),
onSelect: () => _notify('$name selected'),
),
],
));
Future<void> _confirmClear() => car.showAlert(CarAlert(
title: 'Clear history?',
message: 'Recent stores will be removed.',
actions: [
CarAction(
title: 'Clear',
style: CarActionStyle.destructive,
onPressed: () => _notify('History cleared'),
),
CarAction(title: 'Cancel', style: CarActionStyle.cancel),
],
));
/// Shows a short confirmation in the car, replacing any open alert.
Future<void> _notify(String text) async {
await car.dismissAlert();
await car
.showAlert(CarAlert(title: text, actions: [CarAction(title: 'OK')]));
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('techpro_carplay example')),
body: ListenableBuilder(
listenable: car,
builder: (context, _) => ListView(
padding: const EdgeInsets.all(16),
children: [
Card(
child: ListTile(
leading: Icon(
car.connection.isConnected
? Icons.directions_car
: Icons.car_rental_outlined,
),
title: Text(car.connection.isConnected
? 'Connected to ${car.connection.platform?.name}'
: 'No car connected'),
subtitle: const Text(
'Open CarPlay in the iOS Simulator (I/O → External '
'Displays) or the Android Auto Desktop Head Unit.',
),
),
),
const SizedBox(height: 16),
Text('Car screens',
style: Theme.of(context).textTheme.titleMedium),
for (final (index, template) in car.stack.indexed)
ListTile(
leading: Text('${index + 1}'),
title: Text(template.title),
subtitle: Text(template.runtimeType.toString()),
),
if (car.alert case final alert?)
ListTile(
leading: const Icon(Icons.warning_amber),
title: Text(alert.title),
subtitle: const Text('Alert'),
),
const SizedBox(height: 16),
FilledButton(
onPressed: _showHome, child: const Text('Reset car screen')),
],
),
),
),
);
}
}
class _Store {
const _Store(this.name, this.distance, this.hours, this.icon);
final String name;
final String distance;
final String hours;
final String icon;
}
const _stores = [
_Store('Main Street Market', '0.4 km', '8:00 – 20:00', 'assets/green.png'),
_Store('Lakeside Pharmacy', '1.2 km', '9:00 – 18:30', 'assets/blue.png'),
_Store('City Fuel', '2.5 km', 'Open 24 hours', 'assets/orange.png'),
];