Skip to content

Commit

Permalink
Merge pull request #59 from gnassro/master
Browse files Browse the repository at this point in the history
Adding event on disabled items
  • Loading branch information
Frezyx authored Jan 6, 2022
2 parents 7eb77de + 31439d7 commit 619a8b3
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class CustomizableExample extends StatelessWidget {
.map((i) => '${i + 1}')
.toList(),
onSelected: (i, selected) => debugPrint('Button #$i $selected'),
onDisablePressed: (i) => debugPrint('Disable Button #$i pressed'),
),
),
bottomNavigationBar: GroupButtonBottomPanel(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Widget _buildCheckboxExample(GroupingType groupingType, {Axis direction}) {
onSelected: (index, isSelected) => debugPrint(
'$index button is ${isSelected ? 'selected' : 'unselected'}',
),
onDisablePressed: (i) => debugPrint('Disable Button #$i pressed'),
buttons: const [
"Burger",
"Sandwiches",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Widget _buildCustomExample(GroupingType groupingType, {Axis direction}) {
onSelected: (index, isSelected) => debugPrint(
'$index button is ${isSelected ? 'selected' : 'unselected'}',
),
onDisablePressed: (i) => debugPrint('Disable Button #$i pressed'),
buttons: const [
"Dart",
"Kotlin",
Expand Down
2 changes: 2 additions & 0 deletions example/lib/examples/extended_example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class _ExampleState extends State<_Example> {
onSelected: (i, selected) {
_extendedExampleController.selectedGroupingType = i;
},
onDisablePressed: (i) => debugPrint('Disable Button #$i pressed'),
),
GroupButton(
spacing: 7.5,
Expand All @@ -88,6 +89,7 @@ class _ExampleState extends State<_Example> {
curve: Curves.easeIn,
);
},
onDisablePressed: (i) => debugPrint('Disable Button #$i pressed'),
),
],
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Widget _buildRadioExample(GroupingType groupingType, {Axis direction}) {
groupingType: groupingType,
onSelected: (index, isSelected) =>
debugPrint('$index button is selected'),
onDisablePressed: (i) => debugPrint('Disable Button #$i pressed'),
buttons: const [
"12:00",
"13:00",
Expand Down
1 change: 1 addition & 0 deletions example/lib/examples/provider_example/ui/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class _HomePageState extends State<HomePage>
);
_eProvider.updateAnswer(index, value: isSelected);
},
onDisablePressed: (i) => debugPrint('Disable Button #$i pressed'),
selectedColor: Colors.blue,
buttons: q.answerTitles,
selectedButtons: q.selectedIndexes,
Expand Down
4 changes: 4 additions & 0 deletions example/lib/examples/styles_example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class StylesExample extends StatelessWidget {
),
borderRadius: BorderRadius.circular(30),
onSelected: (i, selected) {},
onDisablePressed: (i) {},
),
),
ScrollIjector(
Expand All @@ -68,6 +69,7 @@ class StylesExample extends StatelessWidget {
),
borderRadius: BorderRadius.circular(4),
onSelected: (i, selected) {},
onDisablePressed: (i) {},
),
),
ScrollIjector(
Expand Down Expand Up @@ -99,6 +101,7 @@ class StylesExample extends StatelessWidget {
),
borderRadius: BorderRadius.circular(8),
onSelected: (i, selected) {},
onDisablePressed: (i) {},
),
),
Padding(
Expand Down Expand Up @@ -136,6 +139,7 @@ class StylesExample extends StatelessWidget {
),
selectedColor: Colors.pink,
onSelected: (i, selected) {},
onDisablePressed: (i) {},
),
),
),
Expand Down
19 changes: 15 additions & 4 deletions lib/src/group_button_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class GroupButton extends StatelessWidget {
Key? key,
required this.buttons,
required this.onSelected,
this.disabledButtons,
this.onDisablePressed,
this.disabledButtons = const [],
this.selectedButtons,
this.isRadio = true,
this.groupingType = GroupingType.wrap,
Expand Down Expand Up @@ -49,11 +50,12 @@ class GroupButton extends StatelessWidget {
factory GroupButton.radio({
required List<String> buttons,
required Function(int index) onSelected,
Function(int index)? onDisablePressed,
EdgeInsets textPadding = EdgeInsets.zero,
TextAlign textAlign = TextAlign.left,
AlignmentGeometry? alignment,
double? elevation,
List<int>? disabledButtons,
List<int> disabledButtons = const [],
int? selectedButton,
Axis? direction,
double spacing = 10,
Expand All @@ -80,6 +82,7 @@ class GroupButton extends StatelessWidget {
disabledButtons: disabledButtons,
selectedButton: selectedButton,
onSelected: (index, _) => onSelected(index),
onDisablePressed: onDisablePressed,
direction: direction,
spacing: spacing,
runSpacing: runSpacing,
Expand Down Expand Up @@ -113,11 +116,12 @@ class GroupButton extends StatelessWidget {
factory GroupButton.checkbox({
required List<String> buttons,
required Function(int index, bool selected) onSelected,
Function(int index)? onDisablePressed,
EdgeInsets textPadding = EdgeInsets.zero,
TextAlign textAlign = TextAlign.left,
AlignmentGeometry? alignment,
double? elevation,
List<int>? disabledButtons,
List<int> disabledButtons = const [],
List<int>? selectedButtons,
Axis? direction,
double spacing = 10,
Expand Down Expand Up @@ -145,6 +149,7 @@ class GroupButton extends StatelessWidget {
disabledButtons: disabledButtons,
selectedButtons: selectedButtons,
onSelected: onSelected,
onDisablePressed: onDisablePressed,
direction: direction,
spacing: spacing,
runSpacing: runSpacing,
Expand Down Expand Up @@ -186,7 +191,7 @@ class GroupButton extends StatelessWidget {
final List<String> buttons;

/// [int] button ids that are disabled.
final List<int>? disabledButtons;
final List<int> disabledButtons;

/// [int] button ids that is selected initially.
/// /// Using when [isRadio] is false
Expand All @@ -201,6 +206,11 @@ class GroupButton extends StatelessWidget {
/// Return int [index] of selected button and [isSelected] if [isRadio] = false
final Function(int index, bool isSelected) onSelected;

/// Callback [Function] works by clicking on a disabled group element
///
/// Return int [index] of selected button
final Function(int index)? onDisablePressed;

/// bool variable for switching between modes [ChackBox] and [Radio]
///
/// if the [isRadio] = true, only one button can be selected
Expand Down Expand Up @@ -273,6 +283,7 @@ class GroupButton extends StatelessWidget {
selectedButtons: selectedButtons,
selectedButton: selectedButton,
onSelected: onSelected,
onDisablePressed: onDisablePressed,
isRadio: isRadio,
direction: direction,
spacing: spacing,
Expand Down
11 changes: 7 additions & 4 deletions lib/src/group_button_body.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ class GroupButtonBody extends StatefulWidget {
required this.onSelected,
required this.controller,
required this.groupingType,
this.onDisablePressed,
this.selectedBorderColor,
this.unselectedBorderColor,
this.disabledButtons,
this.disabledButtons = const [],
this.selectedButtons,
this.selectedButton,
this.isRadio = false,
Expand All @@ -37,10 +38,11 @@ class GroupButtonBody extends StatefulWidget {
}) : super(key: key);

final List<String> buttons;
final List<int>? disabledButtons;
final List<int> disabledButtons;
final List<int>? selectedButtons;
final int? selectedButton;
final Function(int, bool) onSelected;
final Function(int)? onDisablePressed;
final bool isRadio;
final Axis? direction;
final double spacing;
Expand Down Expand Up @@ -138,13 +140,14 @@ class _GroupButtonBodyState extends State<GroupButtonBody> {
for (var i = 0; i < buttons.length; i++) {
Widget rebuidedButton = GroupCustomButton(
text: buttons[i],
onPressed: (widget.disabledButtons?.contains(i) ?? false)
? null
onPressed: widget.disabledButtons.contains(i)
? () => widget.onDisablePressed?.call(i)
: () {
_selectButton(i);
widget.onSelected(i, _getCond(i));
},
isSelected: _getCond(i),
isDisable: widget.disabledButtons.contains(i),
selectedTextStyle: widget.selectedTextStyle,
unselectedTextStyle: widget.unselectedTextStyle,
selectedColor: widget.selectedColor,
Expand Down
47 changes: 29 additions & 18 deletions lib/src/group_custom_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class GroupCustomButton extends StatelessWidget {
required this.onPressed,
required this.text,
required this.isSelected,
required this.isDisable,
this.selectedBorderColor,
this.unselectedBorderColor,
this.selectedTextStyle,
Expand All @@ -26,6 +27,7 @@ class GroupCustomButton extends StatelessWidget {
final String text;
final void Function()? onPressed;
final bool isSelected;
final bool isDisable;
final TextStyle? selectedTextStyle;
final TextStyle? unselectedTextStyle;
final Color? selectedColor;
Expand All @@ -42,7 +44,7 @@ class GroupCustomButton extends StatelessWidget {
final AlignmentGeometry? alignment;
final double? elevation;

bool get isEnabled => onPressed != null;
bool get isEnabled => !isDisable;

List<BoxShadow>? get _boxShadow => isSelected
? selectedShadow
Expand All @@ -64,30 +66,39 @@ class GroupCustomButton extends StatelessWidget {

@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return Container(
height: height,
width: width,
decoration: BoxDecoration(
borderRadius: borderRadius,
boxShadow: _boxShadow,
),
child: ElevatedButton(
onPressed: onPressed,
style: ElevatedButton.styleFrom(
elevation: elevation ?? 0.0,
primary: _getBackgroundColor(theme),
shape: _buildShape(),
padding: (width != null || height != null) ? EdgeInsets.zero : null,
alignment: (width != null || height != null) ? alignment : null,
),
child: Padding(
padding: textPadding,
child: Text(
text,
textAlign: textAlign,
style: _textStyle,
),
child: !isDisable
? _customButton(context, onPressed)
: GestureDetector(
onTap: onPressed,
child: _customButton(context, null),
),
);
}

Widget _customButton(BuildContext context, void Function()? onPressed) {
final theme = Theme.of(context);
return ElevatedButton(
onPressed: onPressed,
style: ElevatedButton.styleFrom(
elevation: elevation ?? 0.0,
primary: _getBackgroundColor(theme),
shape: _buildShape(),
padding: (width != null || height != null) ? EdgeInsets.zero : null,
alignment: (width != null || height != null) ? alignment : null,
),
child: Padding(
padding: textPadding,
child: Text(
text,
textAlign: textAlign,
style: _textStyle,
),
),
);
Expand Down
23 changes: 23 additions & 0 deletions test/group_button_disabled_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'test_config.dart';

void main() {
group('Disabled button tap tests', () {
testWidgets(
'Tap disabled group button test',
(WidgetTester tester) async {
await tester.pumpWidget(
const MaterialApp(home: GroupButtonTestWidget()),
);
final button = find.text('14:00');

await tester.tap(button);
await tester.pump();

final text = find.text('14:00');
expect(text, findsOneWidget);
},
);
});
}
3 changes: 2 additions & 1 deletion test/test_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ class GroupButtonTestWidget extends StatefulWidget {
}

class _GroupButtonTestWidgetState extends State<GroupButtonTestWidget> {
List<String> buttons = ['12:00', '13:00'];
List<String> buttons = ['12:00', '13:00', '14:00'];

@override
Widget build(BuildContext context) {
return Scaffold(
body: GroupButton(
buttons: buttons,
disabledButtons: const [2],
onSelected: (index, _) {
setState(() => buttons.removeAt(index));
},
Expand Down

0 comments on commit 619a8b3

Please sign in to comment.