Skip to content

Commit

Permalink
Fixing state for the index of the highlighted tab. #31
Browse files Browse the repository at this point in the history
  • Loading branch information
caduandrade committed Sep 8, 2023
1 parent f074aed commit 2725d62
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* Highlighting the tab's drop position.
* Allow dragging to reorder tabs to the last position.
* Allow dragging tabs between different `TabbedView` instances.
* Bugfix
* Incorrect state for the index of the highlighted tab after being closed.

### Changes

Expand Down
2 changes: 2 additions & 0 deletions lib/src/tab_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,6 @@ class TabData extends ChangeNotifier with TabIndex {
_text = value;
notifyListeners();
}

final UniqueKey uniqueKey = UniqueKey();
}
17 changes: 11 additions & 6 deletions lib/src/tab_widget.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import 'package:flutter/material.dart';
import 'package:tabbed_view/src/draggable_config.dart';
import 'package:tabbed_view/src/flow_layout.dart';
import 'package:tabbed_view/src/draggable_data.dart';
import 'package:tabbed_view/src/flow_layout.dart';
import 'package:tabbed_view/src/internal/tabbed_view_provider.dart';
import 'package:tabbed_view/src/internal/tabs_area/drop_tab_widget.dart';
import 'package:tabbed_view/src/internal/tabs_area/tab_drag_feedback_widget.dart';
import 'package:tabbed_view/src/internal/tabbed_view_provider.dart';
import 'package:tabbed_view/src/tab_button.dart';
import 'package:tabbed_view/src/tab_button_widget.dart';
import 'package:tabbed_view/src/tab_data.dart';
Expand All @@ -20,15 +20,19 @@ typedef UpdateHighlightedIndex = void Function(int? tabIndex);
/// The tab widget. Displays the tab text and its buttons.
class TabWidget extends StatelessWidget {
const TabWidget(
{required this.index,
{required UniqueKey key,
required this.index,
required this.status,
required this.provider,
required this.updateHighlightedIndex});
required this.updateHighlightedIndex,
required this.onClose})
: super(key: key);

final int index;
final TabStatus status;
final TabbedViewProvider provider;
final UpdateHighlightedIndex updateHighlightedIndex;
final Function onClose;

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -84,8 +88,8 @@ class TabWidget extends StatelessWidget {

tabWidget = MouseRegion(
cursor: cursor,
onHover: (details) => updateHighlightedIndex(index),
onExit: (details) => updateHighlightedIndex(null),
onEnter: (event) => updateHighlightedIndex(index),
onExit: (event) => updateHighlightedIndex(null),
child: provider.draggingTabIndex == null
? GestureDetector(
onTap: () => _onSelect(context, index), child: tabWidget)
Expand Down Expand Up @@ -269,6 +273,7 @@ class TabWidget extends StatelessWidget {
void _onClose(BuildContext context, int index) {
if (provider.tabCloseInterceptor == null ||
provider.tabCloseInterceptor!(index)) {
onClose();
TabData tabData = provider.controller.removeTab(index);
if (provider.onTabClose != null) {
provider.onTabClose!(index, tabData);
Expand Down
10 changes: 9 additions & 1 deletion lib/src/tabs_area.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ class _TabsAreaState extends State<TabsArea> {
for (int index = 0; index < controller.tabs.length; index++) {
TabStatus status = _getStatusFor(index);
children.add(TabWidget(
key: controller.tabs[index].uniqueKey,
index: index,
status: status,
provider: widget.provider,
updateHighlightedIndex: _updateHighlightedIndex));
updateHighlightedIndex: _updateHighlightedIndex,
onClose: _onTabClose));
}

children.add(
Expand Down Expand Up @@ -82,4 +84,10 @@ class _TabsAreaState extends State<TabsArea> {
});
}
}

void _onTabClose() {
setState(() {
_highlightedIndex = null;
});
}
}

0 comments on commit 2725d62

Please sign in to comment.