Skip to content

Commit

Permalink
[RSDK-4265] Board and Sensor Widgets (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
njooma authored Aug 2, 2023
1 parent 318fbce commit c36161b
Show file tree
Hide file tree
Showing 12 changed files with 347 additions and 129 deletions.
1 change: 1 addition & 0 deletions example/viam_example_app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.env
19 changes: 14 additions & 5 deletions example/viam_example_app/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:flutter_platform_widgets/flutter_platform_widgets.dart';
import 'package:viam_example_app/screens/base.dart';
import 'package:viam_example_app/screens/board.dart';
Expand All @@ -10,7 +11,8 @@ import 'package:viam_example_app/screens/stream.dart';
import 'package:viam_sdk/viam_sdk.dart';
import 'package:viam_sdk/widgets.dart';

void main() {
void main() async {
await dotenv.load();
runApp(const MyApp());
}

Expand Down Expand Up @@ -92,8 +94,8 @@ class _MyHomePageState extends State<MyHomePage> {
_loading = true;
});
final robotFut = RobotClient.atAddress(
'<URL>',
RobotClientOptions.withLocationSecret('<SECRET>'),
dotenv.env['ROBOT_LOCATION'] ?? '',
RobotClientOptions.withLocationSecret(dotenv.env['LOCATION_SECRET'] ?? ''),
);

robotFut.then((value) {
Expand Down Expand Up @@ -138,7 +140,6 @@ class _MyHomePageState extends State<MyHomePage> {
}
if (rname.subtype == Base.subtype.resourceSubtype && _cameraName != null) {
return BaseScreen(
resourceName: rname,
base: Base.fromRobot(_robot, rname.name),
cameras:
_robot.resourceNames.where((e) => e.subtype == Camera.subtype.resourceSubtype).map((e) => Camera.fromRobot(_robot, e.name)),
Expand Down Expand Up @@ -200,7 +201,15 @@ class _MyHomePageState extends State<MyHomePage> {
])
: _loading
? PlatformCircularProgressIndicator()
: ViamButton(onPressed: _login, text: 'Login', role: ViamButtonRole.inverse, style: ViamButtonStyle.filled)
: Column(children: [
ViamButton(
onPressed: _login,
text: 'Login',
role: ViamButtonRole.inverse,
style: ViamButtonFillStyle.filled,
size: ViamButtonSizeClass.xl,
)
])
],
),
),
Expand Down
5 changes: 1 addition & 4 deletions example/viam_example_app/lib/screens/base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@ import 'package:viam_sdk/widgets.dart';

class BaseScreen extends StatelessWidget {
final Base base;
final ResourceName resourceName;
final Iterable<Camera> cameras;
final RobotClient robot;

// TODO change BaseScreen to accept camera ResourceName.
const BaseScreen({Key? key, required this.base, required this.resourceName, required this.cameras, required this.robot})
: super(key: key);
const BaseScreen({Key? key, required this.base, required this.cameras, required this.robot}) : super(key: key);

@override
Widget build(BuildContext context) {
Expand Down
75 changes: 6 additions & 69 deletions example/viam_example_app/lib/screens/board.dart
Original file line number Diff line number Diff line change
@@ -1,94 +1,31 @@
import 'package:flutter/material.dart';
import 'package:flutter_platform_widgets/flutter_platform_widgets.dart';
import 'package:viam_sdk/viam_sdk.dart';
import 'package:viam_sdk/widgets/resources/board.dart';

class BoardScreen extends StatefulWidget {
class BoardScreen extends StatelessWidget {
final Board board;
final ResourceName resourceName;

const BoardScreen({Key? key, required this.board, required this.resourceName}) : super(key: key);

@override
State<BoardScreen> createState() {
return _BoardScreenState();
}
}

class _BoardScreenState extends State<BoardScreen> {
String getPin = '';
String setPin = '';
bool high = false;
late BoardStatus status = const BoardStatus(<String, int>{}, <String, int>{});

Future<void> _fetchStatus() async {
status = await widget.board.status();
setState(() {});
}

@override
void initState() {
super.initState();
_fetchStatus();
}

@override
Widget build(BuildContext context) {
Widget build(Object context) {
return PlatformScaffold(
appBar: PlatformAppBar(
title: Text(widget.resourceName.name.toUpperCase()),
title: Text(resourceName.name.toUpperCase()),
),
iosContentPadding: true,
body: Center(
child: Column(
children: [
const SizedBox(height: 16),
PlatformText(
'${widget.resourceName.namespace}:${widget.resourceName.type}:${widget.resourceName.subtype}/${widget.resourceName.name}',
'${resourceName.namespace}:${resourceName.type}:${resourceName.subtype}/${resourceName.name}',
style: const TextStyle(fontWeight: FontWeight.w300),
),
const SizedBox(height: 16),
PlatformText(
'Analogs: ${status.analogs}',
style: const TextStyle(fontWeight: FontWeight.w300),
),
const SizedBox(height: 16),
PlatformText(
'Digital Interrupts: ${status.digitalInterrupts}',
style: const TextStyle(fontWeight: FontWeight.w300),
),
const SizedBox(height: 16),
Text('GPIO', style: Theme.of(context).textTheme.headlineSmall),
Row(
children: [
const Spacer(),
Expanded(
child: TextFormField(
onChanged: (value) => setPin = value,
decoration: const InputDecoration(
border: UnderlineInputBorder(),
labelText: 'Pin',
),
),
),
const Spacer(),
DropdownButton(
value: high,
items: const [
DropdownMenuItem(value: true, child: Text('High')),
DropdownMenuItem(value: false, child: Text('Low')),
],
onChanged: ((value) => setState(() {
high = value!;
})),
),
const Spacer()
],
),
const SizedBox(height: 16),
PlatformElevatedButton(
child: const Text('Set Pin State'),
onPressed: () => widget.board.setGpioState(setPin, high),
)
ViamBoardWidget(board: board)
],
),
),
Expand Down
34 changes: 5 additions & 29 deletions example/viam_example_app/lib/screens/sensor.dart
Original file line number Diff line number Diff line change
@@ -1,55 +1,31 @@
import 'package:flutter/material.dart';
import 'package:flutter_platform_widgets/flutter_platform_widgets.dart';
import 'package:viam_sdk/viam_sdk.dart';
import 'package:viam_sdk/widgets.dart';

class SensorScreen extends StatefulWidget {
class SensorScreen extends StatelessWidget {
final Sensor sensor;
final ResourceName resourceName;

const SensorScreen({Key? key, required this.sensor, required this.resourceName}) : super(key: key);

@override
State<SensorScreen> createState() {
return _SensorScreenState();
}
}

class _SensorScreenState extends State<SensorScreen> {
Map<String, dynamic> readings = {};

void _getReadings() {
final readingsFut = widget.sensor.readings();
readingsFut.then((value) => setState(
() {
readings = value;
},
));
}

@override
Widget build(BuildContext context) {
return PlatformScaffold(
appBar: PlatformAppBar(
title: Text(widget.resourceName.name.toUpperCase()),
title: Text(resourceName.name.toUpperCase()),
),
iosContentPadding: true,
body: Center(
child: Column(
children: [
const Padding(padding: EdgeInsets.symmetric(vertical: 8, horizontal: 0)),
PlatformText(
'${widget.resourceName.namespace}:${widget.resourceName.type}:${widget.resourceName.subtype}/${widget.resourceName.name}',
'${resourceName.namespace}:${resourceName.type}:${resourceName.subtype}/${resourceName.name}',
style: const TextStyle(fontWeight: FontWeight.w300),
),
const Padding(padding: EdgeInsets.symmetric(vertical: 8, horizontal: 0)),
DataTable(
columns: const <DataColumn>[DataColumn(label: Text('Reading')), DataColumn(label: Text('Value'))],
rows: readings.keys.map((e) => DataRow(cells: [DataCell(Text(e)), DataCell(Text(readings[e].toString()))])).toList()),
const Padding(padding: EdgeInsets.symmetric(vertical: 8, horizontal: 0)),
PlatformElevatedButton(
child: const Text('Get readings'),
onPressed: () => _getReadings(),
)
ViamSensorWidget(sensor: sensor),
],
),
),
Expand Down
7 changes: 5 additions & 2 deletions example/viam_example_app/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
name: viam_example_app
description: An example app using Viam's Flutter SDK
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
publish_to: "none" # Remove this line if you wish to publish to pub.dev

version: 1.0.0+1

environment:
sdk: '>=3.0.0 <4.0.0'
sdk: ">=3.0.0 <4.0.0"

dependencies:
flutter:
Expand All @@ -16,6 +16,7 @@ dependencies:
flutter_platform_widgets: ^3.2.1
flutter_webrtc: ^0.9.35
image: ^4.0.17
flutter_dotenv: ^5.1.0

dev_dependencies:
flutter_launcher_icons: ^0.13.1
Expand All @@ -25,6 +26,8 @@ dev_dependencies:

flutter:
uses-material-design: true
assets:
- .env

flutter_launcher_icons:
android: "launcher_icon"
Expand Down
2 changes: 2 additions & 0 deletions lib/widgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ export 'widgets/button.dart';
export 'widgets/camera_stream.dart';
export 'widgets/joystick.dart';
export 'widgets/resources/base.dart';
export 'widgets/resources/board.dart';
export 'widgets/resources/sensor.dart';
Loading

0 comments on commit c36161b

Please sign in to comment.