见下面代码
import 'dart:io';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Lighting controller'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
void _open() {
setState(() {
RawDatagramSocket.bind(InternetAddress.ANY_IP_V4, 0)
.then((RawDatagramSocket socket) {
print('Sending from ${socket.address.address}:${socket.port}');
int port = 4000;
socket.send('open'.codeUnits,
InternetAddress("192.168.205.240"), port);
});
});
}
void _close() {
setState(() {
RawDatagramSocket.bind(InternetAddress.ANY_IP_V4, 0)
.then((RawDatagramSocket socket) {
print('Sending from ${socket.address.address}:${socket.port}');
int port = 4000;
socket.send('close'.codeUnits,
InternetAddress("192.168.205.240"), port);
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
FlatButton(
color: Colors.blue,
highlightColor: Colors.blue[700],
colorBrightness: Brightness.dark,
splashColor: Colors.grey,
padding: EdgeInsets.fromLTRB(40, 20, 40, 20),
child: Text("开",
style: TextStyle(
fontSize: 40,
),
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0)),
onPressed: _open,
),
Padding(
padding: new EdgeInsets.fromLTRB(10, 100, 10, 10),
),
FlatButton(
color: Colors.red,
highlightColor: Colors.red[700],
colorBrightness: Brightness.dark,
splashColor: Colors.grey,
padding: EdgeInsets.fromLTRB(40, 20, 40, 20),
child: Text("关",
style: TextStyle(
fontSize: 40,
),
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0)),
onPressed: _close,
),
],
),
),
);
}
}

网友评论