Skip to content

Commit

Permalink
Ready widgets to be expanded (viamrobotics#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
njooma authored Sep 28, 2023
1 parent 59ff247 commit 7b583a1
Show file tree
Hide file tree
Showing 9 changed files with 323 additions and 235 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ setup:
dart pub global activate cider

format:
dart format --line-length=140 --set-exit-if-changed $$(find . -name "*.dart" -not -path "./lib/src/gen/*" -not -path "**.mocks.dart")
dart format --line-length=140 --set-exit-if-changed $$(find . -name "*.dart" -not -path "./lib/src/gen/*" -not -path "**.mocks.dart" -not -path "./.dart_tool/*")

test:
flutter test
Expand Down
2 changes: 1 addition & 1 deletion example/viam_example_app/lib/screens/base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class BaseScreen extends StatelessWidget {
),
iosContentPadding: true,
body: Center(
child: ViamBaseScreen(
child: ViamBaseWidget(
base: base,
cameras: cameras,
robotClient: robot,
Expand Down
2 changes: 1 addition & 1 deletion example/viam_example_app/lib/screens/gripper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class GripperScreen extends StatelessWidget {
),
iosContentPadding: true,
body: Center(
child: ViamGripperScreen(
child: ViamGripperWidget(
gripper: gripper,
cameras: cameras,
robotClient: robot,
Expand Down
1 change: 1 addition & 0 deletions lib/widgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export 'widgets/resources/board.dart';
export 'widgets/resources/gripper.dart';
export 'widgets/resources/motor.dart';
export 'widgets/resources/sensor.dart';
export 'widgets/resources/servo.dart';
19 changes: 6 additions & 13 deletions lib/widgets/resources/base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import '../multi_camera_stream.dart';
///
/// This widget provides a joystick for moving a [Base],
/// along with displaying any camera streams that might be available on the robot.
class ViamBaseScreen extends StatefulWidget {
class ViamBaseWidget extends StatefulWidget {
/// The [Base]
final Base base;

Expand All @@ -18,18 +18,18 @@ class ViamBaseScreen extends StatefulWidget {
/// The current [RobotClient]
final RobotClient robotClient;

const ViamBaseScreen({
const ViamBaseWidget({
Key? key,
required this.base,
required this.cameras,
required this.robotClient,
}) : super(key: key);

@override
State<ViamBaseScreen> createState() => _ViamBaseScreenState();
State<ViamBaseWidget> createState() => _ViamBaseWidgetState();
}

class _ViamBaseScreenState extends State<ViamBaseScreen> {
class _ViamBaseWidgetState extends State<ViamBaseWidget> {
Camera? camera;

@override
Expand All @@ -45,22 +45,15 @@ class _ViamBaseScreenState extends State<ViamBaseScreen> {
return const SizedBox.shrink();
}

Widget _spacer() {
if (widget.cameras.isNotEmpty) {
return const Spacer();
}
return const SizedBox.shrink();
}

@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
_buildCamera(),
_spacer(),
Padding(
padding: const EdgeInsets.fromLTRB(0, 0, 0, 48),
padding: const EdgeInsets.only(bottom: 48),
child: Center(
child: ViamBaseJoystick(base: widget.base),
),
Expand Down
405 changes: 201 additions & 204 deletions lib/widgets/resources/board.dart

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions lib/widgets/resources/gripper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import '../multi_camera_stream.dart';
///
/// This widget provides open and grab functionality for a [Gripper],
/// along with displaying any camera streams that might be available on the robot.
class ViamGripperScreen extends StatefulWidget {
class ViamGripperWidget extends StatefulWidget {
/// The [Gripper]
final Gripper gripper;

Expand All @@ -20,18 +20,18 @@ class ViamGripperScreen extends StatefulWidget {
/// The current [RobotClient]
final RobotClient robotClient;

const ViamGripperScreen({
const ViamGripperWidget({
Key? key,
required this.gripper,
required this.cameras,
required this.robotClient,
}) : super(key: key);

@override
State<ViamGripperScreen> createState() => _ViamGripperScreenState();
State<ViamGripperWidget> createState() => _ViamGripperWidgetState();
}

class _ViamGripperScreenState extends State<ViamGripperScreen> {
class _ViamGripperWidgetState extends State<ViamGripperWidget> {
Camera? camera;
bool _isOpen = false;

Expand Down
15 changes: 4 additions & 11 deletions lib/widgets/resources/motor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,7 @@ class ViamMotorWidget extends StatefulWidget {
class _ViamMotorWidgetState extends State<ViamMotorWidget> {
double power = 0;
bool autoStop = true;

void showError(Object error) {
showDialog(
context: context,
builder: ((context) => AlertDialog(
title: const Text('Error'),
content: Text('Caught error: $error'),
)));
}
Error? error;

Future<void> setPower(double power) async {
try {
Expand All @@ -38,7 +30,7 @@ class _ViamMotorWidgetState extends State<ViamMotorWidget> {
this.power = power;
});
} catch (e) {
showError(e);
error = e as Error;
}
}

Expand All @@ -56,7 +48,7 @@ class _ViamMotorWidgetState extends State<ViamMotorWidget> {
}
}
} catch (e) {
showError(e);
error = e as Error;
}
}

Expand Down Expand Up @@ -122,6 +114,7 @@ class _ViamMotorWidgetState extends State<ViamMotorWidget> {
onChangeEnd: _handleSliderRelease,
),
),
if (error != null) Text('Error: $error', style: const TextStyle(color: Colors.red)),
],
),
);
Expand Down
104 changes: 104 additions & 0 deletions lib/widgets/resources/servo.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import 'package:flutter/material.dart';
import 'package:viam_sdk/viam_sdk.dart';
import 'package:viam_sdk/widgets.dart';

class ViamServoWidget extends StatefulWidget {
final Servo servo;

const ViamServoWidget({Key? key, required this.servo}) : super(key: key);

@override
State<ViamServoWidget> createState() {
return _ViamServoWidgetState();
}
}

class _ViamServoWidgetState extends State<ViamServoWidget> {
int angle = 0;
String moveTo = '0';
Error? error;

@override
void initState() {
_getPosition();

super.initState();
}

Future<void> _getPosition() async {
try {
final angle = await widget.servo.position();
if (mounted) {
setState(() {
this.angle = angle;
});
}
} catch (e) {
error = e as Error;
}
}

Future<void> _move(int distance) async {
try {
await widget.servo.move(distance);
await _getPosition();
} catch (e) {
error = e as Error;
}
}

Future<void> _stop() async {
await widget.servo.stop();
await _getPosition();
}

@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Angle: $angle', style: Theme.of(context).textTheme.titleLarge),
const SizedBox(height: 5),
Row(
children: [
ViamButton(
onPressed: () => _move(angle - 10),
text: '-10',
size: ViamButtonSizeClass.small,
),
const SizedBox(width: 4),
ViamButton(
onPressed: () => _move(angle - 1),
text: '-1',
size: ViamButtonSizeClass.small,
),
const SizedBox(width: 4),
ViamButton(
onPressed: () => _move(angle + 1),
text: '1',
size: ViamButtonSizeClass.small,
),
const SizedBox(width: 4),
ViamButton(
onPressed: () => _move(angle + 10),
text: '10',
size: ViamButtonSizeClass.small,
),
],
),
const SizedBox(height: 8),
Row(
children: [
ViamButton(
onPressed: () => _stop(),
text: 'STOP',
role: ViamButtonRole.danger,
size: ViamButtonSizeClass.small,
),
],
),
if (error != null) Text('Error: $error', style: const TextStyle(color: Colors.red)),
],
);
}
}

0 comments on commit 7b583a1

Please sign in to comment.