Hooked up the new task screen with the bloc to allow for the creatin of a new task

This commit is contained in:
Mariano Uvalle 2019-04-01 01:33:12 -06:00
parent 41298ae655
commit 4e355d7817
2 changed files with 84 additions and 37 deletions

View file

@ -3,16 +3,20 @@ import 'dart:async';
import 'package:rxdart/rxdart.dart'; import 'package:rxdart/rxdart.dart';
import '../models/task_model.dart'; import '../models/task_model.dart';
import '../models/user_model.dart';
import '../resources/authService.dart'; import '../resources/authService.dart';
import '../resources/firestore_provider.dart'; import '../resources/firestore_provider.dart';
class NewTaskBloc { class NewTaskBloc {
final AuthService _auth = authService; final AuthService _auth = authService;
FirebaseUser currentUser; final FirestoreProvider _firestore = firestoreProvider;
final _user = BehaviorSubject<UserModel>();
String text; String text = '';
TaskPriority priority; TaskPriority priority = TaskPriority.high;
String event; String event = '';
Observable<UserModel> get userModelStream => _user.stream;
NewTaskBloc() { NewTaskBloc() {
setCurrentUser(); setCurrentUser();
@ -26,8 +30,24 @@ class NewTaskBloc {
priority = newPriority; priority = newPriority;
} }
void setEvent(String newEvent) {
event = newEvent;
}
Future<void> submit() {
final newTask = TaskModel(
text: text,
priority: priority,
event: event,
ownerUsername: _user.value.username,
done: false,
);
return _firestore.addTask(newTask);
}
Future<void> setCurrentUser() async { Future<void> setCurrentUser() async {
final user = await _auth.currentUser; final user = await _auth.currentUser;
currentUser = user; final userModel = await _firestore.getUser(username: user.email);
_user.add(userModel);
} }
} }

View file

@ -1,6 +1,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import '../blocs/new_task_bloc.dart'; import '../blocs/new_task_bloc.dart';
import '../models/user_model.dart';
import '../widgets/custom_app_bar.dart'; import '../widgets/custom_app_bar.dart';
import '../widgets/custom_dropdown.dart'; import '../widgets/custom_dropdown.dart';
import '../widgets/big_text_input.dart'; import '../widgets/big_text_input.dart';
@ -25,47 +26,60 @@ class _NewTaskScreenState extends State<NewTaskScreen> {
); );
final NewTaskBloc bloc = NewTaskBloc(); final NewTaskBloc bloc = NewTaskBloc();
String dropdownValue;
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
appBar: CustomAppBar( appBar: CustomAppBar(
title: 'Add task', title: 'Add task',
), ),
body: Padding( body: StreamBuilder(
padding: const EdgeInsets.only(top: 15.0, left: 20.0, right: 20.0), stream: bloc.userModelStream,
child: Column( builder: (BuildContext context, AsyncSnapshot<UserModel> snap) {
children: <Widget>[ List<String> events = [];
BigTextInput(
height: 95, if (snap.hasData) {
onChanged: bloc.setText, events = snap.data.events;
}
return Padding(
padding: const EdgeInsets.only(top: 15.0, left: 20.0, right: 20.0),
child: Column(
children: <Widget>[
BigTextInput(
height: 95,
onChanged: bloc.setText,
),
SizedBox(
height: 15,
),
buildDropdownSection(events),
SizedBox(
height: 15,
),
buildPrioritySelectorSection(bloc),
SizedBox(
height: 20,
),
GradientTouchableContainer(
height: 40,
width: 330,
radius: 8,
onTap: () => onSubmit(context, bloc),
child: Text(
'Submit',
style: kButtonStyle,
),
),
],
), ),
SizedBox( );
height: 15, },
),
buildDropdownSection(),
SizedBox(
height: 15,
),
buildPrioritySelectorSection(bloc),
SizedBox(
height: 20,
),
GradientTouchableContainer(
height: 40,
width: 330,
radius: 8,
child: Text(
'Submit',
style: kButtonStyle,
),
),
],
),
), ),
); );
} }
Widget buildDropdownSection() { Widget buildDropdownSection(List<String> events) {
return Row( return Row(
children: <Widget>[ children: <Widget>[
Text( Text(
@ -74,10 +88,11 @@ class _NewTaskScreenState extends State<NewTaskScreen> {
), ),
Spacer(), Spacer(),
CustomDropdownButton( CustomDropdownButton(
value: dropdownValue,
onChanged: (String value) => onDropdownChanged(bloc, value),
width: 230, width: 230,
hint: Text('Event'), hint: Text('Event'),
onChanged: (item) {}, items: events.map((String name) {
items: ['Math', 'Lenguajes', 'Redes'].map((String name) {
return CustomDropdownMenuItem( return CustomDropdownMenuItem(
value: name, value: name,
child: Text( child: Text(
@ -106,4 +121,16 @@ class _NewTaskScreenState extends State<NewTaskScreen> {
], ],
); );
} }
void onDropdownChanged(NewTaskBloc bloc, String newValue) {
bloc.setEvent(newValue);
setState(() {
dropdownValue = newValue;
});
}
void onSubmit(BuildContext context, NewTaskBloc bloc) async {
await bloc.submit();
Navigator.of(context).pop();
}
} }