Keep message translation sheet below top safe area
This commit is contained in:
1 parent
9ac915bdf2
commit
86dd9d3560
2 files changed
+454
-88
No files matched your search
@@ -1,7 +1,10 @@
|
||||
import 'dart:math' as math;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../l10n/l10n.dart';
|
||||
import '../models/translation_support.dart';
|
||||
import 'mesh_ui.dart';
|
||||
|
||||
class MessageTranslationButton extends StatelessWidget {
|
||||
final bool enabled;
|
||||
@@ -38,15 +41,58 @@ Future<void> showMessageTranslationSheet({
|
||||
required ValueChanged<bool> onEnabledChanged,
|
||||
required ValueChanged<String?> onLanguageSelected,
|
||||
}) {
|
||||
return showModalBottomSheet<void>(
|
||||
context: context,
|
||||
isScrollControlled: true,
|
||||
builder: (context) => _MessageTranslationSheet(
|
||||
enabled: enabled,
|
||||
selectedLanguageCode: selectedLanguageCode,
|
||||
onEnabledChanged: onEnabledChanged,
|
||||
onLanguageSelected: onLanguageSelected,
|
||||
),
|
||||
final callerMedia = MediaQuery.of(context);
|
||||
final navMedia =
|
||||
MediaQuery.maybeOf(Navigator.of(context).context) ?? callerMedia;
|
||||
final callerBottom = math.max(
|
||||
callerMedia.viewInsets.bottom,
|
||||
math.max(callerMedia.viewPadding.bottom, callerMedia.padding.bottom),
|
||||
);
|
||||
final navBottom = math.max(
|
||||
navMedia.viewInsets.bottom,
|
||||
math.max(navMedia.viewPadding.bottom, navMedia.padding.bottom),
|
||||
);
|
||||
final extraBottom = math.max(0.0, callerBottom - navBottom);
|
||||
final safeTop = math.max(
|
||||
math.max(callerMedia.viewPadding.top, callerMedia.padding.top),
|
||||
math.max(navMedia.viewPadding.top, navMedia.padding.top),
|
||||
);
|
||||
final bottomSafeInset = math.max(
|
||||
math.max(callerMedia.viewPadding.bottom, callerMedia.padding.bottom),
|
||||
math.max(navMedia.viewPadding.bottom, navMedia.padding.bottom),
|
||||
);
|
||||
|
||||
return showMeshSheet<void>(
|
||||
context,
|
||||
builder: (sheetContext) {
|
||||
final sheetMedia = MediaQuery.of(sheetContext);
|
||||
final screenHeight = math.min(
|
||||
callerMedia.size.height,
|
||||
sheetMedia.size.height,
|
||||
);
|
||||
final maxSheetHeight = math.max(
|
||||
0.0,
|
||||
math.min(
|
||||
screenHeight * 0.82 - bottomSafeInset,
|
||||
screenHeight - safeTop - math.max(callerBottom, navBottom),
|
||||
),
|
||||
);
|
||||
return MediaQuery(
|
||||
data: sheetMedia.copyWith(textScaler: callerMedia.textScaler),
|
||||
child: Padding(
|
||||
padding: EdgeInsets.only(bottom: extraBottom),
|
||||
child: ConstrainedBox(
|
||||
constraints: BoxConstraints(maxHeight: maxSheetHeight),
|
||||
child: _MessageTranslationSheet(
|
||||
enabled: enabled,
|
||||
selectedLanguageCode: selectedLanguageCode,
|
||||
onEnabledChanged: onEnabledChanged,
|
||||
onLanguageSelected: onLanguageSelected,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@@ -69,6 +115,8 @@ class _MessageTranslationSheet extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _MessageTranslationSheetState extends State<_MessageTranslationSheet> {
|
||||
final GlobalKey _switchKey = GlobalKey();
|
||||
final GlobalKey _searchKey = GlobalKey();
|
||||
late final TextEditingController _searchController;
|
||||
late bool _localEnabled;
|
||||
late String? _localSelectedLanguageCode;
|
||||
@@ -98,95 +146,166 @@ class _MessageTranslationSheetState extends State<_MessageTranslationSheet> {
|
||||
});
|
||||
}
|
||||
|
||||
Widget _buildSwitchTile(BuildContext context) {
|
||||
return KeyedSubtree(
|
||||
key: _switchKey,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16),
|
||||
child: SwitchListTile(
|
||||
contentPadding: EdgeInsets.zero,
|
||||
title: Text(context.l10n.translation_translateBeforeSending),
|
||||
subtitle: Text(
|
||||
_localEnabled
|
||||
? context.l10n.translation_composerEnabledHint
|
||||
: context.l10n.translation_composerDisabledHint,
|
||||
),
|
||||
value: _localEnabled,
|
||||
onChanged: (value) {
|
||||
setState(() => _localEnabled = value);
|
||||
widget.onEnabledChanged(value);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildSearchField(BuildContext context) {
|
||||
return KeyedSubtree(
|
||||
key: _searchKey,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.fromLTRB(16, 8, 16, 12),
|
||||
child: TextField(
|
||||
controller: _searchController,
|
||||
onChanged: _updateFilter,
|
||||
decoration: InputDecoration(
|
||||
labelText: context.l10n.translation_targetLanguage,
|
||||
prefixIcon: const Icon(Icons.search),
|
||||
border: const OutlineInputBorder(),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildLanguageTile(BuildContext context, int index) {
|
||||
if (index == 0) {
|
||||
final selected = _localSelectedLanguageCode == null;
|
||||
return ListTile(
|
||||
contentPadding: const EdgeInsets.symmetric(horizontal: 16),
|
||||
leading: Icon(
|
||||
selected ? Icons.radio_button_checked : Icons.radio_button_unchecked,
|
||||
),
|
||||
title: Text(context.l10n.translation_useAppLanguage),
|
||||
onTap: () {
|
||||
setState(() => _localSelectedLanguageCode = null);
|
||||
widget.onLanguageSelected(null);
|
||||
Navigator.pop(context);
|
||||
},
|
||||
);
|
||||
}
|
||||
final option = _filtered[index - 1];
|
||||
final selected = option.code == _localSelectedLanguageCode;
|
||||
return ListTile(
|
||||
contentPadding: const EdgeInsets.symmetric(horizontal: 16),
|
||||
leading: Icon(
|
||||
selected ? Icons.radio_button_checked : Icons.radio_button_unchecked,
|
||||
),
|
||||
title: Text(option.label),
|
||||
subtitle: Text(option.code.toUpperCase()),
|
||||
onTap: () {
|
||||
setState(() => _localSelectedLanguageCode = option.code);
|
||||
widget.onLanguageSelected(option.code);
|
||||
Navigator.pop(context);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return SafeArea(
|
||||
child: Padding(
|
||||
padding: EdgeInsets.only(
|
||||
left: 16,
|
||||
right: 16,
|
||||
top: 16,
|
||||
bottom: 16 + MediaQuery.of(context).viewInsets.bottom,
|
||||
),
|
||||
child: Column(
|
||||
final header = BottomSheetHeader(
|
||||
title: context.l10n.translation_messageTranslation,
|
||||
);
|
||||
final switchTile = _buildSwitchTile(context);
|
||||
final searchField = _buildSearchField(context);
|
||||
final itemCount = _filtered.length + 1;
|
||||
|
||||
return LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
final scaler = MediaQuery.textScalerOf(context);
|
||||
final narrow = constraints.maxWidth < 360;
|
||||
final minPinnedHeight = scaler.scale(narrow ? 360 : 300);
|
||||
|
||||
if (constraints.maxHeight < scaler.scale(150)) {
|
||||
return CustomScrollView(
|
||||
shrinkWrap: true,
|
||||
keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
|
||||
slivers: [
|
||||
SliverToBoxAdapter(child: header),
|
||||
SliverToBoxAdapter(child: switchTile),
|
||||
SliverToBoxAdapter(child: searchField),
|
||||
SliverPadding(
|
||||
padding: const EdgeInsets.only(bottom: 12),
|
||||
sliver: SliverList.list(
|
||||
children: [
|
||||
for (var i = 0; i < itemCount; i++)
|
||||
_buildLanguageTile(context, i),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
if (constraints.maxHeight < minPinnedHeight) {
|
||||
return Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
header,
|
||||
Flexible(
|
||||
child: CustomScrollView(
|
||||
shrinkWrap: true,
|
||||
keyboardDismissBehavior:
|
||||
ScrollViewKeyboardDismissBehavior.onDrag,
|
||||
slivers: [
|
||||
SliverToBoxAdapter(child: switchTile),
|
||||
SliverToBoxAdapter(child: searchField),
|
||||
SliverPadding(
|
||||
padding: const EdgeInsets.only(bottom: 12),
|
||||
sliver: SliverList.list(
|
||||
children: [
|
||||
for (var i = 0; i < itemCount; i++)
|
||||
_buildLanguageTile(context, i),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
return Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Text(
|
||||
context.l10n.translation_messageTranslation,
|
||||
style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
SwitchListTile(
|
||||
contentPadding: EdgeInsets.zero,
|
||||
title: Text(context.l10n.translation_translateBeforeSending),
|
||||
subtitle: Text(
|
||||
_localEnabled
|
||||
? context.l10n.translation_composerEnabledHint
|
||||
: context.l10n.translation_composerDisabledHint,
|
||||
),
|
||||
value: _localEnabled,
|
||||
onChanged: (value) {
|
||||
setState(() => _localEnabled = value);
|
||||
widget.onEnabledChanged(value);
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
TextField(
|
||||
controller: _searchController,
|
||||
onChanged: _updateFilter,
|
||||
decoration: InputDecoration(
|
||||
labelText: context.l10n.translation_targetLanguage,
|
||||
prefixIcon: const Icon(Icons.search),
|
||||
border: const OutlineInputBorder(),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
header,
|
||||
switchTile,
|
||||
searchField,
|
||||
Flexible(
|
||||
child: ListView.builder(
|
||||
shrinkWrap: true,
|
||||
itemCount: _filtered.length + 1,
|
||||
itemBuilder: (context, index) {
|
||||
if (index == 0) {
|
||||
final selected = _localSelectedLanguageCode == null;
|
||||
return ListTile(
|
||||
contentPadding: EdgeInsets.zero,
|
||||
leading: Icon(
|
||||
selected
|
||||
? Icons.radio_button_checked
|
||||
: Icons.radio_button_unchecked,
|
||||
),
|
||||
title: Text(context.l10n.translation_useAppLanguage),
|
||||
onTap: () {
|
||||
setState(() => _localSelectedLanguageCode = null);
|
||||
widget.onLanguageSelected(null);
|
||||
Navigator.pop(context);
|
||||
},
|
||||
);
|
||||
}
|
||||
final option = _filtered[index - 1];
|
||||
final selected = option.code == _localSelectedLanguageCode;
|
||||
return ListTile(
|
||||
contentPadding: EdgeInsets.zero,
|
||||
leading: Icon(
|
||||
selected
|
||||
? Icons.radio_button_checked
|
||||
: Icons.radio_button_unchecked,
|
||||
),
|
||||
title: Text(option.label),
|
||||
subtitle: Text(option.code.toUpperCase()),
|
||||
onTap: () {
|
||||
setState(() => _localSelectedLanguageCode = option.code);
|
||||
widget.onLanguageSelected(option.code);
|
||||
Navigator.pop(context);
|
||||
},
|
||||
);
|
||||
},
|
||||
keyboardDismissBehavior:
|
||||
ScrollViewKeyboardDismissBehavior.onDrag,
|
||||
padding: const EdgeInsets.only(bottom: 12),
|
||||
itemCount: itemCount,
|
||||
itemBuilder: _buildLanguageTile,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user