Skip to content

Commit

Permalink
feat: PlatformDropdownMenu support
Browse files Browse the repository at this point in the history
  • Loading branch information
KRTirtho committed Oct 19, 2022
1 parent 943685d commit fa91f64
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .flutter-plugins-dependencies
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[],"android":[],"macos":[{"name":"macos_ui","path":"/opt/flutter/.pub-cache/hosted/pub.dartlang.org/macos_ui-1.7.5/","native_build":true,"dependencies":[]}],"linux":[],"windows":[],"web":[]},"dependencyGraph":[{"name":"macos_ui","dependencies":[]}],"date_created":"2022-10-13 16:13:27.653277","version":"3.3.4"}
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[],"android":[],"macos":[{"name":"macos_ui","path":"/opt/flutter/.pub-cache/hosted/pub.dartlang.org/macos_ui-1.7.5/","native_build":true,"dependencies":[]}],"linux":[],"windows":[],"web":[]},"dependencyGraph":[{"name":"macos_ui","dependencies":[]}],"date_created":"2022-10-19 22:38:46.370789","version":"3.3.4"}
20 changes: 20 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:platform_ui/platform_ui.dart';

void main() {
platform = TargetPlatform.windows;
runApp(const MyApp());
}

Expand Down Expand Up @@ -130,6 +131,25 @@ class _MyHomePageState extends State<MyHomePage> {
backgroundColor: Colors.blue,
focusedBackgroundColor: Colors.amber,
),
PlatformDropDownMenu(
onChanged: (value) {},
dropdownColor: Colors.orange,
elevation: 20,
items: [
PlatformDropDownMenuItem(
child: const Text("LOL"),
value: "LOL",
),
PlatformDropDownMenuItem(
child: const Text("Cool"),
value: "Cool",
),
PlatformDropDownMenuItem(
child: const Text("Foul"),
value: "Foul",
),
],
),
],
),
),
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ packages:
name: fluent_ui
url: "https://pub.dartlang.org"
source: hosted
version: "3.12.0"
version: "4.0.2"
flutter:
dependency: "direct main"
description: flutter
Expand Down
1 change: 1 addition & 0 deletions lib/platform_ui.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export 'src/platform_text_button.dart';
export 'src/platform_icon_button.dart';
export 'src/platform_switch.dart';
export 'src/platform_text_field.dart';
export 'src/platform_drop_down_menu.dart';
11 changes: 9 additions & 2 deletions lib/src/platform_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:fluent_ui/fluent_ui.dart' hide ThemeData;
import 'package:fluent_ui/fluent_ui.dart' as FluentUI;
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:macos_ui/macos_ui.dart';
import 'package:platform_ui/src/platform_mixin.dart';

