Skip to content

Commit

Permalink
Merge pull request #3 from akshathjain/dev
Browse files Browse the repository at this point in the history
Added the ability to disable elements
  • Loading branch information
akshathjain authored Apr 8, 2019
2 parents 7c13d63 + cb56b34 commit a81e24f
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 50 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ Updated broken images in documentation and changed homepage to the GitHub repo.

## [1.0.3] - March 22, 2019

- Updated the project file structure (importing the package is now easier)
- Updated the project file structure (importing the package is now easier)


## [1.0.4] - April 8, 2019

- Added the option to disable elements of the `CheckboxGroup` and `RadioButtonGroup` using the `disabled` property
7 changes: 3 additions & 4 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ modification, are permitted provided that the following conditions are met:
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* All advertising materials mentioning features or use of this software must
display the following acknowledgement: This product includes software
developed by Akshath Jain (https://akshathjain.com).
* All use of this software must display the following acknowledgement: This
product includes software developed by Akshath Jain (https://akshathjain.com).

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Expand All @@ -24,4 +23,4 @@ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ A set of Flutter widgets that makes grouping Checkboxes and Radio Buttons much e
Add the following to your `pubspec.yaml` file:

dependencies:
grouped_buttons: ^1.0.3
grouped_buttons: ^1.0.4

## Simple Usage
#### Creating a basic `CheckboxGroup`

Expand All @@ -26,7 +26,7 @@ Add the following to your `pubspec.yaml` file:
],
onSelected: (List<String> checked) => print(checked.toString())
);

#### Creating a basic `RadioButtonGroup`

