Skip to content

Commit

Permalink
36 - Adicionando os Anuncios na Tela Principal
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhciolfi committed Mar 14, 2020
1 parent 60504e2 commit 88de85a
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 113 deletions.
8 changes: 8 additions & 0 deletions blocs/home_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import 'package:rxdart/rxdart.dart';
import 'package:xlo/models/ad.dart';

class HomeBloc {

final BehaviorSubject<String> _searchController = BehaviorSubject<String>();
final BehaviorSubject<List<Ad>> _adController = BehaviorSubject<List<Ad>>.seeded([]);

Stream<String> get outSearch => _searchController.stream;
Stream<List<Ad>> get outAd => _adController.stream;

void addAd(Ad ad){
_adController.add(_adController.value..add(ad));
}

void setSearch(String search){
_searchController.add(search);
}

void dispose(){
_searchController.close();
_adController.close();
}

}
249 changes: 136 additions & 113 deletions screens/create/create_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:provider/provider.dart';
import 'package:xlo/api/api_postalcode.dart';
import 'package:xlo/blocs/create_bloc.dart';
import 'package:xlo/blocs/drawer_bloc.dart';
import 'package:xlo/blocs/home_bloc.dart';
import 'package:xlo/common/cep_field.dart';
import 'package:xlo/common/custom_drawer/custom_drawer.dart';
import 'package:xlo/models/ad.dart';
Expand Down Expand Up @@ -46,127 +47,149 @@ class _CreateScreenState extends State<CreateScreen> {
drawer: CustomDrawer(),
body: Form(
key: _formKey,
child: ListView(
children: <Widget>[
ImagesField(
onSaved: (images){
ad.images = images;
},
initialValue: [],
),
TextFormField(
decoration: InputDecoration(
labelText: 'Título *',
labelStyle: TextStyle(
fontWeight: FontWeight.w800,
color: Colors.grey,
fontSize: 18,
child: StreamBuilder<CreateState>(
stream: _createBloc.outState,
builder: (context, snapshot) {
if(snapshot.data == CreateState.LOADING){
return Center(
child: Container(
width: 300.0,
height: 300.0,
alignment: Alignment.center,
child: CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(
Colors.pink,
),
strokeWidth: 5.0,
),
),
contentPadding: const EdgeInsets.fromLTRB(16, 10, 12, 10),
),
validator: (text) {
if (text.isEmpty) return 'Campo obrigatório';
return null;
},
onSaved: (t) {
ad.title = t;
},
),
TextFormField(
maxLines: null,
decoration: InputDecoration(
labelText: 'Descrição *',
labelStyle: TextStyle(
fontWeight: FontWeight.w800,
color: Colors.grey,
fontSize: 18,
);
}
return ListView(
children: <Widget>[
ImagesField(
onSaved: (images){
ad.images = images;
},
initialValue: [],
),
contentPadding: const EdgeInsets.fromLTRB(16, 10, 12, 10),
),
validator: (text) {
if (text.trim().isEmpty) return 'Campo obrigatório';
if (text.trim().length < 10)
return 'Descrição muito curta';
return null;
},
onSaved: (d) {
ad.description = d;
},
),
CepField(
decoration: InputDecoration(
labelText: 'CEP *',
labelStyle: TextStyle(
fontWeight: FontWeight.w800,
color: Colors.grey,
fontSize: 18,
TextFormField(
decoration: InputDecoration(
labelText: 'Título *',
labelStyle: TextStyle(
fontWeight: FontWeight.w800,
color: Colors.grey,
fontSize: 18,
),
contentPadding: const EdgeInsets.fromLTRB(16, 10, 12, 10),
),
validator: (text) {
if (text.isEmpty) return 'Campo obrigatório';
return null;
},
onSaved: (t) {
ad.title = t;
},
),
contentPadding: const EdgeInsets.fromLTRB(16, 10, 12, 10),
),
onSaved: (a) {
ad.address = a;
},
),
TextFormField(
decoration: InputDecoration(
labelText: 'Preço *',
labelStyle: TextStyle(
fontWeight: FontWeight.w800,
color: Colors.grey,
fontSize: 18,
TextFormField(
maxLines: null,
decoration: InputDecoration(
labelText: 'Descrição *',
labelStyle: TextStyle(
fontWeight: FontWeight.w800,
color: Colors.grey,
fontSize: 18,
),
contentPadding: const EdgeInsets.fromLTRB(16, 10, 12, 10),
),
validator: (text) {
if (text.trim().isEmpty) return 'Campo obrigatório';
if (text.trim().length < 10)
return 'Descrição muito curta';
return null;
},
onSaved: (d) {
ad.description = d;
},
),
contentPadding: const EdgeInsets.fromLTRB(16, 10, 12, 10),
),
keyboardType: const TextInputType.numberWithOptions(
decimal: true,
signed: false,
),
inputFormatters: [
WhitelistingTextInputFormatter.digitsOnly,
RealInputFormatter(centavos: true),
],
validator: (text) {
if (text.isEmpty) return 'Campo obrigatório';
if (int.tryParse(getSanitizedText(text)) == null)
return 'Utilize valores válidos';
return null;
},
onSaved: (p) {
ad.price = int.parse(getSanitizedText(p)) / 100;
},
),
HidePhoneWidget(
onSaved: (h){
ad.hidePhone = h;
},
initialValue: false,
),
Container(
height: 50,
child: RaisedButton(
color: Colors.pink,
child: Text(
'Enviar',
style: TextStyle(
color: Colors.white,
fontSize: 18,
fontWeight: FontWeight.w500,
CepField(
decoration: InputDecoration(
labelText: 'CEP *',
labelStyle: TextStyle(
fontWeight: FontWeight.w800,
color: Colors.grey,
fontSize: 18,
),
contentPadding: const EdgeInsets.fromLTRB(16, 10, 12, 10),
),
onSaved: (a) {
ad.address = a;
},
),
onPressed: () async {
if(_formKey.currentState.validate()){
_formKey.currentState.save();
TextFormField(
decoration: InputDecoration(
labelText: 'Preço *',
labelStyle: TextStyle(
fontWeight: FontWeight.w800,
color: Colors.grey,
fontSize: 18,
),
contentPadding: const EdgeInsets.fromLTRB(16, 10, 12, 10),
),
keyboardType: const TextInputType.numberWithOptions(
decimal: true,
signed: false,
),
inputFormatters: [
WhitelistingTextInputFormatter.digitsOnly,
RealInputFormatter(centavos: true),
],
validator: (text) {
if (text.isEmpty) return 'Campo obrigatório';
if (int.tryParse(getSanitizedText(text)) == null)
return 'Utilize valores válidos';
return null;
},
onSaved: (p) {
ad.price = int.parse(getSanitizedText(p)) / 100;
},
),
HidePhoneWidget(
onSaved: (h){
ad.hidePhone = h;
},
initialValue: false,
),
Container(
height: 50,
child: RaisedButton(
color: Colors.pink,
child: Text(
'Enviar',
style: TextStyle(
color: Colors.white,
fontSize: 18,
fontWeight: FontWeight.w500,
),
),
onPressed: () async {
if(_formKey.currentState.validate()){
_formKey.currentState.save();

Provider.of<HomeBloc>(context).addAd(ad);

final bool success = await _createBloc.saveAd(ad);
final bool success = await _createBloc.saveAd(ad);

if(success){
Provider.of<DrawerBloc>(context).setPage(0);
}
}
},
),
)
],
if(success){
Provider.of<DrawerBloc>(context).setPage(0);
}
}
},
),
)
],
);
}
),
),
);
Expand Down
16 changes: 16 additions & 0 deletions screens/home/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:xlo/blocs/home_bloc.dart';
import 'package:xlo/common/custom_drawer/custom_drawer.dart';
import 'package:xlo/models/ad.dart';
import 'package:xlo/screens/home/widgets/search_dialog.dart';
import 'package:xlo/screens/home/widgets/top_bar.dart';

Expand Down Expand Up @@ -84,6 +85,21 @@ class _HomeScreenState extends State<HomeScreen> {
body: Column(
children: <Widget>[
TopBar(),
Expanded(
child: StreamBuilder<List<Ad>>(
stream: _homeBloc.outAd,
builder: (context, snapshot){
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index){
return ListTile(
title: Text(snapshot.data[index].title),
);
}
);
},
),
)
],
),
);
Expand Down

0 comments on commit 88de85a

Please sign in to comment.