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

Split buttons #882

Merged
merged 7 commits into from
Jul 11, 2023
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [next]

- **BREAKING** Removed `SplitButtonBar` and its related widget. Use `SplitButton` or `SplitButton.toggle` instead ([#882](https://github.com/bdlukaa/fluent_ui/pull/882), [#411](https://github.com/bdlukaa/fluent_ui/issues/411))

## 4.7.0

- Add Slovak localization ([#850](https://github.com/bdlukaa/fluent_ui/issues/850))
Expand Down
268 changes: 119 additions & 149 deletions example/lib/screens/inputs/button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import 'package:url_launcher/link.dart';

import '../../widgets/card_highlight.dart';

const kSplitButtonHeight = 32.0;
const kSplitButtonWidth = 36.0;
const _kSplitButtonHeight = 32.0;
const _kSplitButtonWidth = 36.0;

class ButtonPage extends StatefulWidget {
const ButtonPage({super.key});
Expand All @@ -24,20 +24,43 @@ class _ButtonPageState extends State<ButtonPage> with PageMixin {
bool toggleDisabled = false;
bool toggleState = false;
bool splitButtonDisabled = false;
bool splitButtonState = false;
bool radioButtonDisabled = false;
int radioButtonSelected = -1;

AccentColor splitButtonColor = Colors.red;
final splitButtonFlyout = FlyoutController();

@override
void dispose() {
splitButtonFlyout.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
final theme = FluentTheme.of(context);

final splitButtonFlyout = FlyoutContent(
constraints: BoxConstraints(maxWidth: 200.0),
child: Wrap(
runSpacing: 10.0,
spacing: 8.0,
children: Colors.accentColors.map((color) {
return IconButton(
autofocus: splitButtonColor == color,
style: ButtonStyle(
padding: ButtonState.all(
EdgeInsets.all(4.0),
),
),
onPressed: () {
setState(() => splitButtonColor = color);
Navigator.of(context).pop(color);
},
icon: Container(
height: _kSplitButtonHeight,
width: _kSplitButtonHeight,
color: color,
),
);
}).toList(),
),
);

return ScaffoldPage.scrollable(
header: const PageHeader(title: Text('Button')),
children: [
Expand Down Expand Up @@ -276,80 +299,51 @@ ToggleButton(
),
CardHighlight(
child: Row(children: [
SizedBox(
height: 40.0,
child: SplitButtonBar(buttons: [
Button(
style: ButtonStyle(padding: ButtonState.all(EdgeInsets.zero)),
child: Container(
decoration: BoxDecoration(
color: splitButtonDisabled
? splitButtonColor.secondaryBrushFor(
FluentTheme.of(context).brightness,
)
: splitButtonColor,
borderRadius: const BorderRadiusDirectional.horizontal(
start: Radius.circular(4.0),
),
),
height: kSplitButtonHeight,
width: kSplitButtonWidth,
),
onPressed: splitButtonDisabled ? null : () {},
Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
Padding(
padding: const EdgeInsets.only(bottom: 4.0),
child: Text(
'SplitButton with custom content',
style: theme.typography.caption,
),
FlyoutTarget(
controller: splitButtonFlyout,
child: IconButton(
icon: const SizedBox(
height: kSplitButtonHeight - 13.0,
width: kSplitButtonWidth - 13.0,
child: Icon(FluentIcons.chevron_down, size: 8.0),
),
SplitButton(
enabled: !splitButtonDisabled,
child: Container(
decoration: BoxDecoration(
color: splitButtonDisabled
? splitButtonColor.secondaryBrushFor(theme.brightness)
: splitButtonColor,
borderRadius: const BorderRadiusDirectional.horizontal(
start: Radius.circular(4.0),
),
onPressed: splitButtonDisabled
? null
: () async {
final color =
await splitButtonFlyout.showFlyout<AccentColor>(
autoModeConfiguration: FlyoutAutoConfiguration(
preferredMode: FlyoutPlacementMode.bottomCenter,
),
builder: (context) {
return FlyoutContent(
constraints: BoxConstraints(maxWidth: 200.0),
child: Wrap(
runSpacing: 10.0,
spacing: 8.0,
children: Colors.accentColors.map((color) {
return Button(
autofocus: splitButtonColor == color,
style: ButtonStyle(
padding: ButtonState.all(
EdgeInsets.all(4.0),
),
),
onPressed: () {
Navigator.of(context).pop(color);
},
child: Container(
height: 40.0,
width: 40.0,
color: color,
),
);
}).toList(),
),
);
},
);

if (color != null) {
setState(() => splitButtonColor = color);
}
},
),
height: _kSplitButtonHeight,
width: _kSplitButtonWidth,
),
]),
),
flyout: splitButtonFlyout,
),
Padding(
padding: const EdgeInsets.only(bottom: 4.0, top: 8.0),
child: Text(
'A toggleable SplitButton with text content',
style: theme.typography.caption,
),
),
SplitButton.toggle(
enabled: !splitButtonDisabled,
checked: splitButtonState,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text('Choose color'),
),
onInvoked: () {
debugPrint('Invoked split button primary action');
setState(() => splitButtonState = !splitButtonState);
},
flyout: splitButtonFlyout,
),
]),
const Spacer(),
ToggleSwitch(
checked: splitButtonDisabled,
Expand All @@ -361,79 +355,55 @@ ToggleButton(
content: const Text('Disabled'),
),
]),
codeSnippet: '''SizedBox(
height: 40.0,
child: SplitButtonBar(buttons: [
Button(
style: ButtonStyle(padding: ButtonState.all(EdgeInsets.zero)),
child: Container(
decoration: BoxDecoration(
color: splitButtonDisabled
? splitButtonColor.secondaryBrushFor(
FluentTheme.of(context).brightness,
)
: splitButtonColor,
borderRadius: const BorderRadiusDirectional.horizontal(
start: Radius.circular(4.0),
),
),
height: kSplitButtonHeight,
width: kSplitButtonWidth,
),
onPressed: splitButtonDisabled ? null : () {},
),
FlyoutTarget(
controller: splitButtonFlyout,
child: IconButton(
icon: const SizedBox(
height: kSplitButtonHeight - 13.0,
width: kSplitButtonWidth - 13.0,
child: Icon(FluentIcons.chevron_down, size: 8.0),
),
onPressed: splitButtonDisabled
? null
: () async {
final color = await splitButtonFlyout.showFlyout<AccentColor>(
autoModeConfiguration: FlyoutAutoConfiguration(
preferredMode: FlyoutPlacementMode.bottomCenter,
),
builder: (context) {
return FlyoutContent(
constraints: BoxConstraints(maxWidth: 200.0),
child: Wrap(
runSpacing: 10.0,
spacing: 8.0,
children: Colors.accentColors.map((color) {
return Button(
autofocus: splitButtonColor == color,
style: ButtonStyle(
padding: ButtonState.all(
EdgeInsets.all(4.0),
),
),
onPressed: () {
Navigator.of(context).pop(color);
},
child: Container(
height: 40.0,
width: 40.0,
color: color,
),
);
}).toList(),
),
);
},
);
codeSnippet: '''final splitButtonKey = GlobalKey<SplitButtonState>();

if (color != null) {
setState(() => splitButtonColor = color);
}
},
// To create a toggle button, use the [SplitButton.toggle] constructor
SplitButton(
key: splitButtonKey,
enabled: !disabled,
child: Container(
decoration: BoxDecoration(
color: disabled
? color.secondaryBrushFor(theme.brightness)
: color,
borderRadius: const BorderRadiusDirectional.horizontal(
start: Radius.circular(4.0),
),
),
]),
)''',
height: $_kSplitButtonHeight,
width: $_kSplitButtonWidth,
),
flyout: FlyoutContent(
constraints: BoxConstraints(maxWidth: 200.0),
child: Wrap(
runSpacing: 10.0,
spacing: 8.0,
children: Colors.accentColors.map((color) {
return Button(
autofocus: splitButtonColor == color,
style: ButtonStyle(
padding: ButtonState.all(
EdgeInsets.all(4.0),
),
),
onPressed: () {
setState(() => splitButtonColor = color);
Navigator.of(context).pop(color);
},
child: Container(
height: $_kSplitButtonHeight,
width: $_kSplitButtonHeight,
color: color,
),
);
}).toList(),
),
),
)

// Show the flyout programmatically
splitButtonKey.currentState?.showFlyout();
''',
),
subtitle(content: const Text('RadioButton')),
description(
Expand Down
1 change: 1 addition & 0 deletions lib/fluent_ui.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export 'src/controls/surfaces/list_tile.dart';
export 'src/controls/surfaces/progress_indicators.dart';
export 'src/controls/surfaces/snackbar.dart';
export 'src/controls/surfaces/tooltip.dart';
export 'src/controls/utils/chevron_down.dart';
export 'src/controls/utils/divider.dart';
export 'src/controls/utils/hover_button.dart';
export 'src/controls/utils/info_badge.dart';
Expand Down
10 changes: 8 additions & 2 deletions lib/src/controls/inputs/buttons/theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,11 @@ class ButtonThemeData with Diagnosticable {

/// Defines the default color used by [Button]s using the current brightness
/// and state.
static Color buttonColor(BuildContext context, Set<ButtonStates> states) {
static Color buttonColor(
BuildContext context,
Set<ButtonStates> states, {
bool transparentWhenNone = false,
}) {
final res = FluentTheme.of(context).resources;
if (states.isPressing) {
return res.controlFillColorTertiary;
Expand All @@ -241,7 +245,9 @@ class ButtonThemeData with Diagnosticable {
} else if (states.isDisabled) {
return res.controlFillColorDisabled;
}
return res.controlFillColorDefault;
return transparentWhenNone
? res.subtleFillColorTransparent
: res.controlFillColorDefault;
}

/// Defines the default foregournd color used by [Button]s using the current brightness
Expand Down
6 changes: 2 additions & 4 deletions lib/src/controls/inputs/dropdown_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import 'package:fluent_ui/fluent_ui.dart';
import 'package:flutter/foundation.dart';

const double _kVerticalOffset = 6.0;
const Widget _kDefaultDropdownButtonTrailing = Icon(
FluentIcons.chevron_down,
size: 8.0,
);
const Widget _kDefaultDropdownButtonTrailing = ChevronDown();

typedef DropDownButtonBuilder = Widget Function(
BuildContext context,
Expand Down Expand Up @@ -325,6 +322,7 @@ class DropDownButtonState extends State<DropDownButton> {

widget.onOpen?.call();
await _flyoutController.showFlyout(
barrierColor: Colors.transparent,
placementMode: FlyoutPlacementMode.auto,
autoModeConfiguration: FlyoutAutoConfiguration(
preferredMode: widget.placement,
Expand Down
Loading