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

add controller to group button #50

Merged
merged 7 commits into from
Dec 26, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 17 additions & 0 deletions example/lib/examples/styles_example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import 'package:flutter/material.dart';
import 'package:group_button/group_button.dart';

class StylesExample extends StatelessWidget {
final GroupButtonController controller = GroupButtonController();
final GroupButtonController controller2 = GroupButtonController();
@override
Widget build(BuildContext context) {
return MaterialApp(
Expand All @@ -27,6 +29,7 @@ class StylesExample extends StatelessWidget {
ScrollIjector(
groupingType: GroupingType.wrap,
child: GroupButton(
controller: controller,
spacing: 10,
buttons: const [
'12:00',
Expand All @@ -37,6 +40,7 @@ class StylesExample extends StatelessWidget {
'17:00',
'18:00',
],
selectedButton: 4,
selectedShadow: const [],
unselectedShadow: const [],
unselectedColor: Colors.grey[300],
Expand All @@ -47,9 +51,16 @@ class StylesExample extends StatelessWidget {
onSelected: (i, selected) {},
),
),
TextButton(
onPressed: () {
controller.setSelectedIndex(6);
},
child: const Text('Select Button'),
),
ScrollIjector(
groupingType: GroupingType.wrap,
child: GroupButton(
controller: controller2,
spacing: 10,
buttons: const [
'12:00',
Expand All @@ -72,6 +83,12 @@ class StylesExample extends StatelessWidget {
onSelected: (i, selected) {},
),
),
TextButton(
onPressed: () {
controller2.setSelectedIndexes([1, 2, 5]);
},
child: const Text('Select Buttons'),
),
ScrollIjector(
groupingType: GroupingType.wrap,
child: GroupButton(
Expand Down
66 changes: 49 additions & 17 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,59 @@ void main() {
}

class CommonExample extends StatelessWidget {
final controller = GroupButtonController();

@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: GroupButton(
spacing: 10,
buttons: const [
'12:00',
'13:00',
'14:00',
'15:00',
'16:00',
'17:00',
'18:00',
'19:00',
'20:00'
],
borderRadius: BorderRadius.circular(30),
onSelected: (i, selected) => debugPrint('Button #$i selected'),
home: Builder(
builder: (context) => Scaffold(
body: SafeArea(
child: Column(
children: [
Row(
children: [
TextButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => StylesExample(),
),
);
},
child: const Text('Styles Example'),
),
],
),
Center(
child: GroupButton(
controller: controller,
spacing: 10,
buttons: const [
'12:00',
'13:00',
'14:00',
'15:00',
'16:00',
'17:00',
'18:00',
'19:00',
'20:00'
],
borderRadius: BorderRadius.circular(30),
onSelected: (i, selected) =>
debugPrint('Button #$i selected'),
),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
controller.setSelectedIndex(1);
},
),
),
),
Expand Down
18 changes: 13 additions & 5 deletions lib/group_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ library group_button;

import 'package:flutter/material.dart';

import 'src/group_button_body.dart';
import 'src/utils/utils.dart';
import 'package:group_button/src/group_button_body.dart';
import 'package:group_button/src/utils/utils.dart';

export 'src/utils/utils.dart';

Expand All @@ -12,6 +12,7 @@ class GroupButton extends StatelessWidget {
Key? key,
required this.buttons,
required this.onSelected,
this.controller,
this.disabledButtons,
this.selectedButtons,
this.isRadio = true,
Expand Down Expand Up @@ -39,11 +40,15 @@ class GroupButton extends StatelessWidget {
this.alignment,
this.elevation,
}) : assert(
(isRadio && selectedButtons == null) || (!isRadio && selectedButton == null),
(isRadio && selectedButtons == null) ||
(!isRadio && selectedButton == null),
"You can use selectedButton field for isRadio [true] and selectedButtons field with isRadio [false]",
),
super(key: key);

/// Group Button Controller
final GroupButtonController? controller;

/// [EdgeInsets] The inner padding of buttons [GroupButton]
final EdgeInsets textPadding;

Expand Down Expand Up @@ -141,6 +146,7 @@ class GroupButton extends StatelessWidget {
Widget build(BuildContext context) {
return GroupButtonBody(
buttons: buttons,
controller: controller,
disabledButtons: disabledButtons,
selectedButtons: selectedButtons,
selectedButton: selectedButton,
Expand Down Expand Up @@ -183,6 +189,8 @@ class GroupButton extends StatelessWidget {
)
];

static const _kDefaultSelectedTextStyle = TextStyle(fontSize: 14, color: Colors.white);
static const _kDefaultUnselectedTextStyle = TextStyle(fontSize: 14, color: Colors.black);
static const _kDefaultSelectedTextStyle =
TextStyle(fontSize: 14, color: Colors.white);
static const _kDefaultUnselectedTextStyle =
TextStyle(fontSize: 14, color: Colors.black);
}
47 changes: 44 additions & 3 deletions lib/src/group_button_body.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import 'package:flutter/material.dart';
import 'package:group_button/src/group_custom_button.dart';
import 'package:group_button/src/utils/utils.dart';

import 'group_custom_button.dart';

class GroupButtonBody extends StatefulWidget {
const GroupButtonBody({
Key? key,
required this.buttons,
required this.onSelected,
this.controller,
this.selectedBorderColor,
this.unselectedBorderColor,
required this.groupingType,
Expand Down Expand Up @@ -36,6 +36,7 @@ class GroupButtonBody extends StatefulWidget {
this.elevation,
}) : super(key: key);

final GroupButtonController? controller;
final List<String> buttons;
final List<int>? disabledButtons;
final List<int>? selectedButtons;
Expand Down Expand Up @@ -85,6 +86,34 @@ class _GroupButtonBodyState extends State<GroupButtonBody> {
_selectedIndexes[e] = true;
}
});
setState(() {});
if (widget.controller != null) {
widget.controller?.setSelectedIndexes(widget.selectedButtons!);
}
}
if (widget.selectedButton != null) {
if (widget.controller != null) {
widget.controller?.setSelectedIndex(widget.selectedButton!);
}
setState(() => _selectedIndex = widget.selectedButton);
}
if (widget.controller != null) {
widget.controller!.addListener(() {
if (widget.controller!.selectedIndex != _selectedIndex) {
setState(() {
_selectedIndex = widget.controller!.selectedIndex;
});
}
final Map<int, bool> cacheMap = {};
// ignore: avoid_function_literals_in_foreach_calls
widget.controller!.selectedIndexes.forEach((i) {
cacheMap[i] = true;
});
_selectedIndexes.clear();
setState(() {
_selectedIndexes.addAll(cacheMap);
});
});
}
if (widget.selectedButton != null) {
if (!(widget.disabledButtons?.contains(widget.selectedButton) ?? false)) {
Expand Down Expand Up @@ -128,7 +157,9 @@ class _GroupButtonBodyState extends State<GroupButtonBody> {
}

bool _getCond(int i) {
return widget.isRadio ? i == _selectedIndex : _selectedIndexes.containsKey(i) && _selectedIndexes[i] == true;
return widget.isRadio
? i == _selectedIndex
: _selectedIndexes.containsKey(i) && _selectedIndexes[i] == true;
}

List<Widget> _buildButtonsList(
Expand Down Expand Up @@ -186,7 +217,17 @@ class _GroupButtonBodyState extends State<GroupButtonBody> {
void _selectButton(int i) {
if (widget.isRadio) {
setState(() => _selectedIndex = i);
if (widget.controller != null) {
widget.controller?.setSelectedIndex(i);
}
} else {
if (widget.controller != null) {
if (widget.controller!.selectedIndexes.contains(i)) {
widget.controller!.selectedIndexes.remove(i);
} else {
widget.controller!.selectedIndexes.add(i);
}
}
if (_selectedIndexes.containsKey(i)) {
setState(() => _selectedIndexes[i] = !_selectedIndexes[i]!);
} else {
Expand Down
19 changes: 19 additions & 0 deletions lib/src/utils/controller.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'package:flutter/material.dart';

class GroupButtonController extends ChangeNotifier {
int _selectedIndex = 0;
List<int> _selectedIndexes = [0];

int get selectedIndex => _selectedIndex;
List<int> get selectedIndexes => _selectedIndexes;

void setSelectedIndex(int i) {
_selectedIndex = i;
notifyListeners();
}

void setSelectedIndexes(List<int> i) {
_selectedIndexes = i;
notifyListeners();
}
}
1 change: 1 addition & 0 deletions lib/src/utils/utils.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export 'alignments.dart';
export 'controller.dart';
export 'extensions/extensions.dart';
export 'grouping_type.dart';