Skip to content

Commit

Permalink
Merge pull request stryder-dev#417 from bookshiyi/Add-PlatformCheckbox
Browse files Browse the repository at this point in the history
Add PlatformCheckbox
  • Loading branch information
aqwert authored Jul 2, 2023
2 parents 090fbc8 + 8e72415 commit cc7d0f2
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 0 deletions.
10 changes: 10 additions & 0 deletions example/lib/platform_page.dart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@ class PlatformPage extends StatelessWidget {
),
),
),
// ! PlatformCheckbox
PlatformWidgetExample(
title: 'PlatformCheckbox',
builder: (_, __) => StateProvider<bool>(
initialValue: false,
builder: (_, value, setValue) => PlatformCheckbox(
value: value,
onChanged: (newValue) {
setValue(newValue!);
},
// ! PlatformRadio
PlatformWidgetExample(
title: 'PlatformRadio',
Expand Down
1 change: 1 addition & 0 deletions lib/flutter_platform_widgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
190 changes: 190 additions & 0 deletions lib/src/platform_checkbox.dart
Original file line number Diff line number Diff line change
@@ -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<bool?>? 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<Color?>? fillColor;
final Color? hoverColor;
final MaterialStateProperty<Color?>? 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<CupertinoCheckbox, Checkbox> {
//Common
final Key? widgetKey;
final bool? value;
final bool tristate;
final ValueChanged<bool?>? 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<MaterialCheckboxData>? material;
final PlatformBuilder<CupertinoCheckboxData>? 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,
);
}
}

0 comments on commit cc7d0f2

Please sign in to comment.