Expand Down Expand Up @@ -226,10 +227,16 @@ class PlatformApp extends StatelessWidget with PlatformMixin<Widget> {
themeMode: themeMode,
color: color,
locale: locale,
localizationsDelegates: localizationsDelegates,
localizationsDelegates: [
...(localizationsDelegates ?? []),
FluentLocalizations.delegate,
],
supportedLocales: [
...supportedLocales,
const Locale('en', 'US'),
],
localeListResolutionCallback: localeListResolutionCallback,
localeResolutionCallback: localeResolutionCallback,
supportedLocales: supportedLocales,
showPerformanceOverlay: showPerformanceOverlay,
checkerboardRasterCacheImages: checkerboardRasterCacheImages,
checkerboardOffscreenLayers: checkerboardOffscreenLayers,
Expand Down
175 changes: 175 additions & 0 deletions lib/src/platform_drop_down_menu.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
import 'package:flutter/material.dart';
import 'package:macos_ui/macos_ui.dart';
import 'package:platform_ui/platform_ui.dart';
import 'package:fluent_ui/fluent_ui.dart' as FluentUI;

class PlatformDropDownMenuItem<T> {
final Widget child;
final T value;

/// Doesn't work in `windows`
final bool enabled;

/// Doesn't work in `windows`
final AlignmentGeometry alignment;
void Function()? onTap;

PlatformDropDownMenuItem({
required this.child,
required this.value,
this.alignment = AlignmentDirectional.centerStart,
this.enabled = true,
this.onTap,
});

DropdownMenuItem<T> android() {
return DropdownMenuItem<T>(
enabled: enabled,
value: value,
alignment: alignment,
onTap: onTap,
child: child,
);
}

MacosPopupMenuItem ios() {
return macos();
}

DropdownMenuItem linux() {
return android();
}

MacosPopupMenuItem<T> macos() {
return MacosPopupMenuItem<T>(
enabled: enabled,
value: value,
alignment: alignment,
onTap: onTap,
child: child,
);
}

FluentUI.ComboBoxItem<T> windows() {
return FluentUI.ComboBoxItem<T>(
value: value,
onTap: onTap,
child: child,
);
}
}

class PlatformDropDownMenu<T> extends StatelessWidget
with PlatformMixin<Widget> {
final List<PlatformDropDownMenuItem<T>> items;
final T? value;
final Widget? hint;
final Widget? disabledHint;
final ValueChanged<T?>? onChanged;
final VoidCallback? onTap;
final DropdownButtonBuilder? selectedItemBuilder;
final TextStyle? style;
final double? itemHeight;
final Color? focusColor;
final FocusNode? focusNode;
final bool autofocus;
final Color? dropdownColor;
final double? menuMaxHeight;
final AlignmentGeometry alignment;
final int elevation;

const PlatformDropDownMenu({
required this.items,
this.selectedItemBuilder,
this.value,
this.hint,
this.disabledHint,
required this.onChanged,
this.onTap,
this.style,
this.itemHeight = kMinInteractiveDimension,
this.focusColor,
this.focusNode,
this.autofocus = false,
this.dropdownColor,
this.menuMaxHeight,
this.elevation = 8,
this.alignment = AlignmentDirectional.centerStart,
Key? key,
}) : super(key: key);

@override
Widget build(BuildContext context) {
return getPlatformType(context);
}

@override
Widget android(BuildContext context) {
return DropdownButton<T>(
items: items.map((item) => item.android()).toList(),
value: value,
hint: hint,
onChanged: onChanged,
onTap: onTap,
selectedItemBuilder: selectedItemBuilder,
style: style,
itemHeight: itemHeight,
focusColor: focusColor,
focusNode: focusNode,
autofocus: autofocus,
dropdownColor: dropdownColor,
menuMaxHeight: menuMaxHeight,
alignment: alignment,
elevation: elevation,
);
}

@override
Widget ios(BuildContext context) {
return macos(context);
}

@override
Widget linux(BuildContext context) {
return android(context);
}

@override
Widget macos(BuildContext context) {
return MacosPopupButton<T>(
items: items.map((item) => item.macos()).toList(),
value: value,
hint: hint,
disabledHint: disabledHint,
onChanged: onChanged,
onTap: onTap,
selectedItemBuilder: selectedItemBuilder,
style: style,
itemHeight: itemHeight,
focusNode: focusNode,
autofocus: autofocus,
popupColor: dropdownColor,
menuMaxHeight: menuMaxHeight,
alignment: alignment,
);
}

@override
Widget windows(BuildContext context) {
return FluentUI.ComboBox<T>(
items: items.map((item) => item.windows()).toList(),
focusNode: focusNode,
autofocus: autofocus,
popupColor: dropdownColor,
disabledPlaceholder: disabledHint,
focusColor: focusColor,
onChanged: onChanged,
onTap: onTap,
placeholder: hint,
selectedItemBuilder: selectedItemBuilder,
elevation: elevation,
style: style,
value: value,
);
}
}
4 changes: 3 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ environment:
flutter: ">=1.17.0"

dependencies:
fluent_ui: ^3.12.0
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
fluent_ui: ^4.0.2
macos_ui: ^1.7.5

dev_dependencies:
Expand Down

0 comments on commit fa91f64

Please sign in to comment.