Patron bloc integrado

This commit is contained in:
2024-04-29 11:17:27 +02:00
parent 4f7bb569d8
commit dd5be31611
9 changed files with 234 additions and 173 deletions

BIN
lib/assets/logo-fondo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 91 KiB

View File

@ -0,0 +1,16 @@
import 'dart:async';
import 'package:flutter\_bloc/flutter\_bloc.dart';
import 'package:flutter_project/contenedores_event.dart';
import 'package:flutter_project/contenedores_state.dart';
/*
class ContenedoresBloc extends Bloc<ContenedoresEvent, ContenedoresState> {
ContenedoresBloc() : super(const ContenedoresState()) {
on<SelectContenedor>((event, emit) {
emit(ContenedoresState(currentContenedor: event.contenedor));
});
}
}*/

View File

@ -0,0 +1,12 @@
import 'package:flutter_bloc/flutter_bloc.dart';
// Define el estado
class MyState {
}
// Define el bloc
class MyBloc extends Cubit<MyState> {
MyBloc() : super(MyState());
}

View File

@ -0,0 +1,8 @@
// This class represents the state of the ContenedoresBloc
/*import 'package:flutter_project/contenedores_event.dart';
class ContenedoresState {
final Contenedor? currentContenedor;
const ContenedoresState({this.currentContenedor});
}*/

View File

@ -1,31 +1,15 @@
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:url_launcher/url_launcher.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_project/contenedores_event.dart';
import 'my_home_page.dart';
// Definir eventos
enum Contenedor { noticias, pregon, embalses, tiempo }
// Definir estado
class ContenedoresState {
final Contenedor? currentContenedor;
ContenedoresState({this.currentContenedor});
}
// Definir Bloc
class ContenedoresBloc {
final _stateController = StreamController<ContenedoresState>.broadcast();
Stream<ContenedoresState> get state => _stateController.stream;
void dispose() {
_stateController.close();
}
void selectContenedor(Contenedor contenedor) {
final newState = ContenedoresState(currentContenedor: contenedor);
_stateController.sink.add(newState);
}
void main() {
runApp(
BlocProvider(
create: (context) => MyBloc(),
child: const MyApp(),
),
);
}
class MyApp extends StatelessWidget {
@ -36,155 +20,10 @@ class MyApp extends StatelessWidget {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme:
ColorScheme.fromSeed(seedColor: Color.fromARGB(255, 255, 255, 255)),
colorScheme: ColorScheme.fromSeed(seedColor: Color.fromARGB(255, 0, 67, 168)),
useMaterial3: true,
),
home: MyHomePage(
title: 'COMUNIDAD DE REGANTES DE CIVÁN',
contenedoresBloc: ContenedoresBloc()
),
home: const MyHomePage(title: 'COMUNIDAD DE REGANTES DE CIVÁN'),
);
}
}
class MyHomePage extends StatelessWidget {
final String title;
final ContenedoresBloc contenedoresBloc;
MyHomePage({Key? key, required this.title, required this.contenedoresBloc})
: super(key: key);
Future<void> _launchURL(String url) async {
try {
await launch(url);
} catch (e) {
// Captura de la excepción
print('An error occurred: $e');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text(''),
centerTitle: true,
backgroundColor: const Color.fromARGB(255, 33, 150, 243),
leading: IconButton(
onPressed: () {},
icon: const Icon(Icons.menu),
color: Colors.white,
),
flexibleSpace: Container(
padding: const EdgeInsets.all(8.0),
child: Center(
child: Image.asset(
'assets/logo-civan.png',
height: 150,
width: 150,
),
),
),
),
body: Container(
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/agua.jpg'),
fit: BoxFit.cover,
),
),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
_buildContenedor(context, 'Noticias'),
_buildContenedor(context, 'Pregón del día'),
_buildContenedor(context, 'Embalses'),
_buildContenedor(context, 'Tiempo'),
],
),
),
),
bottomNavigationBar: BottomNavigationBar(
items: const [
BottomNavigationBarItem(
icon: Icon(Icons.call),
label: 'Llamar',
),
BottomNavigationBarItem(
icon: Icon(Icons.email),
label: 'Correo',
),
],
selectedItemColor: Colors.blue,
unselectedItemColor: Colors.blue,
backgroundColor: const Color.fromARGB(255, 255, 255, 255),
currentIndex: 0,
onTap: (int index) {
if (index == 0) {
// Abre la aplicación de teléfono con el número marcado
_launchURL('tel:+348766361379'); // Reemplazar con el número de teléfono real
} else if (index == 1) {
// Abre la aplicación de correo electrónico
_launchURL('mailto:info@example.com'); // Reemplazar con la dirección de correo electrónico real
}
},
),
);
}
Widget _buildContenedor(BuildContext context, String nombre) {
return InkWell(
onTap: () {
switch (nombre) {
case 'Noticias':
contenedoresBloc.selectContenedor(Contenedor.noticias);
print("Contenedor noticias pulsado");
break;
case 'Pregón del día':
contenedoresBloc.selectContenedor(Contenedor.pregon);
print("Contenedor pregon pulsado");
break;
case 'Embalses':
contenedoresBloc.selectContenedor(Contenedor.embalses);
print("Contenedor embalses pulsado");
break;
case 'Tiempo':
contenedoresBloc.selectContenedor(Contenedor.tiempo);
print("Contenedor tiempo pulsado");
break;
}
},
child: SizedBox(
width: 300.0,
child: Padding(
padding: const EdgeInsets.only(bottom: 20.0),
child: Container(
padding: const EdgeInsets.all(8.0),
decoration: BoxDecoration(
color: nombre == 'Noticias'
? Colors.blue
: const Color.fromRGBO(33, 150, 243, 1),
borderRadius: BorderRadius.circular(7.0),
),
child: Text(
nombre,
textAlign: TextAlign.center,
style: const TextStyle(
color: Colors.white,
fontSize: 32.0,
fontWeight: FontWeight.bold,
),
),
),
),
),
);
}
}
void main() {
runApp(const MyApp());
}

