import 'dart:math' as math; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../../../ui/adaptive/host.dart'; enum DiffForm { rendered, raw } bool usesCupertino(BuildContext context) => usesCupertinoHost(context); const diffFormLabels = { DiffForm.rendered: 'Rendered', DiffForm.raw: 'Raw patch', }; /// Conservatively choose between two stock controls using the current native /// label role and text scaler. This does not size or restyle either control. bool diffFormSegmentsFit(BuildContext context, double maxWidth) { final style = usesCupertino(context) ? CupertinoTheme.of(context).textTheme.textStyle : Theme.of(context).textTheme.labelLarge ?? DefaultTextStyle.of(context).style; var widest = 0.0; for (final label in diffFormLabels.values) { final painter = TextPainter( text: TextSpan(text: label, style: style), textDirection: Directionality.of(context), textScaler: MediaQuery.textScalerOf(context), maxLines: 1, )..layout(); widest = math.max(widest, painter.width); painter.dispose(); } return (widest + 48) * 2 <= maxWidth; } Widget diffFormControl( BuildContext context, { required DiffForm form, required ValueChanged onChanged, }) { if (usesCupertino(context)) { return CupertinoSlidingSegmentedControl( groupValue: form, onValueChanged: (value) { if (value != null) onChanged(value); }, children: { for (final entry in diffFormLabels.entries) entry.key: Padding( padding: const EdgeInsets.all(6), child: Text(entry.value), ), }, ); } return SegmentedButton( segments: [ for (final entry in diffFormLabels.entries) ButtonSegment(value: entry.key, label: Text(entry.value)), ], selected: {form}, showSelectedIcon: false, onSelectionChanged: (selection) { if (selection.isNotEmpty) onChanged(selection.first); }, ); } /// Native action sheet/bottom sheet when fixed segment labels cannot fit. Widget diffFormMenu( BuildContext context, { required DiffForm form, required ValueChanged onChanged, }) { if (usesCupertino(context)) { return Semantics( label: 'Diff form', button: true, child: CupertinoButton( padding: EdgeInsets.zero, onPressed: () => showCupertinoModalPopup( context: context, builder: (sheetContext) => CupertinoActionSheet( title: const Text('Diff form'), actions: [ for (final entry in diffFormLabels.entries) CupertinoActionSheetAction( isDefaultAction: entry.key == form, onPressed: () { Navigator.of(sheetContext).pop(); onChanged(entry.key); }, child: Text(entry.value), ), ], ), ), child: const Icon(CupertinoIcons.ellipsis), ), ); } return IconButton( tooltip: 'Diff form', icon: const Icon(Icons.more_vert), onPressed: () => showModalBottomSheet( context: context, showDragHandle: true, builder: (sheetContext) => SafeArea( child: Column( mainAxisSize: MainAxisSize.min, children: [ Padding( padding: const EdgeInsets.fromLTRB(16, 0, 16, 8), child: Align( alignment: AlignmentDirectional.centerStart, child: Text( 'Diff form', style: Theme.of(sheetContext).textTheme.titleMedium, ), ), ), for (final entry in diffFormLabels.entries) ListTile( title: Text(entry.value), selected: entry.key == form, onTap: () { Navigator.of(sheetContext).pop(); onChanged(entry.key); }, ), ], ), ), ), ); }