Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] #652842 - Localization choice in app #262

Merged
merged 3 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
231 changes: 162 additions & 69 deletions lib/src/widgets/chat/ds_location_message_bubble.widget.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:map_launcher/map_launcher.dart';

import '../../enums/ds_align.enum.dart';
import '../../enums/ds_border_radius.enum.dart';
import '../../models/ds_message_bubble_style.model.dart';
import '../../models/ds_reply_content.model.dart';
import '../../services/ds_auth.service.dart';
import '../../services/ds_bottom_sheet.service.dart';
import '../../themes/colors/ds_colors.theme.dart';
import '../../themes/icons/ds_icons.dart';
import '../../utils/ds_utils.util.dart';
import '../animations/ds_spinner_loading.widget.dart';
import '../buttons/ds_icon_button.widget.dart';
import '../texts/ds_body_text.widget.dart';
import '../texts/ds_headline_large_text.widget.dart';
import '../utils/ds_cached_network_image_view.widget.dart';
import '../utils/ds_divider.widget.dart';
import 'ds_message_bubble.widget.dart';

class DSLocationMessageBubble extends StatelessWidget {
class DSLocationMessageBubble extends StatefulWidget {
final DSAlign align;
final DSMessageBubbleStyle style;
final String? title;
Expand All @@ -35,81 +40,169 @@ class DSLocationMessageBubble extends StatelessWidget {
}) : style = style ?? DSMessageBubbleStyle();

@override
Widget build(BuildContext context) {
final foregroundColor = style.isLightBubbleBackground(align)
State<DSLocationMessageBubble> createState() =>
_DSLocationMessageBubbleState();
}

class _DSLocationMessageBubbleState extends State<DSLocationMessageBubble> {
late final Future<List<AvailableMap>> _installedMaps;
late final bool _hasValidCoordinates;
late final Color _foregroundColor;
late final double? _doubleLatitude;
late final double? _doubleLongitude;

@override
void initState() {
super.initState();

_foregroundColor = widget.style.isLightBubbleBackground(widget.align)
? DSColors.neutralDarkCity
: DSColors.neutralLightSnow;

final lat = double.tryParse(latitude);
final long = double.tryParse(longitude);

final hasValidCoordinates = lat != null && long != null;
return GestureDetector(
onTap: () async {
final availableMaps = await MapLauncher.installedMaps;

if (hasValidCoordinates) {
await availableMaps.first.showMarker(
coords: Coords(lat, long),
title: title ?? '',
);
}
},
child: DSMessageBubble(
shouldUseDefaultSize: true,
defaultMaxSize: DSUtils.bubbleMinSize,
defaultMinSize: DSUtils.bubbleMinSize,
borderRadius: borderRadius,
replyContent: replyContent,
padding: EdgeInsets.zero,
align: align,
style: style,
child: Padding(
padding: replyContent == null
? EdgeInsets.zero
: const EdgeInsets.only(top: 8.0),
_doubleLatitude = double.tryParse(widget.latitude);
_doubleLongitude = double.tryParse(widget.longitude);

_hasValidCoordinates = _doubleLatitude != null && _doubleLongitude != null;

_installedMaps = MapLauncher.installedMaps;
}

@override
Widget build(BuildContext context) => GestureDetector(
onTap: _hasValidCoordinates ? _openMapList : null,
child: DSMessageBubble(
shouldUseDefaultSize: true,
defaultMaxSize: DSUtils.bubbleMinSize,
defaultMinSize: DSUtils.bubbleMinSize,
borderRadius: widget.borderRadius,
replyContent: widget.replyContent,
padding: EdgeInsets.zero,
align: widget.align,
style: widget.style,
child: _buildBody(),
),
);

Widget _buildBody() => Padding(
padding: widget.replyContent == null
? EdgeInsets.zero
: const EdgeInsets.only(top: 8.0),
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
hasValidCoordinates
? DSCachedNetworkImageView(
url:
'https://maps.googleapis.com/maps/api/staticmap?&size=360x360&markers=$latitude,$longitude&key=${DSAuthService.googleKey}',
placeholder: (_, __) => _buildLoading(),
align: align,
style: style,
)
: SizedBox(
width: 240,
height: 240,
child: Icon(
DSIcons.file_image_broken_outline,
size: 80,
color: style.isLightBubbleBackground(align)
? DSColors.neutralMediumElephant
: DSColors.neutralMediumCloud,
),
),
if (title?.isNotEmpty ?? false)
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 16.0,
vertical: 8.0,
),
child: Align(
alignment: Alignment.topLeft,
child: DSBodyText(
title!,
color: foregroundColor,
isSelectable: true,
overflow: TextOverflow.visible,
),
),
),
_hasValidCoordinates
? _buildThumbnail()
: _buildBrokenThumbnail(),
if (widget.title?.isNotEmpty ?? false) _buildTitle(),
],
),
),
);

Widget _buildTitle() => Padding(
padding: const EdgeInsets.symmetric(
horizontal: 16.0,
vertical: 8.0,
),
child: Align(
alignment: Alignment.topLeft,
child: DSBodyText(
widget.title!,
color: _foregroundColor,
isSelectable: true,
overflow: TextOverflow.visible,
),
),
);

Widget _buildThumbnail() => DSCachedNetworkImageView(
url:
'https://maps.googleapis.com/maps/api/staticmap?&size=360x360&markers=${widget.latitude},${widget.longitude}&key=${DSAuthService.googleKey}',
placeholder: (_, __) => _buildLoading(),
align: widget.align,
style: widget.style,
);

Widget _buildBrokenThumbnail() => SizedBox(
width: 240,
height: 240,
child: Icon(
DSIcons.file_image_broken_outline,
size: 80,
color: widget.style.isLightBubbleBackground(widget.align)
? DSColors.neutralMediumElephant
: DSColors.neutralMediumCloud,
),
);

void _openMapList() {
DSBottomSheetService(
context: context,
fixedHeader: _buildBottomSheetHeader(),
builder: (_) => _buildBottomSheetBody(),
).show();
}

Widget _buildBottomSheetHeader() {
return Column(
children: [
Padding(
padding: const EdgeInsets.all(16.0),
child: SafeArea(
top: false,
bottom: false,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
DSHeadlineLargeText(
'Selecione uma ação',
),
DSIconButton(
onPressed: () {
Get.back();
},
icon: const Icon(DSIcons.close_outline,
color: DSColors.neutralDarkRooftop),
),
],
),
),
),
const DSDivider()
],
);
}

Widget _buildBottomSheetBody() {
return SafeArea(
child: FutureBuilder(
future: _installedMaps,
builder: (_, snapshot) => snapshot.hasData && snapshot.data!.isNotEmpty
? ListView.builder(
shrinkWrap: true,
itemCount: snapshot.data!.length,
itemBuilder: (_, index) {
final map = snapshot.data![index];

return ListTile(
onTap: () {
map.showMarker(
coords: Coords(
_doubleLatitude!,
_doubleLongitude!,
),
title: widget.title ?? '',
);

Get.back();
},
title: DSBodyText(
'Abrir com ${map.mapName}',
),
);
},
)
: _buildLoading(),
),
);
}
Expand All @@ -123,7 +216,7 @@ class DSLocationMessageBubble extends StatelessWidget {
),
child: Center(
child: DSSpinnerLoading(
color: style.isLightBubbleBackground(align)
color: widget.style.isLightBubbleBackground(widget.align)
? DSColors.primaryNight
: DSColors.neutralLightSnow,
size: 32.0,
Expand Down
Loading
Loading