152
lib/my_home_page.dart Normal file
View File

@ -0,0 +1,152 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
class MyHomePage extends StatelessWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text(''),
centerTitle: true,
backgroundColor: Color.fromARGB(255, 194, 218, 230),
flexibleSpace: Container(
padding: const EdgeInsets.all(8.0),
child: Center(
child: Padding(
padding: const EdgeInsets.only(top: 30.0), // Margen superior
child: Image.asset(
'assets/logo-civan.png',
height: 150,
width: 150,
),
),
),
),
),
body: Container(
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/logo-fondo.png'),
fit: BoxFit.cover,
),
),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
InkWell(
onTap: () {
},
child: SizedBox(
width: 300.0,
child: Padding(
padding: const EdgeInsets.only(bottom: 20.0),
child: Container(
padding: const EdgeInsets.all(8.0),
decoration: BoxDecoration(
color: Color.fromARGB(255, 43, 102, 150),
borderRadius: BorderRadius.circular(7.0),
),
child: const Text(
'Noticias',
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontSize: 32.0,
fontWeight: FontWeight.bold,
),
),
),
),
),
),
InkWell(
onTap: () {
},
child: SizedBox(
width: 300.0,
child: Padding(
padding: const EdgeInsets.only(bottom: 20.0),
child: Container(
padding: const EdgeInsets.all(8.0),
decoration: BoxDecoration(
color: Color.fromARGB(255, 43, 102, 150),
borderRadius: BorderRadius.circular(7.0),
),
child: const Text(
'Pregón',
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontSize: 32.0,
fontWeight: FontWeight.bold,
),
),
),
),
),
),
InkWell(
onTap: () {
},
child: SizedBox(
width: 300.0,
child: Padding(
padding: const EdgeInsets.only(bottom: 20.0),
child: Container(
padding: const EdgeInsets.all(8.0),
decoration: BoxDecoration(
color: Color.fromARGB(255, 43, 102, 150),
borderRadius: BorderRadius.circular(7.0),
),
child: const Text(
'Embalses',
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontSize: 32.0,
fontWeight: FontWeight.bold,
),
),
),
),
),
),
InkWell(
onTap: () {
},
child: SizedBox(
width: 300.0,
child: Padding(
padding: const EdgeInsets.only(bottom: 20.0),
child: Container(
padding: const EdgeInsets.all(8.0),
decoration: BoxDecoration(
color: Color.fromARGB(255, 43, 102, 150),
borderRadius: BorderRadius.circular(7.0),
),
child: const Text(
'Tiempo',
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontSize: 32.0,
fontWeight: FontWeight.bold,
),
),
),
),
),
),
],
),
),
),
);
}
}