Skip to content

Commit

Permalink
Add restore default to color picker (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gold872 authored Aug 10, 2024
1 parent b0d1fa0 commit bcb85f7
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 10 deletions.
25 changes: 20 additions & 5 deletions lib/widgets/dialog_widgets/dialog_color_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ class DialogColorPicker extends StatefulWidget {
final Function(Color color) onColorPicked;
final String label;
final Color initialColor;
final Color? defaultColor;

const DialogColorPicker(
{super.key,
required this.onColorPicked,
required this.label,
required this.initialColor});
const DialogColorPicker({
super.key,
required this.onColorPicked,
required this.label,
required this.initialColor,
this.defaultColor,
});

@override
State<DialogColorPicker> createState() => _DialogColorPickerState();
Expand Down Expand Up @@ -106,6 +109,18 @@ class _DialogColorPickerState extends State<DialogColorPicker> {
},
child: const Text('Cancel'),
),
if (widget.defaultColor != null)
TextButton(
onPressed: () {
Navigator.of(context).pop(false);
widget.onColorPicked.call(widget.defaultColor!);

setState(() {
selectedColor = widget.defaultColor!;
});
},
child: const Text('Restore Default'),
),
TextButton(
onPressed: () {
Navigator.of(context).pop(false);
Expand Down
2 changes: 2 additions & 0 deletions lib/widgets/nt_widgets/multi-topic/field_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ class FieldWidgetModel extends NTWidgetModel {
},
label: 'Robot Color',
initialColor: robotColor,
defaultColor: Colors.red,
),
),
),
Expand All @@ -372,6 +373,7 @@ class FieldWidgetModel extends NTWidgetModel {
},
label: 'Trajectory Color',
initialColor: trajectoryColor,
defaultColor: Colors.white,
),
),
),
Expand Down
2 changes: 2 additions & 0 deletions lib/widgets/nt_widgets/single_topic/boolean_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ class BooleanBoxModel extends NTWidgetModel {
},
label: 'True Color',
initialColor: _trueColor,
defaultColor: Colors.green,
),
const SizedBox(width: 10),
DialogColorPicker(
Expand All @@ -148,6 +149,7 @@ class BooleanBoxModel extends NTWidgetModel {
},
label: 'False Color',
initialColor: _falseColor,
defaultColor: Colors.red,
),
],
),
Expand Down
12 changes: 7 additions & 5 deletions lib/widgets/nt_widgets/single_topic/graph.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,13 @@ class GraphModel extends NTWidgetModel {
children: [
Flexible(
child: DialogColorPicker(
onColorPicked: (color) {
mainColor = color;
},
label: 'Graph Color',
initialColor: _mainColor),
onColorPicked: (color) {
mainColor = color;
},
label: 'Graph Color',
initialColor: _mainColor,
defaultColor: Colors.cyan,
),
),
Flexible(
child: DialogTextInput(
Expand Down
1 change: 1 addition & 0 deletions lib/widgets/settings_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ class _SettingsDialogState extends State<SettingsDialog> {
onColorPicked: (color) => widget.onColorChanged?.call(color),
label: 'Team Color',
initialColor: currentColor,
defaultColor: Colors.blueAccent,
),
),
],
Expand Down
162 changes: 162 additions & 0 deletions test/widgets/dialog_widgets/dialog_color_picker_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
import 'package:flutter/material.dart';

import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';

import 'package:elastic_dashboard/widgets/dialog_widgets/dialog_color_picker.dart';
import '../../test_util.dart';

class MockColorCallback extends Mock {
void onColorChanged(Color? color);
}

void main() {
TestWidgetsFlutterBinding.ensureInitialized();

testWidgets('Color picker select', (widgetTester) async {
FlutterError.onError = ignoreOverflowErrors;

MockColorCallback mockCallback = MockColorCallback();

Color? calledBackColor;

when(mockCallback.onColorChanged(any)).thenAnswer((realInvocation) {
calledBackColor = realInvocation.positionalArguments[0];
});

await widgetTester.pumpWidget(
MaterialApp(
home: Scaffold(
body: DialogColorPicker(
onColorPicked: mockCallback.onColorChanged,
label: 'Color Picker',
initialColor: Colors.green,
),
),
),
);

await widgetTester.pumpAndSettle();

expect(find.text('Color Picker'), findsOneWidget);
expect(find.byType(ElevatedButton), findsOneWidget);

await widgetTester.tap(find.byType(ElevatedButton));
await widgetTester.pumpAndSettle();

expect(find.text('Cancel'), findsOneWidget);
expect(find.text('Restore Default'), findsNothing);
expect(find.text('Save'), findsOneWidget);

final hexInput = find.widgetWithText(TextField, 'Hex Code');

expect(hexInput, findsOneWidget);

await widgetTester.enterText(hexInput, '0000FF');
await widgetTester.testTextInput.receiveAction(TextInputAction.done);
await widgetTester.pumpAndSettle();

expect(calledBackColor, isNull);

await widgetTester.tap(find.text('Save'));
await widgetTester.pumpAndSettle();

expect(calledBackColor, isNotNull);
expect(calledBackColor!.value, 0xFF0000FF);
});

testWidgets('Color picker cancel', (widgetTester) async {
FlutterError.onError = ignoreOverflowErrors;

MockColorCallback mockCallback = MockColorCallback();

Color? calledBackColor;

when(mockCallback.onColorChanged(any)).thenAnswer((realInvocation) {
calledBackColor = realInvocation.positionalArguments[0];
});

await widgetTester.pumpWidget(
MaterialApp(
home: Scaffold(
body: DialogColorPicker(
onColorPicked: mockCallback.onColorChanged,
label: 'Color Picker',
initialColor: Colors.green,
),
),
),
);

await widgetTester.pumpAndSettle();

expect(find.text('Color Picker'), findsOneWidget);
expect(find.byType(ElevatedButton), findsOneWidget);

await widgetTester.tap(find.byType(ElevatedButton));
await widgetTester.pumpAndSettle();

expect(find.text('Cancel'), findsOneWidget);
expect(find.text('Restore Default'), findsNothing);
expect(find.text('Save'), findsOneWidget);

final hexInput = find.widgetWithText(TextField, 'Hex Code');

expect(hexInput, findsOneWidget);

await widgetTester.enterText(hexInput, '0000FF');
await widgetTester.testTextInput.receiveAction(TextInputAction.done);
await widgetTester.pumpAndSettle();

expect(calledBackColor, isNull);

await widgetTester.tap(find.text('Cancel'));
await widgetTester.pumpAndSettle();

expect(calledBackColor, isNotNull);
expect(calledBackColor!.value, Colors.green.value);
});

testWidgets('Color picker restore default', (widgetTester) async {
FlutterError.onError = ignoreOverflowErrors;

MockColorCallback mockCallback = MockColorCallback();

Color? calledBackColor;

when(mockCallback.onColorChanged(any)).thenAnswer((realInvocation) {
calledBackColor = realInvocation.positionalArguments[0];
});

await widgetTester.pumpWidget(
MaterialApp(
home: Scaffold(
body: DialogColorPicker(
onColorPicked: mockCallback.onColorChanged,
label: 'Color Picker',
initialColor: Colors.green,
defaultColor: Colors.red,
),
),
),
);

await widgetTester.pumpAndSettle();

expect(find.text('Color Picker'), findsOneWidget);
expect(find.byType(ElevatedButton), findsOneWidget);

await widgetTester.tap(find.byType(ElevatedButton));
await widgetTester.pumpAndSettle();

expect(find.text('Cancel'), findsOneWidget);
expect(find.text('Restore Default'), findsOneWidget);
expect(find.text('Save'), findsOneWidget);

await widgetTester.tap(find.text('Restore Default'));
await widgetTester.pumpAndSettle();

expect(calledBackColor, isNotNull);
expect(calledBackColor!.value, Colors.red.value);
});
}

0 comments on commit bcb85f7

Please sign in to comment.