Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented checkbox #24

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## [0.0.3]

* Implemented `Checkbox`
* Implemented `ProgressCircle` and `ProgressBar`
* Implemented the `Switch` widget

Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ dragging left or right. See the documentation for all customization options.
<img src="https://imgur.com/jTPXGuq.gif" width="75%"/>

# Buttons

## Checkbox

| off | on | mixed |
| --- | -- | ----- |
| ![](https://developer.apple.com/design/human-interface-guidelines/macos/images/CheckBoxes_Deselected.svg) | ![](https://developer.apple.com/design/human-interface-guidelines/macos/images/CheckBoxes_Selected.svg) | ![](https://developer.apple.com/design/human-interface-guidelines/macos/images/CheckBoxes_Mixed.svg) |

## Switch
<img src="https://imgur.com/IBh5jkz.jpg" width="50%" height="50%"/>

Expand Down
6 changes: 5 additions & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,16 @@ class _DemoState extends State<Demo> {
child: Text('Sidebar'),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Switch(
value: value,
onChanged: (v) => setState(() => value = v),
),
Checkbox(
value: value,
onChanged: (v) => setState(() => value = v),
),
],
),
);
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.0.2"
version: "0.0.3"
bdlukaa marked this conversation as resolved.
Show resolved Hide resolved
matcher:
dependency: transitive
description:
Expand Down
4 changes: 3 additions & 1 deletion lib/macos_ui.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ library macos_ui;

bdlukaa marked this conversation as resolved.
Show resolved Hide resolved
/// todo: package-level docs

export 'package:flutter/widgets.dart' hide Icon, IconTheme, TextBox;
export 'package:flutter/widgets.dart';
export 'package:flutter/cupertino.dart' show CupertinoIcons;
export 'package:flutter/material.dart'
show
Brightness,
Expand All @@ -20,5 +21,6 @@ export 'src/util.dart';
export 'src/styles/theme.dart';
export 'src/styles/typography.dart';
export 'src/layout/scaffold.dart';
export 'src/buttons/checkbox.dart';
export 'src/buttons/switch.dart';
export 'src/indicators/progress_indicators.dart';
102 changes: 102 additions & 0 deletions lib/src/buttons/checkbox.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import 'package:macos_ui/macos_ui.dart';
bdlukaa marked this conversation as resolved.
Show resolved Hide resolved

/// A checkbox is a type of button that lets the user choose between
/// two opposite states, actions, or values. A selected checkbox is
/// considered on when it contains a checkmark and off when it's empty.
/// A checkbox is almost always followed by a title unless it appears in
/// a checklist.
class Checkbox extends StatelessWidget {
/// Creates a checkbox.
///
/// [size] must be non-negative
const Checkbox({
Key? key,
required this.value,
required this.onChanged,
this.size = 16.0,
this.activeColor,
this.disabledColor = CupertinoColors.quaternaryLabel,
this.offBorderColor = CupertinoColors.tertiaryLabel,
}) : assert(size >= 0),
super(key: key);

/// Whether the checkbox is checked or not. If null, it'll be considered
/// mixed.
final bool? value;

/// Called whenever the state of the checkbox changes. If null, the checkbox
/// will be considered disabled
final ValueChanged<bool>? onChanged;

/// The size of the checkbox. It must be non-negative.
final double size;

/// The background color when the checkbox is on or mixed. If null,
/// [Style.activeColor] is used
final Color? activeColor;

/// The background color when the checkbox is disabled. [CupertinoColors.quaternaryLabel]
/// is used by default
final Color disabledColor;

/// The color of the border when the checkbox is off. [CupertinoColors.tertiaryLabel]
/// is used by default
final Color offBorderColor;

/// Whether the checkbox is mixed or not.
bool get isMixed => value == null;

/// Whether the checkbox is disabled or not.
bool get isDisabled => onChanged == null;

@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
if (value == null || value == false) {
onChanged?.call(true);
} else {
onChanged?.call(false);
}
},
child: Container(
height: size,
width: size,
alignment: Alignment.center,
decoration: isDisabled || value == null || value == true
? BoxDecoration(
color: CupertinoDynamicColor.resolve(
isDisabled
? disabledColor
: activeColor ??
context.maybeStyle?.accentColor ??
bdlukaa marked this conversation as resolved.
Show resolved Hide resolved
CupertinoColors.activeBlue,
context,
),
borderRadius: BorderRadius.circular(4.0),
)
: BoxDecoration(
border: Border.all(
width: 0.5,
color: CupertinoDynamicColor.resolve(
offBorderColor,
context,
),
),
borderRadius: BorderRadius.circular(4.0),
),
child: Icon(
isDisabled
? null
: isMixed
? CupertinoIcons.minus
: value == false
? null
: CupertinoIcons.check_mark,
color: CupertinoColors.white,
size: (size - 3).clamp(0, size),
),
),
);
}
}