Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- Row
- Cached Image
- MainAxisAlignment
- Row Widget
- WillPopScope
- Flutter Example
- node.js
- ListTile
- Column Widget
- ListView.builder
- CrossAxisAlignment
- Load Image
- Flutter Tutorial
- Flutter 강좌
- 반석천
- listview
- HTTP
- Image.network
- Flutter 예제
- Snackbar
- InkWell
- Networking
- navigator
- Flutter 앱 배포
- FutureBuilder
- Scaffold
- sqlite
- Hello World
- flutter
- AppBar
Archives
- Today
- Total
꿈꾸는 시스템 디자이너
Flutter Example - TCP Socket Client 본문
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:get_ip/get_ip.dart';
import 'package:shared_preferences/shared_preferences.dart';
SocketClientState pageState;
class SocketClient extends StatefulWidget {
@override
SocketClientState createState() {
pageState = SocketClientState();
return pageState;
}
}
class SocketClientState extends State<SocketClient> {
final scaffoldKey = GlobalKey<ScaffoldState>();
String localIP = "";
int port = 9000;
List<MessageItem> items = List<MessageItem>();
TextEditingController ipCon = TextEditingController();
TextEditingController msgCon = TextEditingController();
Socket clientSocket;
@override
void initState() {
super.initState();
getIP();
WidgetsBinding.instance.addPostFrameCallback((_) {
_loadServerIP();
});
}
@override
void dispose() {
disconnectFromServer();
super.dispose();
}
void getIP() async {
var ip = await GetIp.ipAddress;
setState(() {
localIP = ip;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
key: scaffoldKey,
appBar: AppBar(title: Text("Socket Client")),
body: Column(
children: <Widget>[
ipInfoArea(),
connectArea(),
messageListArea(),
submitArea(),
],
));
}
Widget ipInfoArea() {
return Card(
child: ListTile(
dense: true,
leading: Text("IP"),
title: Text(localIP),
),
);
}
Widget connectArea() {
return Card(
child: ListTile(
dense: true,
leading: Text("Server IP"),
title: TextField(
controller: ipCon,
decoration: InputDecoration(
contentPadding:
const EdgeInsets.symmetric(vertical: 10, horizontal: 10),
isDense: true,
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(5)),
borderSide: BorderSide(color: Colors.grey[300]),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(5)),
borderSide: BorderSide(color: Colors.grey[400]),
),
filled: true,
fillColor: Colors.grey[50]),
),
trailing: RaisedButton(
child: Text((clientSocket != null) ? "Disconnect" : "Connect"),
onPressed:
(clientSocket != null) ? disconnectFromServer : connectToServer,
),
),
);
}
Widget messageListArea() {
return Expanded(
child: ListView.builder(
reverse: true,
itemCount: items.length,
itemBuilder: (context, index) {
MessageItem item = items[index];
return Container(
alignment: (item.owner == localIP)
? Alignment.centerRight
: Alignment.centerLeft,
child: Container(
margin: const EdgeInsets.symmetric(vertical: 5, horizontal: 10),
padding:
const EdgeInsets.symmetric(vertical: 10, horizontal: 10),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: (item.owner == localIP)
? Colors.blue[100]
: Colors.grey[200]),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
(item.owner == localIP) ? "Client" : "Server",
style: TextStyle(fontWeight: FontWeight.bold),
),
Text(
item.content,
style: TextStyle(fontSize: 18),
),
],
),
),
);
}),
);
}
Widget submitArea() {
return Card(
child: ListTile(
title: TextField(
controller: msgCon,
),
trailing: IconButton(
icon: Icon(Icons.send),
color: Colors.blue,
disabledColor: Colors.grey,
onPressed: (clientSocket != null) ? submitMessage : null,
),
),
);
}
void connectToServer() async {
print("Destination Address: ${ipCon.text}");
_storeServerIP();
Socket.connect(ipCon.text, port, timeout: Duration(seconds: 5))
.then((socket) {
setState(() {
clientSocket = socket;
});
showSnackBarWithKey(
"Connected to ${socket.remoteAddress.address}:${socket.remotePort}");
socket.listen(
(onData) {
print(String.fromCharCodes(onData).trim());
setState(() {
items.insert(
0,
MessageItem(clientSocket.remoteAddress.address,
String.fromCharCodes(onData).trim()));
});
},
onDone: onDone,
onError: onError,
);
}).catchError((e) {
showSnackBarWithKey(e.toString());
});
}
void onDone() {
showSnackBarWithKey("Connection has terminated.");
disconnectFromServer();
}
void onError(e) {
print("onError: $e");
showSnackBarWithKey(e.toString());
disconnectFromServer();
}
void disconnectFromServer() {
print("disconnectFromServer");
clientSocket.close();
setState(() {
clientSocket = null;
});
}
void sendMessage(String message) {
clientSocket.write("$message\n");
}
void _storeServerIP() async {
SharedPreferences sp = await SharedPreferences.getInstance();
sp.setString("serverIP", ipCon.text);
}
void _loadServerIP() async {
SharedPreferences sp = await SharedPreferences.getInstance();
setState(() {
ipCon.text = sp.getString("serverIP");
});
}
void submitMessage() {
if (msgCon.text.isEmpty) return;
setState(() {
items.insert(0, MessageItem(localIP, msgCon.text));
});
sendMessage(msgCon.text);
msgCon.clear();
}
showSnackBarWithKey(String message) {
scaffoldKey.currentState
..hideCurrentSnackBar()
..showSnackBar(SnackBar(
content: Text(message),
action: SnackBarAction(
label: 'Done',
onPressed: (){},
),
));
}
}
class MessageItem {
String owner;
String content;
MessageItem(this.owner, this.content);
}
▶ Go to Table of Contents | 강의 목차로 이동
※ This example is also available in the Flutter Code Examples app. | 본 예제는 Flutter Code Examples 앱에서도 제공됩니다.
'Tutorial > Flutter with App' 카테고리의 다른 글
Flutter Example - TCP Socket Server (0) | 2020.04.14 |
---|---|
Flutter Example - ListView | Handle Items | Change items (0) | 2020.03.24 |
Flutter Example - How to set the volume | volume plugin (2) | 2020.03.23 |
Flutter Example - How to add Admob Ad in Flutter App | firebase_admob plugin (0) | 2019.10.31 |
Flutter Example - Http Request | Get method | http plugin (0) | 2019.10.23 |
Comments