Skip to content

Instantly share code, notes, and snippets.

@doyle-flutter
Created February 16, 2021 09:12
Show Gist options
  • Save doyle-flutter/ed89ea2f2158cb6dfcc8aa15c7c31cdc to your computer and use it in GitHub Desktop.
Save doyle-flutter/ed89ea2f2158cb6dfcc8aa15c7c31cdc to your computer and use it in GitHub Desktop.
// Android HTTP 설정
// - Manifest :
// (1) <uses-permission android:name="android.permission.INTERNET" />
// (2) <application
// android:label="exmaplee"
// android:icon="@mipmap/ic_launcher"
// android:usesCleartextTraffic="true">
// IOS HTTP 설정
// - Info.plist
// <key>NSAppTransportSecurity</key>
// <dict>
// <key>NSAllowsArbitraryLoads</key>
// <true/>
// </dict>
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:socket_io_client/socket_io_client.dart' as IO;
void main() => runApp(MaterialApp(home: Login(),));
// ignore: must_be_immutable
class Login extends StatelessWidget {
TextEditingController controller = new TextEditingController(text: "");
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('로그인 '),),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.login),
onPressed: () async{
if(this.controller.text.isEmpty) return null;
http.Response res = await http.get('http://127.0.0.1:3003/login?id=${this.controller.text}');
bool check = json.decode(res.body);
if(!check){
await showDialog<void>(
context: context,
builder: (BuildContext context) => AlertDialog(
title: Text("로그인 실패"),
actions: [TextButton(child: Text("닫기"),onPressed: () => Navigator.of(context).pop<void>(),)],
),
);
this.controller.text = "";
return;
}
await Navigator.of(context).push(
MaterialPageRoute<void>(builder: (BuildContext context) => MyApp(id: controller.text)),
);
this.controller.text = "";
return;
},
),
body: Center(
child: Container(
margin: EdgeInsets.all(20.0),
child: TextField(controller: controller,),
),
),
);
}
}
// ignore: must_be_immutable
class MyApp extends StatefulWidget {
String id;
MyApp({@required this.id});
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
List fds;
String serverURL = "http://127.0.0.1:3003";
IO.Socket socket = IO.io("http://127.0.0.1/",<String, dynamic>{
'transports': ['websocket'],
'autoConnect': false,
});
@override
void initState() {
Future.microtask(() async{
socket..connect()..on('userEvent', (data){
Future.microtask(() async{
http.Response res = await http.get("$serverURL/fds?id=${widget.id}");
fds = json.decode(res.body);
if(!mounted) return;
setState(() {});
});
});
http.Response res = await http.get("$serverURL/fds?id=${widget.id}");
fds = json.decode(res.body);
if(!mounted) return;
setState(() {});
});
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("안녕하세요 ${widget.id} 님"),),
body: this.fds == null
? Center(child: Text("로딩중"),)
: ListView.builder(
itemCount: this.fds.length,
itemBuilder: (BuildContext context, int index){
return ListTile(
leading: CircleAvatar(
child: Container(
decoration: BoxDecoration(
color: this.fds[index]['on'] ? Colors.blue : Colors.grey
),
),
),
title: Text(this.fds[index]['name'].toString()),
subtitle: Text("상태명"),
trailing: Icon(Icons.call),
);
}
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.send),
onPressed: () async{
http.Response res = await http.get('$serverURL/logout?id=${widget.id}');
bool check = json.decode(res.body);
if(!check) return print("로그아웃 실패");
socket.emit("userLogout", "data");
return Navigator.of(context).pop<void>();
},
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment