Skip to content

Commit

Permalink
reactions OK
Browse files Browse the repository at this point in the history
  • Loading branch information
C4st3ll4n committed Oct 16, 2020
1 parent 7157aa2 commit af5b531
Show file tree
Hide file tree
Showing 4 changed files with 254 additions and 99 deletions.
200 changes: 110 additions & 90 deletions lib/screens/list_screen.dart
Original file line number Diff line number Diff line change
@@ -1,102 +1,122 @@
import 'package:flutter/material.dart';
import 'package:mobx/mobx.dart';
import 'package:todomobx/store/list_store.dart';
import 'package:todomobx/widgets/custom_icon_button.dart';
import 'package:todomobx/widgets/custom_text_field.dart';

import 'login_screen.dart';

class ListScreen extends StatefulWidget {

@override
_ListScreenState createState() => _ListScreenState();
@override
_ListScreenState createState() => _ListScreenState();
}

class _ListScreenState extends State<ListScreen> {

@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
resizeToAvoidBottomInset: false,
body: Container(
margin: const EdgeInsets.fromLTRB(32, 0, 32, 32),
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.symmetric(vertical: 16, horizontal: 2),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
'Tarefas',
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.w900,
fontSize: 32
),
),
IconButton(
icon: Icon(Icons.exit_to_app),
color: Colors.white,
onPressed: (){
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (context)=>LoginScreen())
);
},
),
],
),
),
Expanded(
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
),
elevation: 16,
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
children: <Widget>[
CustomTextField(
hint: 'Tarefa',
onChanged: (todo){

},
suffix: CustomIconButton(
radius: 32,
iconData: Icons.add,
onTap: (){

},
),
),
const SizedBox(height: 8,),
Expanded(
child: ListView.separated(
itemCount: 10,
itemBuilder: (_, index){
return ListTile(
title: Text(
'Item $index',
),
onTap: (){

},
);
},
separatorBuilder: (_, __){
return Divider();
},
),
),
],
),
),
),
),
],
),
),
),
);

ListStore _listStore = ListStore();

ReactionDisposer disposer;

@override
void didChangeDependencies() {
disposer = reaction((_) => _listStore.hasUser, (user) {
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (context)=>LoginScreen())
);
});
super.didChangeDependencies();
}

@override
void dispose() {
disposer();
super.dispose();
}

@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
resizeToAvoidBottomInset: false,
body: Container(
margin: const EdgeInsets.fromLTRB(32, 0, 32, 32),
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.symmetric(
vertical: 16, horizontal: 2),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
'Tarefas',
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.w900,
fontSize: 32
),
),
IconButton(
icon: Icon(Icons.exit_to_app),
color: Colors.white,
onPressed: _listStore.logout,
),
],
),
),
Expanded(
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
),
elevation: 16,
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
children: <Widget>[
CustomTextField(
enabled: !_listStore.loading,
hint: 'Tarefa',
onChanged: (todo) {

},
suffix: CustomIconButton(
radius: 32,
iconData: Icons.add,
onTap: () {

},
),
),
const SizedBox(height: 8,),
Expanded(
child: ListView.separated(
itemCount: 10,
itemBuilder: (_, index) {
return ListTile(
title: Text(
'Item $index',
),
onTap: !_listStore.loading?null:() {

},
);
},
separatorBuilder: (_, __) {
return Divider();
},
),
),
],
),
),
),
),
],
),
),
),
);
}
}
25 changes: 16 additions & 9 deletions lib/screens/login_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,33 @@ class LoginScreen extends StatefulWidget {

class _LoginScreenState extends State<LoginScreen> {
LoginStore _loginStore = LoginStore();

ReactionDisposer reactionDisposer;
@override
void didChangeDependencies() {

super.didChangeDependencies();

//FORMA 1
/*
autorun((r){
print("Tem gente ? ${_loginStore.isLogged}");
//FORMA 1
/*if(_loginStore.isLogged){
if(_loginStore.isLogged){
Navigator.pushReplacement(context, MaterialPageRoute(builder: (_)=>ListScreen()));
}*/

reaction((_)=>_loginStore.isLogged, (logged){
logged?Navigator.pushReplacement(context, MaterialPageRoute(builder: (_)=>ListScreen())):null;
});

}
});
*/

reactionDisposer = reaction((_)=>_loginStore.isLogged, (logged){
logged?Navigator.pushReplacement(context, MaterialPageRoute(builder: (_)=>ListScreen())):null;
});
}

@override
void dispose() {
reactionDisposer();
super.dispose();
}

@override
Expand Down
35 changes: 35 additions & 0 deletions lib/store/list_store.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import 'package:flutter/foundation.dart';
import 'package:mobx/mobx.dart';

part 'list_store.g.dart';

class ListStore = _ListStore with _$ListStore;

abstract class _ListStore with Store {

@observable
String _newTodo = "";

@observable
bool loading = false;

@observable
bool hasUser = true;

@action
void updateTodo(String todo) => _newTodo = todo;

@action
Future<void> logout() async{
loading = true;
await Future.delayed(Duration(seconds: 2));
hasUser = false;
loading = false;
}

@computed
Function get logoutPresses => loading?logout:null;

ObserverList todoList;

}
93 changes: 93 additions & 0 deletions lib/store/list_store.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit af5b531

Please sign in to comment.