Last active
July 17, 2020 23:06
-
-
Save angelhdzdev/fdfff944e519a2c8c1cf47bf1a9e4bb2 to your computer and use it in GitHub Desktop.
Searching Users By Name/Email Demo
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
import 'package:flutter/material.dart'; | |
import 'dart:async'; | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
body: Center( | |
child: Home(), | |
), | |
), | |
); | |
} | |
} | |
class Usuario { | |
final String nombre; | |
final String email; | |
Usuario({this.nombre, this.email}); | |
factory Usuario.fromJson(Map json) => | |
Usuario(nombre: json['nombre'], email: json['email']); | |
} | |
class Home extends StatefulWidget { | |
@override | |
_HomeState createState() => _HomeState(); | |
} | |
const Map db = { | |
'usuarios': [ | |
{'nombre': 'Angel', 'email': '[email protected]'}, | |
{'nombre': 'Angel', 'email': '[email protected]'}, | |
{'nombre': 'Angela', 'email': '[email protected]'}, | |
{'nombre': 'Anna', 'email': '[email protected]'}, | |
{'nombre': 'Antonio', 'email': '[email protected]'}, | |
{'nombre': 'Claudia', 'email': '[email protected]'} | |
] | |
}; | |
enum Busqueda { porNombre, porEmail } | |
class _HomeState extends State<Home> { | |
StreamController<List<Usuario>> sc = StreamController(); | |
TextEditingController _nombreController = TextEditingController(); | |
Busqueda _busqueda = Busqueda.porNombre; | |
GlobalKey<FormState> _formKey = GlobalKey(); | |
@override | |
void initState() { | |
super.initState(); | |
} | |
Stream<List<Usuario>> get stream => sc.stream; | |
void dispose() { | |
super.dispose(); | |
sc.close(); | |
} | |
void _buscarUsuario(String texto, Busqueda busqueda) { | |
print('Buscando $texto $busqueda.'); | |
List<Usuario> resultados = []; | |
//Aqui se cambiaria la logica por una consulta a la DB | |
db['usuarios'].forEach((item) { | |
final Usuario usuario = Usuario.fromJson(item); | |
bool resultado; | |
switch (busqueda) { | |
case Busqueda.porEmail: | |
resultado = usuario.email == texto; | |
break; | |
case Busqueda.porNombre: | |
resultado = usuario.nombre.contains(texto); | |
break; | |
} | |
if (resultado) { | |
resultados.add(usuario); | |
} | |
}); | |
if (resultados.length > 0) { | |
print(resultados); | |
sc.sink.add(resultados); | |
} else { | |
sc.sink.addError('No hay resultados.'); | |
} | |
} | |
@override | |
Widget build(BuildContext context) { | |
return SingleChildScrollView( | |
child: Container( | |
padding: EdgeInsets.all(40.0), | |
child: Column(children: <Widget>[ | |
Text('Búsqueda', style: TextStyle(fontSize: 40.0, color: Colors.blue)), | |
StreamBuilder( | |
stream: stream, | |
builder: (context, AsyncSnapshot<List<Usuario>> snapshot) { | |
Widget widget = Text('No hay datos.'); | |
if (snapshot.hasData) { | |
final List<Usuario> usuarios = snapshot.data; | |
widget = Container( | |
padding: EdgeInsets.all(10.0), | |
height: 100.0, | |
child: ListView.builder( | |
shrinkWrap: true, | |
itemCount: usuarios.length, | |
itemBuilder: (context, index) { | |
final Usuario usuario = usuarios[index]; | |
return ListTile( | |
leading: Icon(Icons.person), | |
title: Text(usuario.nombre), | |
trailing: Text(usuario.email)); | |
})); | |
} | |
return widget; | |
}), | |
Form( | |
key: _formKey, | |
child: Column(children: [ | |
TextFormField( | |
controller: _nombreController, | |
validator: (String value) { | |
if (value.isEmpty) { | |
return 'No puede estar vacio.'; | |
} | |
return null; | |
}, | |
), | |
Row( | |
children: [ | |
Radio( | |
value: Busqueda.porNombre, | |
onChanged: (value) { | |
setState(() => _busqueda = value); | |
}, | |
groupValue: _busqueda, | |
), | |
Text('Buscar nombre') | |
]), | |
Row( | |
children: [ | |
Radio( | |
value: Busqueda.porEmail, | |
onChanged: (value) { | |
setState(() => _busqueda = value); | |
}, | |
groupValue: _busqueda, | |
), | |
Text('Buscar email') | |
]), | |
RaisedButton( | |
child: Text('Buscar'), | |
onPressed: () { | |
if (_formKey.currentState.validate()) { | |
_buscarUsuario(_nombreController.text, _busqueda); | |
} | |
}) | |
])) | |
]), | |
), | |
); | |
} | |
} | |
void main() { | |
runApp(MyApp()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment