setRootTemplate static method

Future<void> setRootTemplate({
  1. required CPTemplate rootTemplate,
  2. bool animated = true,
})

Sets the root template of the navigation hierarchy. If a navigation hierarchy already exists, CarPlay replaces the entire hierarchy.

  • rootTemplate is a template to use as the root of a new navigation hierarchy. If one exists, it will replace the current rootTemplate. Must be one of the type: CPTabBarTemplate, CPGridTemplate, CPListTemplate If not, it will throw an TypeError

  • If animated is true, CarPlay animates the presentation of the template, but will be ignored this flag when there isn’t an existing navigation hierarchy to replace.

! CarPlay cannot have more than 5 templates on one screen.

Implementation

static Future<void> setRootTemplate({
  required CPTemplate rootTemplate,
  bool animated = true,
}) async {
  if (rootTemplate is CPTabBarTemplate ||
      rootTemplate is CPGridTemplate ||
      rootTemplate is CPListTemplate ||
      rootTemplate is CPInformationTemplate ||
      rootTemplate is CPPointOfInterestTemplate) {
    return _carPlayController.methodChannel
        .invokeMethod('setRootTemplate', <String, dynamic>{
      'rootTemplate': rootTemplate.toJson(),
      'animated': animated,
      'runtimeType': 'F${rootTemplate.runtimeType}',
    }).then((value) {
      if (value) {
        if (FlutterCarPlayController.templateHistory.isEmpty) {
          FlutterCarPlayController.templateHistory.add(rootTemplate);
        } else {
          FlutterCarPlayController.templateHistory[0] = rootTemplate;
        }
      }
    });
  }
}