-
-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Popup Menu now uses the iOS actions sheet (#5494)
* iOS popup menu (cherry picked from commit 493f3d2) * Reorder mandatory fields
- Loading branch information
Showing
7 changed files
with
157 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
packages/smooth_app/lib/widgets/smooth_menu_button.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:flutter/cupertino.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_gen/gen_l10n/app_localizations.dart'; | ||
|
||
/// A Button similar to a [PopupMenuButton] for non Apple platforms. | ||
/// On iOS and macOS, it's still an [IconButton], but that opens a | ||
/// [CupertinoActionSheet]. | ||
class SmoothPopupMenuButton<T> extends StatefulWidget { | ||
const SmoothPopupMenuButton({ | ||
required this.onSelected, | ||
required this.itemBuilder, | ||
this.actionsTitle, | ||
this.buttonIcon, | ||
this.buttonLabel, | ||
}) : assert(buttonLabel == null || buttonLabel.length > 0), | ||
assert(actionsTitle == null || actionsTitle.length > 0); | ||
|
||
final void Function(T value) onSelected; | ||
final Iterable<SmoothPopupMenuItem<T>> Function(BuildContext context) | ||
itemBuilder; | ||
final Icon? buttonIcon; | ||
final String? buttonLabel; | ||
final String? actionsTitle; | ||
|
||
@override | ||
State<SmoothPopupMenuButton<T>> createState() => | ||
_SmoothPopupMenuButtonState<T>(); | ||
} | ||
|
||
class _SmoothPopupMenuButtonState<T> extends State<SmoothPopupMenuButton<T>> { | ||
@override | ||
Widget build(BuildContext context) { | ||
if (Platform.isIOS || Platform.isMacOS) { | ||
return IconButton( | ||
icon: widget.buttonIcon ?? Icon(Icons.adaptive.more), | ||
tooltip: widget.buttonLabel ?? | ||
MaterialLocalizations.of(context).showMenuTooltip, | ||
onPressed: _openModalSheet, | ||
); | ||
} else { | ||
return PopupMenuButton<T>( | ||
icon: widget.buttonIcon ?? Icon(Icons.adaptive.more), | ||
tooltip: widget.buttonLabel ?? | ||
MaterialLocalizations.of(context).showMenuTooltip, | ||
onSelected: widget.onSelected, | ||
itemBuilder: (BuildContext context) { | ||
return widget.itemBuilder(context).map((SmoothPopupMenuItem<T> item) { | ||
return PopupMenuItem<T>( | ||
value: item.value, | ||
enabled: item.enabled, | ||
child: ListTile( | ||
leading: Icon(item.icon), | ||
title: Text(item.label), | ||
), | ||
); | ||
}).toList(growable: false); | ||
}, | ||
); | ||
} | ||
} | ||
|
||
// iOS and macOS behavior | ||
void _openModalSheet() { | ||
showCupertinoModalPopup( | ||
context: context, | ||
builder: (BuildContext context) { | ||
return CupertinoActionSheet( | ||
title: Text( | ||
widget.actionsTitle ?? | ||
AppLocalizations.of(context).menu_button_list_actions, | ||
), | ||
actions: widget | ||
.itemBuilder(context) | ||
.where((SmoothPopupMenuItem<T> item) => item.enabled) | ||
.map((SmoothPopupMenuItem<T> item) { | ||
return CupertinoActionSheetAction( | ||
isDefaultAction: | ||
item.type == SmoothPopupMenuItemType.highlighted, | ||
isDestructiveAction: | ||
item.type == SmoothPopupMenuItemType.destructive, | ||
onPressed: () { | ||
widget.onSelected(item.value); | ||
Navigator.of(context).maybePop(); | ||
}, | ||
child: Text(item.label), | ||
); | ||
}).toList(growable: false), | ||
); | ||
}); | ||
} | ||
} | ||
|
||
class SmoothPopupMenuItem<T> { | ||
const SmoothPopupMenuItem({ | ||
required this.value, | ||
required this.label, | ||
this.icon, | ||
this.type, | ||
this.enabled = true, | ||
}) : assert(label.length > 0); | ||
|
||
final T value; | ||
final String label; | ||
final IconData? icon; | ||
final SmoothPopupMenuItemType? type; | ||
final bool enabled; | ||
} | ||
|
||
/// The style of an item in the menu | ||
/// On Material platforms, all values behave the same. | ||
/// On iOS, the [highlighted] value is in black and [destructive] in red. | ||
enum SmoothPopupMenuItemType { | ||
normal, | ||
highlighted, | ||
destructive, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters