2022-09-15 21:59:37 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
2023-03-27 17:02:44 +00:00
|
|
|
/// For some reason original [SegmentedButton] does not have animations.
|
|
|
|
///
|
|
|
|
/// The [SegmentedButtons] was written for SelfPrivacy before [SegmentedButton] was introduced.
|
|
|
|
/// While it doesn't have that much options to pass, it has cute little animation.
|
|
|
|
/// It is based on [ToggleButtons].
|
2022-09-15 21:59:37 +00:00
|
|
|
class SegmentedButtons extends StatelessWidget {
|
2023-03-27 17:02:44 +00:00
|
|
|
/// Creates a segmented buttons widget. This is a SelfPrivacy implementation.
|
|
|
|
///
|
|
|
|
/// Provide the button titles in [titles] as a [List<String>].
|
|
|
|
/// Current selection is provided in [isSelected] as a [List<bool>].
|
|
|
|
/// This widget will call [onPressed] with the index of the button that was pressed.
|
2022-09-15 21:59:37 +00:00
|
|
|
const SegmentedButtons({
|
|
|
|
required this.isSelected,
|
|
|
|
required this.onPressed,
|
|
|
|
required this.titles,
|
2022-10-26 16:26:09 +00:00
|
|
|
super.key,
|
2022-09-15 21:59:37 +00:00
|
|
|
});
|
|
|
|
|
2023-03-27 17:02:44 +00:00
|
|
|
/// The current selection state of the buttons.
|
|
|
|
///
|
|
|
|
/// The length of this list must be equal to the length of [titles].
|
|
|
|
/// Several buttons can be selected at the same time.
|
2022-09-15 21:59:37 +00:00
|
|
|
final List<bool> isSelected;
|
2023-03-27 17:02:44 +00:00
|
|
|
|
|
|
|
/// The callback that is called when a button is pressed.
|
|
|
|
/// It will be called with the index of the button that was pressed.
|
2022-09-15 21:59:37 +00:00
|
|
|
final Function(int)? onPressed;
|
2023-03-27 17:02:44 +00:00
|
|
|
|
|
|
|
/// The titles of the buttons.
|
2022-09-15 21:59:37 +00:00
|
|
|
final List<String> titles;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(final BuildContext context) => LayoutBuilder(
|
|
|
|
builder: (final context, final constraints) => ToggleButtons(
|
|
|
|
constraints: BoxConstraints(
|
2023-03-27 17:02:44 +00:00
|
|
|
minWidth: (constraints.maxWidth - 8) / titles.length,
|
2022-09-16 10:07:26 +00:00
|
|
|
minHeight: 40 + Theme.of(context).visualDensity.vertical * 4,
|
2022-09-15 21:59:37 +00:00
|
|
|
),
|
|
|
|
borderRadius: BorderRadius.circular(48),
|
|
|
|
borderColor: Theme.of(context).colorScheme.outline,
|
|
|
|
selectedBorderColor: Theme.of(context).colorScheme.outline,
|
|
|
|
fillColor: Theme.of(context).colorScheme.secondaryContainer,
|
|
|
|
selectedColor: Theme.of(context).colorScheme.onSecondaryContainer,
|
|
|
|
color: Theme.of(context).colorScheme.onSurface,
|
|
|
|
isSelected: isSelected,
|
|
|
|
onPressed: onPressed,
|
|
|
|
children: titles.asMap().entries.map((final entry) {
|
|
|
|
final index = entry.key;
|
|
|
|
final title = entry.value;
|
2022-09-15 22:28:10 +00:00
|
|
|
return Stack(
|
|
|
|
alignment: Alignment.centerLeft,
|
2022-09-15 21:59:37 +00:00
|
|
|
children: [
|
2022-09-15 22:28:10 +00:00
|
|
|
AnimatedOpacity(
|
|
|
|
duration: const Duration(milliseconds: 200),
|
|
|
|
opacity: isSelected[index] ? 1 : 0,
|
|
|
|
child: AnimatedScale(
|
|
|
|
duration: const Duration(milliseconds: 200),
|
2023-02-24 10:38:57 +00:00
|
|
|
curve: Curves.easeInOutCubicEmphasized,
|
2022-09-15 22:28:10 +00:00
|
|
|
alignment: Alignment.centerLeft,
|
|
|
|
scale: isSelected[index] ? 1 : 0,
|
|
|
|
child: Icon(
|
|
|
|
Icons.check,
|
|
|
|
size: 18,
|
|
|
|
color: Theme.of(context).colorScheme.onSecondaryContainer,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
AnimatedPadding(
|
|
|
|
padding: isSelected[index]
|
|
|
|
? const EdgeInsets.only(left: 24)
|
|
|
|
: EdgeInsets.zero,
|
|
|
|
duration: const Duration(milliseconds: 200),
|
2023-02-24 10:38:57 +00:00
|
|
|
curve: Curves.easeInOutCubicEmphasized,
|
2022-09-15 22:28:10 +00:00
|
|
|
child: Text(
|
|
|
|
title,
|
|
|
|
style: Theme.of(context).textTheme.labelLarge,
|
2022-09-15 21:59:37 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}).toList(),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|