Skip to content

Instantly share code, notes, and snippets.

@waystilos
Created October 11, 2018 16:53
Show Gist options
  • Save waystilos/e3b307eb6d6984f4fdee08c8e7b166ee to your computer and use it in GitHub Desktop.
Save waystilos/e3b307eb6d6984f4fdee08c8e7b166ee to your computer and use it in GitHub Desktop.
Issue getting flutter app to load
// bloc.dart
import 'dart:async';
import 'validators.dart';
class Bloc extends Validators {
final _email = new StreamController<String>();
final _password = new StreamController<String>();
// Add data to stream
Stream<String> get email => _email.stream.transform(validateEmail);
Stream<String> get password => _password.stream.transform(validatePassword);
// Change Data
Function(String) get changeEmail => _email.sink.add;
Function(String) get changePassword => _password.sink.add;
dispose() {
_email.close();
_password.close();
}
}
final bloc = Bloc();
// login_screen.dart
import 'package:flutter/material.dart';
import '../blocs/bloc.dart';
class LoginScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.all(20.0),
child: Column(
children: [
emailField(),
passwordField(),
Container(
margin: EdgeInsets.only(top: 25.0),
),
raisedButton()
],
));
}
Widget emailField() {
return StreamBuilder(
stream: bloc.email,
builder: (context, snapshot) {
return TextField(
onChanged: bloc.changeEmail,
keyboardType: TextInputType.emailAddress,
decoration: InputDecoration(
hintText: '[email protected]',
labelText: 'Enter your email!',
errorText: snapshot.error),
);
});
}
Widget passwordField() {
return TextField(
obscureText: true,
decoration: InputDecoration(hintText: 'password', labelText: 'password'),
);
}
Widget raisedButton() {
return RaisedButton(
child: Text('Login'),
color: Colors.blue,
onPressed: () {},
);
}
}
// validation.dart
import 'dart:async';
class Validators {
final validateEmail =
StreamTransformer<String, String>.fromHandlers(handleData: (email, sink) {
if (email.contains('@')) {
sink.add(email);
} else {
sink.addError('Please enter valid email');
}
});
final validatePassword = StreamTransformer<String, String>.fromHandlers(
handleData: (password, sink) {
if (password.length > 4) {
sink.add(password);
} else {
sink.addError('Please enter the right number of characters');
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment