Skip to content

Instantly share code, notes, and snippets.

@Subhangi3
Created November 3, 2022 09:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Subhangi3/168c917dfb34e6eabdfcd2ecb6423e7e to your computer and use it in GitHub Desktop.
Save Subhangi3/168c917dfb34e6eabdfcd2ecb6423e7e to your computer and use it in GitHub Desktop.
// ignore_for_file: avoid_print
import 'package:flutter/material.dart';
import 'package:flutter_carplay/flutter_carplay.dart';
void main() {
runApp(const MyCarplayApp());
}
class MyCarplayApp extends StatefulWidget {
const MyCarplayApp({Key? key}) : super(key: key);
@override
State<MyCarplayApp> createState() => _MyCarplayAppState();
}
class _MyCarplayAppState extends State<MyCarplayApp> {
CPConnectionStatusTypes connectionStatus = CPConnectionStatusTypes.unknown;
final FlutterCarplay _flutterCarplay = FlutterCarplay();
@override
void initState() {
super.initState();
final List<CPListSection> collectionItems = [];
final List<CPListSection> playlistsItems = [];
final List<CPListSection> songsItems = [];
collectionItems.add(CPListSection(
header: "header 1",
items: [
CPListItem(
text: "Drum (Big Kids Edition)",
detailText: "",
accessoryType: CPListItemAccessoryTypes.disclosureIndicator,
image: 'images/logo_flutter_1080px_clr.png',
onPress: (complete, self) {
complete();
},
),
CPListItem(
text: "Alligator Level 1 (Rhythm Kids)",
detailText: "",
accessoryType: CPListItemAccessoryTypes.disclosureIndicator,
image: 'images/logo_flutter_1080px_clr.png',
onPress: (complete, self) {
complete();
},
),
CPListItem(
text: "Fiddle (Family Edition)",
detailText: "",
accessoryType: CPListItemAccessoryTypes.disclosureIndicator,
image: 'images/logo_flutter_1080px_clr.png',
onPress: (complete, self) {
openFiddleListTemplate(title: self.text);
complete();
},
),
],
));
FlutterCarplay.setRootTemplate(
rootTemplate: CPTabBarTemplate(title: "Carplay1234", templates: [
CPListTemplate(
sections: collectionItems,
systemIcon: "folder.fill",
title: "Collections",
),
CPListTemplate(
sections: [],
systemIcon: "list.bullet",
title: "Playlists",
emptyViewTitleVariants: ["Settings"],
emptyViewSubtitleVariants: [
"No settings have been added here yet. You can start adding right away"
],
),
CPListTemplate(
sections: [],
systemIcon: "music.note",
title: "Songs",
emptyViewTitleVariants: ["Settings"],
emptyViewSubtitleVariants: [
"No settings have been added here yet. You can start adding right away"
],
),
]),
);
_flutterCarplay.addListenerOnConnectionChange(onCarplayConnectionChange);
}
@override
void dispose() {
_flutterCarplay.removeListenerOnConnectionChange();
super.dispose();
}
void onCarplayConnectionChange(CPConnectionStatusTypes status) {
// Do things when carplay state is connected, background or disconnected
setState(() {
connectionStatus = status;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Flutter Carplay'),
),
body: Column(),
),
);
}
void openFiddleListTemplate({required String title}) {
FlutterCarplay.push(
template: CPListTemplate(
systemIcon: "systemIcon",
title: title,
sections: [
CPListSection(
header: "header 2",
items: [
CPListItem(
text: "Hello song",
detailText: "Downloaded",
accessoryType: CPListItemAccessoryTypes.disclosureIndicator,
image: 'images/logo_flutter_1080px_clr.png',
onPress: (complete, self) {
print("Hello song tap");
// self.setDetailText("Streaming");
// showAlert();
// complete();
Future.delayed(const Duration(seconds: 1), () {
self.setDetailText("Customizable Detail Text");
complete();
});
},
),
CPListItem(
text: "Crawdad (RP)",
detailText: "Downloaded",
accessoryType: CPListItemAccessoryTypes.disclosureIndicator,
image:
"https://www.musictogether.com/familymusiczone/content/collection_cover_img/WelcomeCD18.jpg",
onPress: (complete, self) {
self.updateTexts(detailText: "Streaming");
complete();
},
),
CPListItem(
text: "The Sounds of Fall",
detailText: "Downloaded",
accessoryType: CPListItemAccessoryTypes.disclosureIndicator,
image:
"https://www.musictogether.com/familymusiczone/content/collection_cover_img/WelcomeCD18.jpg",
onPress: (complete, self) {
self.updateTexts(detailText: "Streaming");
complete();
},
),
],
),
],
),
animated: true,
);
}
void showAlert() {
FlutterCarplay.showAlert(
template: CPAlertTemplate(
titleVariants: ["Alert Title"],
actions: [
CPAlertAction(
title: "Okay",
style: CPAlertActionStyles.normal,
onPress: () {
FlutterCarplay.popModal(animated: true);
print("Okay pressed");
},
),
CPAlertAction(
title: "Cancel",
style: CPAlertActionStyles.cancel,
onPress: () {
FlutterCarplay.popModal(animated: true);
print("Cancel pressed");
},
),
CPAlertAction(
title: "Remove",
style: CPAlertActionStyles.destructive,
onPress: () {
FlutterCarplay.popModal(animated: true);
print("Remove pressed");
},
),
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment