Hooked up the new task screen with the bloc to allow for the creatin of a new task
This commit is contained in:
parent
41298ae655
commit
4e355d7817
2 changed files with 84 additions and 37 deletions
|
|
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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,13 +26,23 @@ 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(
|
||||||
|
stream: bloc.userModelStream,
|
||||||
|
builder: (BuildContext context, AsyncSnapshot<UserModel> snap) {
|
||||||
|
List<String> events = [];
|
||||||
|
|
||||||
|
if (snap.hasData) {
|
||||||
|
events = snap.data.events;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Padding(
|
||||||
padding: const EdgeInsets.only(top: 15.0, left: 20.0, right: 20.0),
|
padding: const EdgeInsets.only(top: 15.0, left: 20.0, right: 20.0),
|
||||||
child: Column(
|
child: Column(
|
||||||
children: <Widget>[
|
children: <Widget>[
|
||||||
|
|
@ -42,7 +53,7 @@ class _NewTaskScreenState extends State<NewTaskScreen> {
|
||||||
SizedBox(
|
SizedBox(
|
||||||
height: 15,
|
height: 15,
|
||||||
),
|
),
|
||||||
buildDropdownSection(),
|
buildDropdownSection(events),
|
||||||
SizedBox(
|
SizedBox(
|
||||||
height: 15,
|
height: 15,
|
||||||
),
|
),
|
||||||
|
|
@ -54,6 +65,7 @@ class _NewTaskScreenState extends State<NewTaskScreen> {
|
||||||
height: 40,
|
height: 40,
|
||||||
width: 330,
|
width: 330,
|
||||||
radius: 8,
|
radius: 8,
|
||||||
|
onTap: () => onSubmit(context, bloc),
|
||||||
child: Text(
|
child: Text(
|
||||||
'Submit',
|
'Submit',
|
||||||
style: kButtonStyle,
|
style: kButtonStyle,
|
||||||
|
|
@ -61,11 +73,13 @@ class _NewTaskScreenState extends State<NewTaskScreen> {
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue