Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:PhotoReport/blocs/categoria_bloc.dart';
- import 'package:PhotoReport/helpers/categoria_helper.dart';
- import 'package:PhotoReport/helpers/obra_helper.dart';
- import 'package:PhotoReport/widgets/CustomShowDialog.dart';
- import 'package:flutter/material.dart';
- import 'add_categoria.dart';
- class ListaCategoriasPage extends StatefulWidget {
- final Obra obra;
- ListaCategoriasPage(this.obra);
- @override
- _ListaCategoriasPageState createState() => _ListaCategoriasPageState();
- }
- class _ListaCategoriasPageState extends State<ListaCategoriasPage> {
- Obra obra;
- CategoriaBloc _bloc;
- @override
- void initState() {
- super.initState();
- obra = widget.obra;
- _bloc = CategoriaBloc(buildingId: obra.id);
- _bloc.outState.listen((state) {
- switch (state) {
- case CategoriaState.LOADING:
- _buildLoadingWidget();
- break;
- case CategoriaState.SUCCESS:
- case CategoriaState.IDLE:
- case CategoriaState.FAIL:
- default:
- _buildingBody();
- break;
- }
- });
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: Text("Categorias"),
- ),
- body: StreamBuilder<CategoriaState>(
- stream: _bloc.outState,
- builder: (context, snapshot) {
- switch (snapshot.data) {
- case CategoriaState.LOADING:
- return _buildLoadingWidget();
- break;
- default:
- return _buildingBody();
- break;
- }
- },
- ),
- floatingActionButton: FloatingActionButton(
- heroTag: "ListaCategorias",
- onPressed: () async {
- var resultado = await Navigator.of(context).push(MaterialPageRoute(
- builder: (BuildContext context) => AddCategoriaPage(
- "add",
- obra: obra,
- ),
- fullscreenDialog: true));
- if (resultado != null) {
- setState(() {
- _bloc.obterCategorias(obra.id);
- });
- }
- },
- child: Icon(Icons.add),
- ),
- );
- }
- Widget _buildingBody() {
- return ListView.builder(
- itemCount: _bloc.categorias.length,
- itemBuilder: (context, index) {
- return _bloc.categorias.length > 0
- ? _buildCategoriaWidget(index)
- : Container();
- },
- );
- }
- Widget _buildLoadingWidget() {
- return Center(
- child: Column(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- CircularProgressIndicator(),
- SizedBox(
- height: 10.0,
- ),
- Text("Carregando...")
- ],
- ));
- }
- Widget _buildErrorWidget(String error) {
- return Center(
- child: Column(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- Text("Ocorreu um erro: $error"),
- ],
- ));
- }
- Widget _buildCategoriaWidget(int index) {
- return GestureDetector(
- child: Card(
- color: Theme.of(context).cardColor,
- child: Padding(
- padding: EdgeInsets.all(10.0),
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: <Widget>[
- Text(_bloc.categorias[index].nome,
- style: Theme.of(context)
- .textTheme
- .title
- .copyWith(color: Colors.black)),
- Text(
- _bloc.categorias[index].qtdImagens > 1
- ? "${_bloc.categorias[index].qtdImagens} imagens"
- : "${_bloc.categorias[index].qtdImagens} imagem",
- style: Theme.of(context)
- .textTheme
- .subtitle
- .copyWith(color: Colors.black))
- ],
- ),
- ),
- ),
- onTap: () {
- setState(() {
- _settingModalBottomSheet(context, _bloc.categorias[index]);
- });
- },
- );
- }
- void _settingModalBottomSheet(BuildContext context, Categoria categoria) {
- showModalBottomSheet(
- context: context,
- builder: (BuildContext bc) {
- return Container(
- child: new Wrap(
- children: <Widget>[
- new ListTile(
- leading: new Icon(Icons.edit),
- title: new Text('Editar'),
- onTap: () async {
- setState(() async {
- await Navigator.of(context).push(MaterialPageRoute(
- builder: (context) => AddCategoriaPage("edit",
- obra: obra, categoria: categoria)));
- Navigator.of(context).pop();
- setState(() {
- _bloc.getCategorias(obra.id);
- });
- });
- }),
- new ListTile(
- leading: new Icon(Icons.delete),
- title: new Text('Deletar'),
- onTap: () {
- Navigator.of(context).pop();
- showDialog(
- context: context,
- barrierDismissible: false,
- builder: (BuildContext context) {
- return WillPopScope(
- onWillPop: () => Future(() => false),
- child: CustomAlertDialog(
- title: Text("Atenção!"),
- content: Text(
- "Deseja excluir a categoria selecionada?"),
- actions: <Widget>[
- FlatButton(
- child: Text("Sim"),
- onPressed: () {
- setState(() async {
- Navigator.of(context).pop();
- setState(() {
- _bloc.deletar(categoria);
- });
- //await categoriaBloc.getCategorias(obra.id);
- });
- },
- ),
- FlatButton(
- child: Text("Não"),
- onPressed: () {
- Navigator.of(context).pop();
- },
- ),
- ],
- ),
- );
- });
- },
- ),
- ],
- ),
- );
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement