Skip to content

Commit

Permalink
flutter: fix incorrect rendering due to not using keys
Browse files Browse the repository at this point in the history
Flutter would reuse an arbitrary GroupWidget when rebuilding
the tree
  • Loading branch information
iamsergio committed Jan 19, 2025
1 parent ae38238 commit 36cd1bf
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/flutter/dart/lib/widgets/DockWidget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import 'package:KDDockWidgets/KDDockWidgets.dart';

class DockWidget extends StatefulWidget {
final DockItem dockItem;
DockWidget(this.dockItem, {Key? key}) : super(key: key) {}
DockWidget(this.dockItem) : super(key: ObjectKey(dockItem)) {}

@override
State<DockWidget> createState() {
Expand Down
2 changes: 1 addition & 1 deletion src/flutter/dart/lib/widgets/FloatingWidget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import 'package:flutter/material.dart';

class FloatingWidget extends StatefulWidget {
final FloatingItem floatingItem;
FloatingWidget(this.floatingItem, {Key? key}) : super(key: key);
FloatingWidget(this.floatingItem) : super(key: ObjectKey(floatingItem));

@override
State<FloatingWidget> createState() {
Expand Down
2 changes: 1 addition & 1 deletion src/flutter/dart/lib/widgets/GroupWidget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import 'package:signals_slots/signals_slots.dart';

class GroupWidget extends PositionedWidget {
final Group group;
GroupWidget(this.group, {Key? key}) : super(group, key: key);
GroupWidget(this.group) : super(group, key: ObjectKey(group));

@override
State<PositionedWidget> createState() {
Expand Down
2 changes: 1 addition & 1 deletion src/flutter/dart/lib/widgets/SeparatorWidget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import 'PositionedWidget.dart';

class SeparatorWidget extends PositionedWidget {
final Separator separator;
SeparatorWidget(this.separator, {Key? key}) : super(separator, key: key);
SeparatorWidget(this.separator) : super(separator, key: ObjectKey(separator));

@override
State<PositionedWidget> createState() {
Expand Down
2 changes: 1 addition & 1 deletion src/flutter/dart/lib/widgets/TabBarWidget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import 'dart:ui';

class TabBarWidget extends StatefulWidget {
final Group group;
TabBarWidget(this.group, {Key? key}) : super(key: key);
TabBarWidget(this.group) : super(key: ObjectKey(group));

@override
State<TabBarWidget> createState() {
Expand Down
2 changes: 1 addition & 1 deletion src/flutter/dart/lib/widgets/TitleBarWidget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import 'package:flutter/material.dart';

class TitleBarWidget extends StatefulWidget {
final TitleBar titlebar;
TitleBarWidget(this.titlebar, {Key? key}) : super(key: key);
TitleBarWidget(this.titlebar) : super(key: ObjectKey(titlebar));

@override
State<TitleBarWidget> createState() {
Expand Down
35 changes: 35 additions & 0 deletions tests/flutter/integration_test/ui_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,39 @@ void main() async {
await tester.pump();
await saveScreenShot(tester, prefix: "basic");
});

testWidgets('TitleBar close button', (WidgetTester tester) async {
final dock1 = DockItem(uniqueName: "dw1", guestWidget: MyWidget());
final dock2 = DockItem(uniqueName: "dw2", guestWidget: MyWidget());

final dropArea = DropArea();
dropArea.addDockItem(dock1, Location.LocationOnTop);
dropArea.addDockItem(dock2, Location.LocationOnBottom);

dropArea.setLayoutSize(700, 700);
final dropAreaWidget = DropAreaWidget(dropArea);

// Build the widget tree
await tester.pumpWidget(MyApp(
child: dropAreaWidget,
));

await tester.pump();
await saveScreenShot(tester, prefix: "titlebar-close-button");

expect(dropArea.groups.length, 2);
var topGroup = dropArea.groups.first;
var bottomGroup = dropArea.groups.last;

topGroup.titlebar.onCloseClicked();
expect(dropArea.containsGroup(topGroup), false);
expect(dropArea.containsGroup(bottomGroup), true);
expect(dropArea.groups.length, 1);
expect(dropArea.groups.first, bottomGroup);
expect(bottomGroup.numDockWidgets(), 1);
expect(bottomGroup.dockWidgetAt(0), dock2);

await tester.pump();
await saveScreenShot(tester, prefix: "titlebar-close-button-2");
});
}

0 comments on commit 36cd1bf

Please sign in to comment.