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 RTL support #33

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
71 changes: 46 additions & 25 deletions lib/src/tab_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ class SegmentedTabControl extends StatelessWidget {
}
}

class _SegmentedTabControl extends StatefulWidget implements PreferredSizeWidget {
class _SegmentedTabControl extends StatefulWidget
implements PreferredSizeWidget {
const _SegmentedTabControl({
super.key,
required this.height,
Expand Down Expand Up @@ -210,7 +211,8 @@ class _SegmentedTabControlState extends State<_SegmentedTabControl>
}

void _calculateTotalFlex() {
_totalFlex = widget.tabs.fold(0, (previousValue, tab) => previousValue + tab.flex);
_totalFlex =
widget.tabs.fold(0, (previousValue, tab) => previousValue + tab.flex);
}

void _calculateFlexFactors() {
Expand All @@ -232,7 +234,8 @@ class _SegmentedTabControlState extends State<_SegmentedTabControl>
}

void _updateTabController() {
final TabController? newController = widget.controller ?? DefaultTabController.of(context);
final TabController? newController =
widget.controller ?? DefaultTabController.of(context);
assert(() {
if (newController == null) {
throw FlutterError(
Expand All @@ -259,7 +262,8 @@ class _SegmentedTabControlState extends State<_SegmentedTabControl>

if (_controller != null) {
_controller!.animation!.addListener(_handleTabControllerAnimationTick);
_currentIndicatorAlignment = _animationValueToAlignment(_controller!.index.toDouble());
_currentIndicatorAlignment =
_animationValueToAlignment(_controller!.index.toDouble());
}
}

Expand Down Expand Up @@ -311,9 +315,11 @@ class _SegmentedTabControlState extends State<_SegmentedTabControl>
final tabLeftX = index > 0 ? flexFactors[index - 1] * _maxWidth : 0;
double target;
if (reminder > 0) {
target = tabLeftX + ((reminder * 2) * (alignmentXRanges[index].endInclusive - tabLeftX));
target = tabLeftX +
((reminder * 2) * (alignmentXRanges[index].endInclusive - tabLeftX));
} else {
target = tabLeftX + ((reminder * 2) * (tabLeftX - alignmentXRanges[index].start));
target = tabLeftX +
((reminder * 2) * (tabLeftX - alignmentXRanges[index].start));
}

return target;
Expand All @@ -324,10 +330,11 @@ class _SegmentedTabControlState extends State<_SegmentedTabControl>
final currentTabHalfWidth = tabWidth / 2;
final halfMaxWidth = _maxWidth / 2;

final x =
(position - halfMaxWidth + currentTabHalfWidth) / (halfMaxWidth - currentTabHalfWidth);
final x = (position - halfMaxWidth + currentTabHalfWidth) /
(halfMaxWidth - currentTabHalfWidth);

return Alignment(x, 0);
final isRTL = Directionality.of(context) == TextDirection.rtl;
return Alignment(isRTL ? -x : x, 0);
}

TickerFuture _animateIndicatorTo(Alignment target) {
Expand All @@ -343,25 +350,30 @@ class _SegmentedTabControlState extends State<_SegmentedTabControl>
Widget build(BuildContext context) {
final currentTab = widget.tabs[_internalIndex];

final textStyle = widget.textStyle ?? Theme.of(context).textTheme.bodyMedium!;
final textStyle =
widget.textStyle ?? Theme.of(context).textTheme.bodyMedium!;

final selectedTextStyle = widget.selectedTextStyle ?? textStyle;

final selectedTabTextColor =
currentTab.selectedTextColor ?? widget.selectedTabTextColor ?? Colors.white;
final selectedTabTextColor = currentTab.selectedTextColor ??
widget.selectedTabTextColor ??
Colors.white;

final tabTextColor =
currentTab.textColor ?? widget.tabTextColor ?? Colors.white.withOpacity(0.7);
final tabTextColor = currentTab.textColor ??
widget.tabTextColor ??
Colors.white.withOpacity(0.7);

return DefaultTextStyle(
style: widget.textStyle ?? DefaultTextStyle.of(context).style,
child: LayoutBuilder(
builder: (context, _) {
final indicatorWidth = ((_maxWidth - widget.indicatorPadding.horizontal) / _totalFlex) *
widget.tabs[_internalIndex].flex;
final indicatorWidth =
((_maxWidth - widget.indicatorPadding.horizontal) / _totalFlex) *
widget.tabs[_internalIndex].flex;

return ClipRRect(
borderRadius: widget.barDecoration?.borderRadius ?? BorderRadius.zero,
borderRadius:
widget.barDecoration?.borderRadius ?? BorderRadius.zero,
child: SizedBox(
height: widget.height,
child: Stack(
Expand Down Expand Up @@ -407,7 +419,8 @@ class _SegmentedTabControlState extends State<_SegmentedTabControl>
duration: kTabScrollDuration,
curve: Curves.ease,
width: indicatorWidth,
height: widget.height - widget.indicatorPadding.vertical,
height: widget.height -
widget.indicatorPadding.vertical,
decoration: widget.indicatorDecoration?.copyWith(
color: currentTab.color,
gradient: currentTab.gradient,
Expand Down Expand Up @@ -478,7 +491,8 @@ class _SegmentedTabControlState extends State<_SegmentedTabControl>
return (details) {
_internalAnimationController.stop();
setState(() {
_currentTilePadding = EdgeInsets.symmetric(vertical: widget.squeezeIntensity);
_currentTilePadding =
EdgeInsets.symmetric(vertical: widget.squeezeIntensity);
});
};
}
Expand All @@ -488,7 +502,8 @@ class _SegmentedTabControlState extends State<_SegmentedTabControl>
return null;
}
return (details) {
double x = _currentIndicatorAlignment.x + details.delta.dx / (maxWidth / widget.tabs.length);
double x = _currentIndicatorAlignment.x +
details.delta.dx / (maxWidth / widget.tabs.length);
if (x < -1) {
x = -1;
} else if (x > 1) {
Expand All @@ -503,9 +518,11 @@ class _SegmentedTabControlState extends State<_SegmentedTabControl>

int _alignmentToIndex(Alignment alignment) {
final currentPosition = _xToPercentsCoefficient(alignment);
final roundedCurrentPosition = num.parse(currentPosition.toStringAsFixed(2));
final roundedCurrentPosition =
num.parse(currentPosition.toStringAsFixed(2));

final index = flexFactors.indexWhere((flexFactor) => roundedCurrentPosition <= flexFactor);
final index = flexFactors
.indexWhere((flexFactor) => roundedCurrentPosition <= flexFactor);

return index == -1 ? _controller!.length - 1 : index;
}
Expand All @@ -528,7 +545,8 @@ class _SegmentedTabControlState extends State<_SegmentedTabControl>
};
}

TickerFuture _animateIndicatorToNearest(Offset pixelsPerSecond, double width) {
TickerFuture _animateIndicatorToNearest(
Offset pixelsPerSecond, double width) {
final nearest = _internalIndex;
final target = _animationValueToAlignment(nearest.toDouble());
_internalAnimation = _internalAnimationController.drive(AlignmentTween(
Expand Down Expand Up @@ -588,7 +606,8 @@ class _Labels extends StatelessWidget {
flex: tab.flex,
child: InkWell(
splashColor: tab.splashColor ?? splashColor,
highlightColor: tab.splashHighlightColor ?? splashHighlightColor,
highlightColor:
tab.splashHighlightColor ?? splashHighlightColor,
borderRadius: radius as BorderRadius?,
onTap: callbackBuilder?.call(index),
child: Padding(
Expand All @@ -597,7 +616,9 @@ class _Labels extends StatelessWidget {
child: AnimatedDefaultTextStyle(
duration: kTabScrollDuration,
curve: Curves.ease,
style: (index == currentIndex) ? selectedTextStyle : textStyle,
style: (index == currentIndex)
? selectedTextStyle
: textStyle,
child: Text(
tab.label,
overflow: TextOverflow.clip,
Expand Down