-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
327 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
|
||
class Usuario { | ||
|
||
String nombre; | ||
String email; | ||
String uid; | ||
bool online; | ||
|
||
Usuario({ | ||
this.nombre, | ||
this.email, | ||
this.uid, | ||
this.online | ||
}); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,158 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:flutter/cupertino.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:chat_socket/widgets/chat_message.dart'; | ||
|
||
class ChatPage extends StatefulWidget { | ||
@override | ||
_ChatPageState createState() => _ChatPageState(); | ||
} | ||
|
||
class _ChatPageState extends State<ChatPage> with TickerProviderStateMixin { | ||
final _textController = new TextEditingController(); | ||
final _focusNode = new FocusNode(); | ||
bool _estaEscribiendo = false; | ||
|
||
List<ChatMessage> _messages = [ | ||
// ChatMessage(texto: 'Hola Mundo', uid: '123'), | ||
// ChatMessage(texto: 'Hola Mundo', uid: '456'), | ||
]; | ||
|
||
class ChatPage extends StatelessWidget { | ||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
body: Center( | ||
child: Text('EspPage'), | ||
appBar: AppBar( | ||
backgroundColor: Colors.white, | ||
title: Column( | ||
children: [ | ||
CircleAvatar( | ||
child: Text( | ||
'Te', | ||
style: TextStyle(fontSize: 12), | ||
), | ||
backgroundColor: Colors.blue[100], | ||
maxRadius: 14, | ||
), | ||
SizedBox( | ||
height: 3, | ||
), | ||
Text( | ||
'Nombre', | ||
style: TextStyle(color: Colors.black54, fontSize: 14), | ||
) | ||
], | ||
), | ||
centerTitle: true, | ||
elevation: 1, | ||
), | ||
body: Container( | ||
child: Column( | ||
children: [ | ||
Flexible( | ||
child: ListView.builder( | ||
physics: BouncingScrollPhysics(), | ||
itemCount: _messages.length, | ||
itemBuilder: (_, i) => _messages[i], | ||
reverse: true, | ||
), | ||
), | ||
Divider( | ||
height: 1, | ||
), | ||
Container( | ||
color: Colors.white, | ||
child: _inputChat(), | ||
) | ||
], | ||
), | ||
), | ||
); | ||
} | ||
|
||
Widget _inputChat() { | ||
return SafeArea( | ||
child: Container( | ||
margin: EdgeInsets.symmetric(horizontal: 8.0), | ||
child: Row( | ||
children: [ | ||
Flexible( | ||
child: TextField( | ||
controller: _textController, | ||
onSubmitted: _handleSubmit, | ||
onChanged: (texto) { | ||
setState(() { | ||
if (texto.trim().length > 0) { | ||
_estaEscribiendo = true; | ||
} else { | ||
_estaEscribiendo = false; | ||
} | ||
}); | ||
}, | ||
decoration: | ||
InputDecoration.collapsed(hintText: 'Enviar mensaje'), | ||
focusNode: _focusNode, | ||
), | ||
), | ||
Container( | ||
margin: EdgeInsets.symmetric(horizontal: 4.0), | ||
child: Platform.isIOS | ||
? CupertinoButton( | ||
child: Text('Enviar'), | ||
onPressed: _estaEscribiendo | ||
? () => _handleSubmit(_textController.text.trim()) | ||
: null, | ||
) | ||
: Container( | ||
margin: EdgeInsets.symmetric(horizontal: 4.0), | ||
child: IconTheme( | ||
data: IconThemeData(color: Colors.blue[400]), | ||
child: IconButton( | ||
highlightColor: Colors.transparent, | ||
splashColor: Colors.transparent, | ||
icon: Icon(Icons.send), | ||
onPressed: _estaEscribiendo | ||
? () => _handleSubmit(_textController.text.trim()) | ||
: null, | ||
), | ||
), | ||
), | ||
) | ||
], | ||
), | ||
), | ||
); | ||
} | ||
|
||
_handleSubmit(String texto) { | ||
if (texto.length == 0) return; | ||
|
||
print(texto); | ||
_textController.clear(); | ||
_focusNode.requestFocus(); | ||
|
||
final newMessage = new ChatMessage( | ||
texto: texto, | ||
uid: '123', | ||
animationController: AnimationController( | ||
vsync: this, duration: Duration(milliseconds: 400)), | ||
); | ||
|
||
_messages.insert(0, newMessage); | ||
newMessage.animationController.forward(); | ||
|
||
setState(() { | ||
_estaEscribiendo = false; | ||
}); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
//TODO Off del socket | ||
// Eliminamos los controladores de animación de todos los mensajes | ||
for (ChatMessage messages in _messages) { | ||
messages.animationController.dispose(); | ||
} | ||
super.dispose(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,94 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:pull_to_refresh/pull_to_refresh.dart'; | ||
import 'package:chat_socket/models/usuario.dart'; | ||
|
||
class UserPage extends StatefulWidget { | ||
@override | ||
_UserPageState createState() => _UserPageState(); | ||
} | ||
|
||
class _UserPageState extends State<UserPage> { | ||
RefreshController _refreshController = | ||
RefreshController(initialRefresh: false); | ||
|
||
final usuarios = [ | ||
Usuario(nombre: '1Nombre', email: 'test1@test.com', uid: '1', online: true), | ||
Usuario( | ||
nombre: '2Nombre', email: 'test2@test.com', uid: '1', online: false), | ||
Usuario( | ||
nombre: '3Nombre', email: 'test3@test.com', uid: '1', online: false), | ||
]; | ||
|
||
class UserPage extends StatelessWidget { | ||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
body: Center( | ||
child: Text('UserPage'), | ||
appBar: AppBar( | ||
title: Text( | ||
'Mi nombre', | ||
style: TextStyle(color: Colors.black54), | ||
), | ||
|
||
elevation: 1, | ||
backgroundColor: Colors.white, | ||
leading: IconButton( | ||
icon: Icon( | ||
Icons.exit_to_app, | ||
color: Colors.black54, | ||
), | ||
onPressed: () {}, | ||
), | ||
actions: [ | ||
Container( | ||
margin: EdgeInsets.only(right: 10), | ||
child: Icon( | ||
Icons.check_circle, | ||
color: Colors.blue[400], | ||
), | ||
//child: Icon(Icons.offline_bolt, color: Colors.red,), | ||
) | ||
], | ||
), | ||
body: SmartRefresher( | ||
controller: _refreshController, | ||
enablePullDown: true, | ||
onRefresh: _cargarUsuarios, | ||
header: WaterDropHeader( | ||
complete: Icon(Icons.check, color: Colors.blue[400]), | ||
waterDropColor: Colors.blue[400], | ||
), | ||
child: _listViewUsuarios(), | ||
), | ||
); | ||
} | ||
|
||
ListView _listViewUsuarios() { | ||
return ListView.separated( | ||
physics: BouncingScrollPhysics(), | ||
itemBuilder: (_, i) => _usuarioListTile(usuarios[i]), | ||
separatorBuilder: (_, i) => Divider(), | ||
itemCount: usuarios.length, | ||
); | ||
} | ||
} | ||
|
||
ListTile _usuarioListTile(Usuario usuario) { | ||
return ListTile( | ||
title: Text(usuario.nombre), | ||
subtitle: Text(usuario.email), | ||
leading: CircleAvatar( | ||
child: Text(usuario.nombre.substring(0, 2)), | ||
backgroundColor: Colors.blue[200], | ||
), | ||
trailing: Container( | ||
width: 10, | ||
height: 10, | ||
decoration: BoxDecoration( | ||
color: usuario.online ? Colors.green[300] : Colors.red, | ||
borderRadius: BorderRadius.circular(100)), | ||
), | ||
); | ||
} | ||
|
||
_cargarUsuarios() async { | ||
await Future.delayed(Duration(milliseconds: 1000)); | ||
_refreshController.refreshCompleted(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import 'package:flutter/material.dart'; | ||
|
||
|
||
class ChatMessage extends StatelessWidget { | ||
|
||
final String texto; | ||
final String uid; | ||
final AnimationController animationController; | ||
|
||
|
||
const ChatMessage({ | ||
@required this.texto, | ||
@required this.uid, | ||
@required this.animationController}); | ||
|
||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return FadeTransition( | ||
opacity: animationController, | ||
child: SizeTransition( | ||
sizeFactor: CurvedAnimation(parent: animationController, curve: Curves.easeOut), | ||
child: Container( | ||
child: this.uid == '123' ? _myMessage() : _notMyMessage(), | ||
|
||
), | ||
), | ||
); | ||
} | ||
|
||
Widget _myMessage(){ | ||
|
||
return Align( | ||
alignment: Alignment.centerRight, | ||
child: Container( | ||
margin: EdgeInsets.only(bottom: 5, left: 50, right: 5), | ||
padding: EdgeInsets.all(8.0), | ||
child: Text(this.texto, style: TextStyle(color: Colors.white),), | ||
decoration: BoxDecoration( | ||
color: Color(0xff4d9ef6), | ||
borderRadius: BorderRadius.circular(20) | ||
), | ||
), | ||
); | ||
|
||
} | ||
|
||
Widget _notMyMessage(){ | ||
|
||
return Align( | ||
alignment: Alignment.centerLeft, | ||
child: Container( | ||
margin: EdgeInsets.only(bottom: 5, right: 50, left: 5), | ||
padding: EdgeInsets.all(8.0), | ||
child: Text(this.texto, style: TextStyle(color: Colors.black87),), | ||
decoration: BoxDecoration( | ||
color: Color(0xffe4e5e8), | ||
borderRadius: BorderRadius.circular(20) | ||
), | ||
), | ||
); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters