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 hoverColor and margin between tabs #153

Open
wants to merge 1 commit into
base: master
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
41 changes: 29 additions & 12 deletions lib/src/components/navigation/tabs.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
import 'package:shadcn_flutter/shadcn_flutter.dart';

class Tabs extends StatelessWidget {
class Tabs extends StatefulWidget {
final int index;
final ValueChanged<int> onChanged;
final List<Widget> tabs;
final EdgeInsetsGeometry? padding;
final Color? hoverColor;
final double? marginBetweenItems;

const Tabs({
super.key,
required this.index,
required this.onChanged,
required this.tabs,
this.padding,
this.hoverColor,
this.marginBetweenItems,
});

@override
State<Tabs> createState() => _TabsState();
}

class _TabsState extends State<Tabs> {
int? hoveredIndex;

@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
Expand All @@ -29,37 +40,43 @@ class Tabs extends StatelessWidget {
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
for (var i = 0; i < tabs.length; i++)
for (var i = 0; i < widget.tabs.length; i++)
Expanded(
child: GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () {
onChanged(i);
widget.onChanged(i);
},
child: MouseRegion(
hitTestBehavior: HitTestBehavior.translucent,
cursor: SystemMouseCursors.click,
onEnter: (_) => setState(() => hoveredIndex = i),
onExit: (_) => setState(() => hoveredIndex = null),
child: AnimatedContainer(
duration: const Duration(
milliseconds:
50), // slightly faster than kDefaultDuration
margin: EdgeInsets.only(
right: widget.marginBetweenItems ?? 1),
duration: const Duration(milliseconds: 50),
alignment: Alignment.center,
padding: padding ??
padding: widget.padding ??
const EdgeInsets.symmetric(
horizontal: 16,
vertical: 4,
) *
scaling,
decoration: BoxDecoration(
color:
i == index ? theme.colorScheme.background : null,
color: i == widget.index
? theme.colorScheme.background
: hoveredIndex == i
? widget.hoverColor ??
theme.colorScheme.primaryForeground
: null,
borderRadius: BorderRadius.circular(
theme.radiusMd,
),
),
child: (i == index
? tabs[i].foreground()
: tabs[i].muted())
child: (i == widget.index
? widget.tabs[i].foreground()
: widget.tabs[i].muted())
.small()
.medium(),
),
Expand Down