diff --git a/example/lib/platform_page.dart.dart b/example/lib/platform_page.dart.dart index a44c0630..f427e23a 100644 --- a/example/lib/platform_page.dart.dart +++ b/example/lib/platform_page.dart.dart @@ -106,6 +106,16 @@ class PlatformPage extends StatelessWidget { ), ), ), + // ! PlatformCheckbox + PlatformWidgetExample( + title: 'PlatformCheckbox', + builder: (_, __) => StateProvider( + initialValue: false, + builder: (_, value, setValue) => PlatformCheckbox( + value: value, + onChanged: (newValue) { + setValue(newValue!); + }, // ! PlatformRadio PlatformWidgetExample( title: 'PlatformRadio', diff --git a/lib/flutter_platform_widgets.dart b/lib/flutter_platform_widgets.dart index 8a4c5f8a..b033d502 100644 --- a/lib/flutter_platform_widgets.dart +++ b/lib/flutter_platform_widgets.dart @@ -24,6 +24,7 @@ export 'src/platform_alert_dialog.dart'; export 'src/platform_app.dart'; export 'src/platform_app_bar.dart'; export 'src/platform_circular_progress_indicator.dart'; +export 'src/platform_checkbox.dart'; export 'src/platform_date_picker.dart'; export 'src/platform_dialog_action.dart'; export 'src/platform_elevated_button.dart'; diff --git a/lib/src/platform_checkbox.dart b/lib/src/platform_checkbox.dart new file mode 100644 index 00000000..2c14d4e7 --- /dev/null +++ b/lib/src/platform_checkbox.dart @@ -0,0 +1,190 @@ +/* + * flutter_platform_widgets + * Copyright (c) 2018 Lance Johnstone. All rights reserved. + * See LICENSE for distribution and usage details. + */ + +import 'package:flutter/cupertino.dart' show CupertinoCheckbox; +import 'package:flutter/material.dart' + show Checkbox, MaterialTapTargetSize, MaterialStateProperty, VisualDensity; +import 'package:flutter/widgets.dart'; + +import 'platform.dart'; +import 'widget_base.dart'; + +abstract class _BaseData { + _BaseData({ + this.widgetKey, + this.value, + this.tristate = false, + this.onChanged, + this.activeColor, + this.checkColor, + this.focusColor, + this.focusNode, + this.autofocus = false, + this.shape, + this.side, + }); + final Key? widgetKey; + final bool? value; + final bool tristate; + final ValueChanged? onChanged; + final Color? activeColor; + final Color? checkColor; + final Color? focusColor; + final FocusNode? focusNode; + final bool autofocus; + final OutlinedBorder? shape; + final BorderSide? side; +} + +class MaterialCheckboxData extends _BaseData { + MaterialCheckboxData({ + // Common + super.widgetKey, + super.value, + super.tristate = false, + super.onChanged, + super.activeColor, + super.checkColor, + super.focusColor, + super.focusNode, + super.autofocus = false, + super.shape, + super.side, + //Material + this.mouseCursor, + this.fillColor, + this.hoverColor, + this.overlayColor, + this.splashRadius, + this.materialTapTargetSize, + this.visualDensity, + this.isError = false, + }); + + final MouseCursor? mouseCursor; + final MaterialStateProperty? fillColor; + final Color? hoverColor; + final MaterialStateProperty? overlayColor; + final double? splashRadius; + final MaterialTapTargetSize? materialTapTargetSize; + final VisualDensity? visualDensity; + final bool isError; +} + +class CupertinoCheckboxData extends _BaseData { + CupertinoCheckboxData({ + //Common + super.widgetKey, + super.value, + super.tristate = false, + super.onChanged, + super.activeColor, + super.checkColor, + super.focusColor, + super.focusNode, + super.autofocus = false, + super.shape, + super.side, + + //Cupertino + this.inactiveColor, + }); + + final Color? inactiveColor; +} + +class PlatformCheckbox extends PlatformWidgetBase { + //Common + final Key? widgetKey; + final bool? value; + final bool tristate; + final ValueChanged? onChanged; + final Color? activeColor; + final Color? checkColor; + final Color? focusColor; + final FocusNode? focusNode; + final bool autofocus; + final OutlinedBorder? shape; + final BorderSide? side; + + //Platform + final PlatformBuilder? material; + final PlatformBuilder? cupertino; + + PlatformCheckbox({ + //Common + super.key, + this.widgetKey, + required this.onChanged, + this.value, + this.tristate = false, + this.activeColor, + this.checkColor, + this.focusColor, + this.focusNode, + this.autofocus = false, + this.shape, + this.side, + //Platform + this.material, + this.cupertino, + }); + + @override + Checkbox createMaterialWidget(BuildContext context) { + final data = material?.call(context, platform(context)); + final value = data?.value ?? this.value; + final tristate = data?.tristate ?? this.tristate; + assert(tristate || value != null); + return Checkbox( + //Common + key: data?.widgetKey ?? widgetKey, + value: value, + tristate: tristate, + onChanged: data?.onChanged ?? onChanged, + activeColor: data?.activeColor ?? activeColor, + checkColor: data?.checkColor ?? checkColor, + focusColor: data?.focusColor ?? focusColor, + focusNode: data?.focusNode ?? focusNode, + autofocus: data?.autofocus ?? autofocus, + shape: data?.shape ?? shape, + side: data?.side ?? side, + //Material + mouseCursor: data?.mouseCursor, + fillColor: data?.fillColor, + hoverColor: data?.hoverColor, + overlayColor: data?.overlayColor, + splashRadius: data?.splashRadius, + materialTapTargetSize: data?.materialTapTargetSize, + visualDensity: data?.visualDensity, + isError: data?.isError ?? false, + ); + } + + @override + CupertinoCheckbox createCupertinoWidget(BuildContext context) { + final data = cupertino?.call(context, platform(context)); + final value = data?.value ?? this.value; + final tristate = data?.tristate ?? this.tristate; + assert(tristate || value != null); + return CupertinoCheckbox( + //Common + key: data?.widgetKey ?? widgetKey, + value: value, + tristate: tristate, + onChanged: data?.onChanged ?? onChanged, + activeColor: data?.activeColor ?? activeColor, + checkColor: data?.checkColor ?? checkColor, + focusColor: data?.focusColor ?? focusColor, + focusNode: data?.focusNode ?? focusNode, + autofocus: data?.autofocus ?? autofocus, + shape: data?.shape ?? shape, + side: data?.side ?? side, + //Cupertino + inactiveColor: data?.inactiveColor, + ); + } +}