roslib_dart/example/service/lib/main.dart

89 lines
2.2 KiB
Dart
Raw Normal View History

2022-02-22 20:23:58 +08:00
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:roslibdart/roslibdart.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Roslibdart Service Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Roslibdart Service Example'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
late Ros ros;
late Service service;
int msgToPublished = 0;
@override
void initState() {
2024-10-12 14:04:26 +08:00
ros = Ros(url: 'ws://192.168.1.149:9090');
2022-02-22 20:23:58 +08:00
service = Service(
2024-10-12 14:04:26 +08:00
name: '/add_two_ints', ros: ros, type: "/tutorial_interfaces/AddTwoInts");
2022-02-22 20:23:58 +08:00
super.initState();
ros.connect();
Timer(const Duration(seconds: 3), () async {
2024-10-12 14:04:26 +08:00
// await service.advertise(serviceHandler);
var res = await service.call({'a': 1, 'b': 2});
print(res);
2022-02-22 20:23:58 +08:00
});
}
void destroyConnection() async {
await ros.close();
setState(() {});
}
Future<Map<String, dynamic>>? serviceHandler(
Map<String, dynamic> args) async {
Map<String, dynamic> response = {};
response['sum'] = args['a'] + args['b'];
msgToPublished = response['sum'];
setState(() {});
return response;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
2022-02-22 21:54:27 +08:00
Text('Service returns answer ' + msgToPublished.toString()),
2022-02-22 20:23:58 +08:00
],
),
),
);
}
}