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

Added configurable border width and disable flag for the shadow on tapping (helpful if you want a flat button and the elevation is 0) #107

Merged
merged 6 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
2 changes: 1 addition & 1 deletion example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ subprojects {
project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
tasks.register("clean", Delete) {
delete rootProject.buildDir
}
2 changes: 2 additions & 0 deletions lib/src/group_button_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,10 @@ class GroupButton<T> extends StatelessWidget {
selectedBorderColor: options.selectedBorderColor,
unselectedBorderColor: options.unselectedBorderColor,
borderRadius: options.borderRadius,
borderWidth: options.borderWidth,
selectedShadow: options.selectedShadow,
unselectedShadow: options.unselectedShadow,
tappingShadowColor: options.tappingShadowColor,
buttonWidth: options.buttonWidth,
buttonHeight: options.buttonHeight,
mainGroupAlignment: options.mainGroupAlignment,
Expand Down
24 changes: 11 additions & 13 deletions lib/src/group_button_body.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ class GroupButtonBody<T> extends StatefulWidget {
this.selectedColor,
this.unselectedColor,
this.borderRadius = BorderRadius.zero,
this.borderWidth,
this.selectedShadow = const [],
this.unselectedShadow = const [],
this.tappingShadowColor,
this.buttonWidth,
this.buttonHeight,
this.mainGroupAlignment = MainGroupAlignment.center,
Expand Down Expand Up @@ -59,8 +61,10 @@ class GroupButtonBody<T> extends StatefulWidget {
final Color? selectedBorderColor;
final Color? unselectedBorderColor;
final BorderRadius? borderRadius;
final double? borderWidth;
final List<BoxShadow> selectedShadow;
final List<BoxShadow> unselectedShadow;
final Color? tappingShadowColor;
final double? buttonWidth;
final double? buttonHeight;
final GroupingType? groupingType;
Expand Down Expand Up @@ -166,10 +170,8 @@ class _GroupButtonBodyState<T> extends State<GroupButtonBody<T>> {
);
} else {
button = GroupButtonItem(
text: widget.buttonIndexedTextBuilder
?.call(_isSelected(i), i, context) ??
widget.buttonTextBuilder
?.call(_isSelected(i), widget.buttons[i], context) ??
text: widget.buttonIndexedTextBuilder?.call(_isSelected(i), i, context) ??
widget.buttonTextBuilder?.call(_isSelected(i), widget.buttons[i], context) ??
widget.buttons[i].toString(),
onPressed: _controller.disabledIndexes.contains(i)
? () => _controller.onDisablePressed?.call(i)
Expand All @@ -186,8 +188,10 @@ class _GroupButtonBodyState<T> extends State<GroupButtonBody<T>> {
selectedBorderColor: widget.selectedBorderColor,
unselectedBorderColor: widget.unselectedBorderColor,
borderRadius: widget.borderRadius,
borderWidth: widget.borderWidth,
selectedShadow: widget.selectedShadow,
unselectedShadow: widget.unselectedShadow,
tappingShadowColor: widget.tappingShadowColor,
height: widget.buttonHeight,
width: widget.buttonWidth,
textAlign: widget.textAlign,
Expand All @@ -199,9 +203,7 @@ class _GroupButtonBodyState<T> extends State<GroupButtonBody<T>> {

/// Padding adding
/// when groupingType is row or column
if (widget.spacing > 0.0 &&
widget.buttonIndexedBuilder == null &&
widget.buttonBuilder == null) {
if (widget.spacing > 0.0 && widget.buttonIndexedBuilder == null && widget.buttonBuilder == null) {
if (widget.groupingType == GroupingType.row) {
button = Padding(
padding: EdgeInsets.symmetric(horizontal: widget.spacing),
Expand Down Expand Up @@ -230,18 +232,14 @@ class _GroupButtonBodyState<T> extends State<GroupButtonBody<T>> {
} else {
final maxSelected = widget.maxSelected;
final selectedIndexesCount = _controller.selectedIndexes.length;
if (maxSelected != null &&
selectedIndexesCount >= maxSelected &&
!_controller.selectedIndexes.contains(i)) {
if (maxSelected != null && selectedIndexesCount >= maxSelected && !_controller.selectedIndexes.contains(i)) {
return;
}
_controller.toggleIndexes([i]);
}
}

bool _isSelected(int i) {
return widget.isRadio
? _controller.selectedIndex == i
: _controller.selectedIndexes.contains(i);
return widget.isRadio ? _controller.selectedIndex == i : _controller.selectedIndexes.contains(i);
}
}
9 changes: 8 additions & 1 deletion lib/src/group_button_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ class GroupButtonItem extends StatelessWidget {
this.selectedColor,
this.unselectedColor,
this.borderRadius,
this.borderWidth,
this.selectedShadow,
this.unselectedShadow,
this.tappingShadowColor,
this.height,
this.width,
this.textAlign,
Expand All @@ -35,8 +37,10 @@ class GroupButtonItem extends StatelessWidget {
final Color? selectedBorderColor;
final Color? unselectedBorderColor;
final BorderRadius? borderRadius;
final double? borderWidth;
final List<BoxShadow>? selectedShadow;
final List<BoxShadow>? unselectedShadow;
final Color? tappingShadowColor;
final double? height;
final double? width;
final TextAlign? textAlign;
Expand Down Expand Up @@ -64,6 +68,8 @@ class GroupButtonItem extends StatelessWidget {
? unselectedBorderColor
: null;

double get _borderWidth => borderWidth ?? 1.0;

@override
Widget build(BuildContext context) {
return Container(
Expand All @@ -87,6 +93,7 @@ class GroupButtonItem extends StatelessWidget {
return ElevatedButton(
onPressed: onPressed,
style: ElevatedButton.styleFrom(
shadowColor: tappingShadowColor ?? _getBackgroundColor(theme),
elevation: elevation ?? 0.0,
backgroundColor: _getBackgroundColor(theme),
shape: _buildShape(),
Expand Down Expand Up @@ -136,7 +143,7 @@ class GroupButtonItem extends StatelessWidget {

BorderSide buildBorderSide(Color? color) {
if (color != null) {
return BorderSide(color: color);
return BorderSide(color: color, width: _borderWidth);
}
return BorderSide.none;
}
Expand Down
3 changes: 3 additions & 0 deletions lib/src/options/defaults.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ const defaultUnselectedTextStyle = TextStyle(
fontSize: 14,
color: Colors.black,
);

@protected
const defaultTappingShadowColor = Color.fromARGB(18, 18, 18, 20);
9 changes: 8 additions & 1 deletion lib/src/options/group_button_options.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:flutter/material.dart';

import 'package:group_button/group_button.dart';
import 'package:group_button/src/options/defaults.dart';

Expand All @@ -17,8 +16,10 @@ class GroupButtonOptions {
this.selectedBorderColor,
this.unselectedBorderColor,
this.borderRadius,
this.borderWidth,
this.selectedShadow = defaultShadow,
this.unselectedShadow = defaultShadow,
this.tappingShadowColor = defaultTappingShadowColor,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tappingShadowColor must be null by default

this.buttonHeight,
this.buttonWidth,
this.mainGroupAlignment = MainGroupAlignment.center,
Expand Down Expand Up @@ -74,12 +75,18 @@ class GroupButtonOptions {
/// How much the button will be rounded
final BorderRadius? borderRadius;

/// The width of the button's border
final double? borderWidth;

/// list of selected button(s) [BoxShadow]
final List<BoxShadow> selectedShadow;

/// list of unselected buttons [BoxShadow]
final List<BoxShadow> unselectedShadow;

/// shadow [Color] shown when performing an onTap()
final Color? tappingShadowColor;

/// Height of Group button
final double? buttonHeight;

Expand Down