with_bloc 1.0.2 with_bloc: ^1.0.2 copied to clipboard
A widget to create and manage BLoCs implemented on top of ValueNotifier, such as StateQueue.
import 'package:flutter/material.dart';
import 'package:state_queue/state_queue.dart';
import 'package:with_bloc/src/with_bloc.dart';
@immutable
abstract class LoginState {}
class LoggedOutState implements LoginState {}
class LoginInProgressState implements LoginState {}
class LoginErrorState implements LoginState {
LoginErrorState(this.message);
final String message;
}
class LoggedInState implements LoginState {
LoggedInState(this.username);
final String username;
}
class LoginBloc extends StateQueue<LoginState> {
LoginBloc() : super(LoggedOutState());
void login(String username, String password) {
run((state) async* {
// yield n;
});
}
}
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
WithBloc<LoginBloc, LoginState>(
createBloc: (context) => LoginBloc(),
builder: (context, bloc, state, _) {
if (state is LoggedInState) {
// render logged in app
return Center(
child: Text(
'Hello ${state.username}',
),
);
}
if (state is LoggedInState) {
return Center(
child: CircularProgressIndicator(),
);
}
return _LoginScreen(
onLoginTap: bloc.login,
errorMessage: state is LoginErrorState ? state.message : null,
);
},
);
return Container();
}
}
class _LoginScreen extends StatelessWidget {
const _LoginScreen({
Key key,
@required this.onLoginTap,
this.errorMessage,
}) : super(key: key);
final void Function(String username, String password) onLoginTap;
final String errorMessage;
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
TextField(),
TextField(),
Button(),
],
);
}
}