ExpandedControls: improved castingToText

This commit is contained in:
gianlucaparadise 2021-12-15 08:28:15 +01:00
parent 7c2896a9de
commit f7fa33d04c
3 changed files with 25 additions and 6 deletions

View file

@ -30,6 +30,8 @@ const _bottomUpBlackGradient = BoxDecoration(
class ExpandedControls extends StatefulWidget {
final FlutterCastFramework castFramework;
/// Label to introduce cast device. Default is "Casting to {{device_name}}", where {{device_name}} is replaced with the device name
final String? castingToText;
/// This is called when the back button is tapped or when the session is closed
@ -129,6 +131,7 @@ class _ExpandedControlsState extends State<ExpandedControls> {
padding: const EdgeInsets.all(8.0),
child: ExpandedControlsConnectedDeviceLabel(
castFramework: widget.castFramework,
castingToText: widget.castingToText,
),
),
Padding(

View file

@ -2,16 +2,29 @@ import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_cast_framework/cast.dart';
/// Placeholder to be used for the castingToText of ExpandedControlsConnectedDeviceLabel
const CAST_DEVICE_NAME_PLACEHOLDER = "{{cast_device_name}}";
class ExpandedControlsConnectedDeviceLabel extends StatelessWidget {
final FlutterCastFramework castFramework;
final String castingToText;
final _defaultCastingToText = "Casting to $CAST_DEVICE_NAME_PLACEHOLDER";
const ExpandedControlsConnectedDeviceLabel({
/// Label to introduce cast device. Default is "Casting to {{cast_device_name}}",
/// where {{cast_device_name}} is replaced with the device name.
/// {{cast_device_name}} can be found in the constant CAST_DEVICE_NAME_PLACEHOLDER.
final String? castingToText;
ExpandedControlsConnectedDeviceLabel({
Key? key,
required this.castFramework,
this.castingToText = "Casting to",
this.castingToText,
}) : super(key: key);
String _replaceDeviceName(String textWithPlaceholder, String deviceName) {
return textWithPlaceholder.replaceAll(
CAST_DEVICE_NAME_PLACEHOLDER, deviceName);
}
@override
Widget build(BuildContext context) {
final textStyle =
@ -21,9 +34,11 @@ class ExpandedControlsConnectedDeviceLabel extends StatelessWidget {
future: castFramework.castContext.sessionManager.getCastDevice(),
builder: (context, snapshot) {
if (snapshot.hasData) {
var castDevice = snapshot.data;
var castDeviceName = castDevice?.friendlyName ?? "";
return Text("$castingToText $castDeviceName", style: textStyle);
final castDevice = snapshot.data;
final castDeviceName = castDevice?.friendlyName ?? "";
final baseCastLabel = castingToText ?? _defaultCastingToText;
final label = _replaceDeviceName(baseCastLabel, castDeviceName);
return Text(label, style: textStyle);
} else if (snapshot.hasError) {
debugPrint("error while retrieving cast device ${snapshot.error}");
return Text("");

View file

@ -6,3 +6,4 @@ export 'src/cast/widgets/expanded_controls/ExpandedControls.dart';
export 'src/cast/widgets/expanded_controls/ExpandedControlsPlayer.dart';
export 'src/cast/widgets/expanded_controls/ExpandedControlsProgress.dart';
export 'src/cast/widgets/expanded_controls/ExpandedControlsToolbar.dart';
export 'src/cast/widgets/expanded_controls/ExpandedControlsConnectedDeviceLabel.dart';