-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
325 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,147 +1,143 @@ | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter/widgets.dart'; | ||
import 'package:forui/forui.dart'; | ||
import 'package:forui/src/widgets/slider/slider.dart'; | ||
import 'package:meta/meta.dart'; | ||
|
||
@internal | ||
class Bar extends StatelessWidget { | ||
final FSliderController controller; | ||
final FSliderStyle style; | ||
final LayoutDirection direction; | ||
final List<FSliderMark> marks; | ||
|
||
const Bar({ | ||
required this.controller, | ||
required this.style, | ||
required this.direction, | ||
required this.marks, | ||
super.key, | ||
}); | ||
const Bar({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
final InheritedData( | ||
:controller, | ||
style: FSliderStyle(:activeColor, :inactiveColor, :borderRadius, :crossAxisExtent, :markStyle, :thumbStyle), | ||
:direction, | ||
:marks, | ||
:enabled, | ||
) = InheritedData.of(context); | ||
|
||
late final double height; | ||
late final double width; | ||
late final double Function(TapDownDetails) translate; | ||
late final Widget Function(double, Widget) marker; | ||
late final ValueWidgetBuilder<FSliderData> active; | ||
|
||
final half = style.thumbStyle.dimension / 2; | ||
// We use the thumb style's dimension as the bar's padding. | ||
final half = thumbStyle.dimension / 2; | ||
|
||
switch (direction) { | ||
case LayoutDirection.ltr: | ||
height = style.crossAxisExtent; | ||
width = controller.value.extent.total + style.thumbStyle.dimension; | ||
height = crossAxisExtent; | ||
width = controller.value.extent.total + thumbStyle.dimension; | ||
translate = (details) => details.localPosition.dx - half; | ||
marker = (offset, marker) => Positioned(left: offset, child: marker); | ||
active = (context, active, child) => Positioned( | ||
left: active.offset.min, | ||
child: SizedBox( | ||
height: style.crossAxisExtent, | ||
height: crossAxisExtent, | ||
width: active.extent.current + half, | ||
child: child!, | ||
), | ||
); | ||
|
||
case LayoutDirection.rtl: | ||
height = style.crossAxisExtent; | ||
width = controller.value.extent.total + style.thumbStyle.dimension; | ||
height = crossAxisExtent; | ||
width = controller.value.extent.total + thumbStyle.dimension; | ||
translate = (details) => controller.value.extent.total + half - details.localPosition.dx; | ||
marker = (offset, marker) => Positioned(right: offset, child: marker); | ||
active = (context, active, child) => Positioned( | ||
right: active.offset.min, | ||
child: SizedBox( | ||
height: style.crossAxisExtent, | ||
height: crossAxisExtent, | ||
width: active.extent.current + half, | ||
child: child!, | ||
), | ||
); | ||
|
||
case LayoutDirection.ttb: | ||
height = controller.value.extent.total + style.thumbStyle.dimension; | ||
width = style.crossAxisExtent; | ||
height = controller.value.extent.total + thumbStyle.dimension; | ||
width = crossAxisExtent; | ||
translate = (details) => details.localPosition.dy - half; | ||
marker = (offset, marker) => Positioned(top: offset, child: marker); | ||
active = (context, active, child) => Positioned( | ||
top: active.offset.min, | ||
child: SizedBox( | ||
height: active.extent.current + half, | ||
width: style.crossAxisExtent, | ||
width: crossAxisExtent, | ||
child: child!, | ||
), | ||
); | ||
|
||
case LayoutDirection.btt: | ||
height = controller.value.extent.total + style.thumbStyle.dimension; | ||
width = style.crossAxisExtent; | ||
height = controller.value.extent.total + thumbStyle.dimension; | ||
width = crossAxisExtent; | ||
translate = (details) => controller.value.extent.total + half - details.localPosition.dy; | ||
marker = (offset, marker) => Positioned(bottom: offset, child: marker); | ||
active = (context, active, child) => Positioned( | ||
bottom: active.offset.min, | ||
child: SizedBox( | ||
height: active.extent.current + half, | ||
width: style.crossAxisExtent, | ||
width: crossAxisExtent, | ||
child: child!, | ||
), | ||
); | ||
} | ||
|
||
return GestureDetector( | ||
onTapDown: (details) => controller.tap( | ||
switch (translate(details)) { | ||
< 0 => 0, | ||
final translated when controller.value.extent.total < translated => controller.value.extent.total, | ||
final translated => translated, | ||
}, | ||
Widget bar = DecoratedBox( | ||
decoration: BoxDecoration( | ||
borderRadius: borderRadius, | ||
color: inactiveColor, | ||
), | ||
child: DecoratedBox( | ||
decoration: BoxDecoration( | ||
borderRadius: style.borderRadius, | ||
color: style.inactiveColor, | ||
), | ||
child: SizedBox( | ||
height: height, | ||
width: width, | ||
child: Stack( | ||
alignment: Alignment.center, | ||
children: [ | ||
for (final FSliderMark(:style, :percentage, :visible) in marks) | ||
if (visible) | ||
marker( | ||
percentage * controller.value.extent.total + half - ((style ?? this.style.markStyle).dimension / 2), | ||
DecoratedBox( | ||
decoration: BoxDecoration( | ||
shape: BoxShape.circle, | ||
color: (style ?? this.style.markStyle).color, | ||
), | ||
child: SizedBox.square( | ||
dimension: (style ?? this.style.markStyle).dimension, | ||
), | ||
child: SizedBox( | ||
height: height, | ||
width: width, | ||
child: Stack( | ||
alignment: Alignment.center, | ||
children: [ | ||
for (final FSliderMark(:style, :percentage, :visible) in marks) | ||
if (visible) | ||
marker( | ||
percentage * controller.value.extent.total + half - ((style ?? markStyle).dimension / 2), | ||
DecoratedBox( | ||
decoration: BoxDecoration( | ||
shape: BoxShape.circle, | ||
color: (style ?? markStyle).color, | ||
), | ||
child: SizedBox.square( | ||
dimension: (style ?? markStyle).dimension, | ||
), | ||
), | ||
ValueListenableBuilder( | ||
valueListenable: controller, | ||
builder: active, | ||
child: DecoratedBox( | ||
decoration: BoxDecoration( | ||
borderRadius: style.borderRadius, | ||
color: style.activeColor, | ||
), | ||
), | ||
ValueListenableBuilder( | ||
valueListenable: controller, | ||
builder: active, | ||
child: DecoratedBox( | ||
decoration: BoxDecoration( | ||
borderRadius: borderRadius, | ||
color: activeColor, | ||
), | ||
), | ||
], | ||
), | ||
), | ||
], | ||
), | ||
), | ||
); | ||
} | ||
|
||
@override | ||
void debugFillProperties(DiagnosticPropertiesBuilder properties) { | ||
super.debugFillProperties(properties); | ||
properties | ||
..add(DiagnosticsProperty('controller', controller)) | ||
..add(DiagnosticsProperty('style', style)) | ||
..add(EnumProperty('direction', direction)) | ||
..add(IterableProperty('marks', marks)); | ||
if (enabled) { | ||
bar = GestureDetector( | ||
onTapDown: (details) => controller.tap( | ||
switch (translate(details)) { | ||
< 0 => 0, | ||
final translated when controller.value.extent.total < translated => controller.value.extent.total, | ||
final translated => translated, | ||
}, | ||
), | ||
child: bar, | ||
); | ||
} | ||
|
||
return bar; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.