RadioButtonGroup(
Expand All @@ -49,6 +49,7 @@ There are several options that allow for more control.
|`activeColor` |The color to use when a Checkbox is checked. |
|`checkColor` |The color to use for the check icon when a Checkbox is checked. |
|`checked` |Specifies which boxes to be automatically check. Every element must match a label. This is useful for clearing all selections (set it to []). If this is non-null, then the user must handle updating this list; otherwise, the state of the CheckboxGroup won't change. |
| `disabled` |Specifies which boxes should be disabled. If this is non-null, no boxes will be disabled. The strings passed to this must match the labels. |
|`itemBuilder` |Called when needed to build a CheckboxGroup element. |
|`labels` |(required) A list of strings that describes each Checkbox. Each label must be distinct. |
|`labelStyle` |The style to use for the labels. |
Expand Down Expand Up @@ -87,6 +88,7 @@ There are several options that allow for more control.
| Properties | Description |
|---------------|--------------|
|`activeColor` |The color to use when a Radio button is checked. |
| `disabled` |Specifies which buttons should be disabled. If this is non-null, no buttons will be disabled. The strings passed to this must match the labels. |
|`itemBuilder` |Called when needed to build a RadioButtonGroup element. |
|`labels` |(required) A list of strings that describes each Radio button. Each label must be distinct. |
|`labelStyle` |The style to use for the labels. |
Expand Down
33 changes: 20 additions & 13 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class HomePage extends StatefulWidget {
_HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
class _HomePageState extends State<HomePage> {

List<String> _checked = ["A", "B"];
String _picked = "Two";
Expand All @@ -41,7 +41,7 @@ class _HomePageState extends State<HomePage> {
),
body: _body(),
);
//
//
}

Widget _body(){
Expand All @@ -55,9 +55,9 @@ class _HomePageState extends State<HomePage> {
//BASIC CHECKBOXGROUP
Container(
padding: const EdgeInsets.only(left: 14.0, top: 14.0),
child: Text("Basic CheckboxGroup",
child: Text("Basic CheckboxGroup",
style: TextStyle(
fontWeight: FontWeight.bold,
fontWeight: FontWeight.bold,
fontSize: 20.0
),
),
Expand All @@ -73,6 +73,10 @@ class _HomePageState extends State<HomePage> {
"Friday",
"Saturday",
],
disabled: [
"Wednesday",
"Friday"
],
onChange: (bool isChecked, String label, int index) => print("isChecked: $isChecked label: $label index: $index"),
onSelected: (List<String> checked) => print("checked: ${checked.toString()}"),
),
Expand All @@ -82,19 +86,22 @@ class _HomePageState extends State<HomePage> {
//BASIC RADIOBUTTONGROUP
Container(
padding: const EdgeInsets.only(left: 14.0, top: 14.0),
child: Text("Basic RadioButtonGroup",
child: Text("Basic RadioButtonGroup",
style: TextStyle(
fontWeight: FontWeight.bold,
fontWeight: FontWeight.bold,
fontSize: 20.0
),
),
),

RadioButtonGroup(
labels: [
"Option 1",
"Option 2",
],
disabled: [
"Option 1"
],
onChange: (String label, int index) => print("label: $label index: $index"),
onSelected: (String label) => print(label),
),
Expand All @@ -109,9 +116,9 @@ class _HomePageState extends State<HomePage> {
///CUSTOM CHECKBOX GROUP
Container(
padding: const EdgeInsets.only(left: 14.0, top: 14.0, bottom: 14.0),
child: Text("Custom CheckboxGroup",
child: Text("Custom CheckboxGroup",
style: TextStyle(
fontWeight: FontWeight.bold,
fontWeight: FontWeight.bold,
fontSize: 20.0
),
),
Expand All @@ -138,15 +145,15 @@ class _HomePageState extends State<HomePage> {
);
},
),



///CUSTOM RADIOBUTTON GROUP
Container(
padding: const EdgeInsets.only(left: 14.0, top: 14.0, bottom: 14.0),
child: Text("Custom RadioButtonGroup",
child: Text("Custom RadioButtonGroup",
style: TextStyle(
fontWeight: FontWeight.bold,
fontWeight: FontWeight.bold,
fontSize: 20.0
),
),
Expand All @@ -173,7 +180,7 @@ class _HomePageState extends State<HomePage> {
);
},
),

]
);
}
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ packages:
path: ".."
relative: true
source: path
version: "1.0.3"
version: "1.0.4"
matcher:
dependency: transitive
description:
Expand Down
40 changes: 26 additions & 14 deletions lib/src/checkbox_group.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,20 @@ class CheckboxGroup extends StatefulWidget {
/// A list of strings that describes each Checkbox. Each label must be distinct.
final List<String> labels;

/// Specifies which boxes to be automatically check.
/// Specifies which boxes to be automatically check.
/// Every element must match a label.
/// This is useful for clearing all selections (set it to []).
/// This is useful for clearing all selections (set it to []).
/// If this is non-null, then the user must handle updating this list; otherwise, the state of the CheckboxGroup won't change.
final List<String> checked;

/// Specifies which boxes should be disabled.
/// If this is non-null, no boxes will be disabled.
/// The strings passed to this must match the labels.
final List<String> disabled;

/// Called when the value of the CheckboxGroup changes.
final void Function(bool isChecked, String label, int index) onChange;

/// Called when the user makes a selection.
final void Function(List<String> selected) onSelected;

Expand Down Expand Up @@ -58,6 +63,7 @@ class CheckboxGroup extends StatefulWidget {
Key key,
@required this.labels,
this.checked,
this.disabled,
this.onChange,
this.onSelected,
this.labelStyle = const TextStyle(),
Expand Down Expand Up @@ -85,8 +91,8 @@ class _CheckboxGroupState extends State<CheckboxGroup> {
super.initState();

//set the selected to the checked (if not null)
_selected = widget.checked ?? [];
_selected = widget.checked ?? [];

}

@override
Expand All @@ -105,24 +111,30 @@ class _CheckboxGroupState extends State<CheckboxGroup> {

Checkbox cb = Checkbox(
value: _selected.contains(widget.labels.elementAt(i)),
onChanged: (bool isChecked) => onChanged(isChecked, i),
onChanged: (widget.disabled != null && widget.disabled.contains(widget.labels.elementAt(i))) ? null :
(bool isChecked) => onChanged(isChecked, i),
checkColor: widget.checkColor,
activeColor: widget.activeColor ?? Theme.of(context).toggleableActiveColor,
tristate: widget.tristate,
);

Text t = Text(widget.labels.elementAt(i), style: widget.labelStyle);

Text t = Text(
widget.labels.elementAt(i),
style: (widget.disabled != null && widget.disabled.contains(widget.labels.elementAt(i))) ?
widget.labelStyle.apply(color: Theme.of(context).disabledColor) :
widget.labelStyle
);



//use user defined method to build
if(widget.itemBuilder != null)
content.add(widget.itemBuilder(cb, t, i));
else{ //otherwise, use predefined method of building

//vertical orientation means Column with Row inside
if(widget.orientation == GroupedButtonsOrientation.VERTICAL){

content.add(Row(children: <Widget>[
SizedBox(width: 12.0),
cb,
Expand All @@ -131,26 +143,26 @@ class _CheckboxGroupState extends State<CheckboxGroup> {
]));

}else{ //horizontal orientation means Row with Column inside

content.add(Column(children: <Widget>[
cb,
SizedBox(width: 12.0),
t,
]));

}

}
}

return Container(
padding: widget.padding,
margin: widget.margin,
child: widget.orientation == GroupedButtonsOrientation.VERTICAL ? Column(children: content) : Row(children: content),
);
}


void onChanged(bool isChecked, int i){
bool isAlreadyContained = _selected.contains(widget.labels.elementAt(i));

Expand Down
38 changes: 25 additions & 13 deletions lib/src/radio_button_group.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ class RadioButtonGroup extends StatefulWidget {
/// If this is non-null, then the user must handle updating this; otherwise, the state of the RadioButtonGroup won't change.
final String picked;

/// Specifies which buttons should be disabled.
/// If this is non-null, no buttons will be disabled.
/// The strings passed to this must match the labels.
final List<String> disabled;

/// Called when the value of the RadioButtonGroup changes.
final void Function(String label, int index) onChange;

Expand Down Expand Up @@ -49,6 +54,7 @@ class RadioButtonGroup extends StatefulWidget {
Key key,
@required this.labels,
this.picked,
this.disabled,
this.onChange,
this.onSelected,
this.labelStyle = const TextStyle(),
Expand Down Expand Up @@ -92,26 +98,32 @@ class _RadioButtonGroupState extends State<RadioButtonGroup> {
value: i,

//just changed the selected filter to current selection
//since these are radio buttons, and you can only pick
//since these are radio buttons, and you can only pick
//one at a time
onChanged: (var index) => setState((){
_selected = widget.labels.elementAt(i);

if(widget.onChange != null) widget.onChange(widget.labels.elementAt(i), i);
if(widget.onSelected != null) widget.onSelected(widget.labels.elementAt(i));
}),
onChanged: (widget.disabled != null && widget.disabled.contains(widget.labels.elementAt(i))) ? null :
(var index) => setState((){
_selected = widget.labels.elementAt(i);

if(widget.onChange != null) widget.onChange(widget.labels.elementAt(i), i);
if(widget.onSelected != null) widget.onSelected(widget.labels.elementAt(i));
}),
);

Text t = Text(widget.labels.elementAt(i), style: widget.labelStyle);
Text t = Text(
widget.labels.elementAt(i),
style: (widget.disabled != null && widget.disabled.contains(widget.labels.elementAt(i))) ?
widget.labelStyle.apply(color: Theme.of(context).disabledColor) :
widget.labelStyle
);

//use user defined method to build
if(widget.itemBuilder != null)
content.add(widget.itemBuilder(rb, t, i));
else{ //otherwise, use predefined method of building

//vertical orientation means Column with Row inside
if(widget.orientation == GroupedButtonsOrientation.VERTICAL){

content.add(Row(children: <Widget>[
SizedBox(width: 12.0),
rb,
Expand All @@ -120,16 +132,16 @@ class _RadioButtonGroupState extends State<RadioButtonGroup> {
]));

}else{ //horizontal orientation means Row with Column inside

content.add(Column(children: <Widget>[
rb,
SizedBox(width: 12.0),
t,
]));

}
}

}

return Container(
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: grouped_buttons
description: A simple package that makes grouping Checkboxes and Radio Buttons much easier.
version: 1.0.3
version: 1.0.4
author: Akshath Jain <[email protected]>
homepage: https://github.com/akshathjain/grouped_buttons

Expand Down

0 comments on commit a81e24f

Please sign in to comment.