Added create a unit screen, and implemented otp screen bloc and view

This commit is contained in:
Abdullah Alassaf
2024-05-30 10:59:22 +03:00
parent 28576f2cd5
commit 9877dde78e
22 changed files with 789 additions and 348 deletions
@@ -0,0 +1,42 @@
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/menu/bloc/create_unit_bloc/create_unit_event.dart';
import 'package:syncrow_app/features/menu/bloc/create_unit_bloc/create_unit_state.dart';
import 'package:syncrow_app/utils/helpers/snack_bar.dart';
class CreateUnitBloc extends Bloc<CreateUnitEvent, CreateUnitState> {
List<String> rooms = [];
String unitName = '';
String locationName = '';
CreateUnitBloc() : super(InitialState()) {
on<CreateRoomEvent>(_createRoom);
on<RemoveRoomEvent>(_removeRoom);
}
void _createRoom(CreateRoomEvent event, Emitter<CreateUnitState> emit) async {
try {
emit(LoadingState());
if (rooms.contains(event.roomName)) {
emit(const ErrorState(message: 'This name is already exist'));
emit(CreateRoomState(roomList: rooms));
return;
}
rooms.add(event.roomName);
emit(CreateRoomState(roomList: rooms));
} catch (e) {
emit(const ErrorState(message: 'Something went wrong'));
return;
}
}
void _removeRoom(RemoveRoomEvent event, Emitter<CreateUnitState> emit) {
try {
emit(LoadingState());
rooms.removeWhere((element) => element == event.roomName);
emit(CreateRoomState(roomList: rooms));
} catch (e) {
emit(const ErrorState(message: 'Something went wrong'));
return;
}
}
}
@@ -0,0 +1,30 @@
import 'package:equatable/equatable.dart';
abstract class CreateUnitEvent extends Equatable {
const CreateUnitEvent();
@override
List<Object> get props => [];
}
class InitialEvent extends CreateUnitEvent {}
class LoadingEvent extends CreateUnitEvent {}
class CreateRoomEvent extends CreateUnitEvent {
final String roomName;
const CreateRoomEvent({required this.roomName});
@override
List<Object> get props => [roomName];
}
class RemoveRoomEvent extends CreateUnitEvent {
final String roomName;
const RemoveRoomEvent({required this.roomName});
@override
List<Object> get props => [roomName];
}
@@ -0,0 +1,30 @@
import 'package:equatable/equatable.dart';
abstract class CreateUnitState extends Equatable {
const CreateUnitState();
@override
List<Object> get props => [];
}
class InitialState extends CreateUnitState {}
class LoadingState extends CreateUnitState {}
class CreateRoomState extends CreateUnitState {
final List<String> roomList;
const CreateRoomState({required this.roomList});
@override
List<Object> get props => [roomList];
}
class ErrorState extends CreateUnitState {
final String message;
const ErrorState({required this.message});
@override
List<Object> get props => [message];
}
@@ -1,124 +1,247 @@
// ignore_for_file: unnecessary_const
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:syncrow_app/features/menu/bloc/create_unit_bloc/create_unit_bloc.dart';
import 'package:syncrow_app/features/menu/bloc/create_unit_bloc/create_unit_event.dart';
import 'package:syncrow_app/features/menu/bloc/create_unit_bloc/create_unit_state.dart';
import 'package:syncrow_app/features/shared_widgets/default_container.dart';
import 'package:syncrow_app/features/shared_widgets/default_scaffold.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_large.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_medium.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_small.dart';
import 'package:syncrow_app/generated/assets.dart';
import 'package:syncrow_app/utils/context_extension.dart';
import 'package:syncrow_app/utils/helpers/snack_bar.dart';
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';
class CreateHomeView extends StatelessWidget {
const CreateHomeView({super.key});
class CreateUnitView extends StatelessWidget {
const CreateUnitView({super.key});
@override
Widget build(BuildContext context) {
return DefaultScaffold(
actions: [
TextButton(
onPressed: () {},
child: const BodyLarge(
text: 'Save',
fontWeight: FontWeight.bold,
),
),
],
title: 'Create Home',
child: Column(
children: [
//Home Info
DefaultContainer(
padding: const EdgeInsets.symmetric(
horizontal: 25,
vertical: 5,
),
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const BodyMedium(text: 'Home Name '),
Flexible(
child: TextField(
textAlign: TextAlign.end,
decoration: InputDecoration(
hintText: 'Enter Name',
hintStyle:
context.bodyMedium.copyWith(color: Colors.grey),
border: InputBorder.none,
),
),
),
],
),
//Divider
Container(
height: 1,
color: ColorsManager.greyColor,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const BodyMedium(text: 'Location '),
Flexible(
child: TextField(
textAlign: TextAlign.end,
decoration: InputDecoration(
hintText: 'Set',
hintStyle:
context.bodyMedium.copyWith(color: Colors.grey),
border: InputBorder.none,
),
),
),
],
),
],
),
),
//Rooms Info
const SizedBox(height: 10),
const Row(
children: [
SizedBox(
width: 25,
),
BodySmall(
text: 'Rooms',
String unitName = '';
String location = '';
TextEditingController textEditingController = TextEditingController();
return BlocProvider(
create: (context) => CreateUnitBloc(),
child: BlocConsumer<CreateUnitBloc, CreateUnitState>(listener: (context, state) {
if (state is ErrorState) {
CustomSnackBar.displaySnackBar(state.message);
}
}, builder: (context, state) {
textEditingController.text = '';
return DefaultScaffold(
actions: [
TextButton(
onPressed: () {},
child: const BodyLarge(
text: 'Save',
fontWeight: FontWeight.bold,
),
],
),
DefaultContainer(
padding: const EdgeInsets.symmetric(
horizontal: 25,
vertical: 5,
),
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.start,
children: [
TextButton(
onPressed: () {
//TODO add room
},
child: const Row(
],
title: 'Create a Unit',
child: Container(
padding: const EdgeInsets.symmetric(vertical: 40),
width: MediaQuery.sizeOf(context).width,
height: MediaQuery.sizeOf(context).height,
child: SingleChildScrollView(
child: Column(
children: [
//Home Info
DefaultContainer(
padding: const EdgeInsets.symmetric(
horizontal: 25,
vertical: 5,
),
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.start,
children: [
BodyMedium(
text: 'Add Room',
fontColor: ColorsManager.primaryColor,
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const BodyMedium(text: 'Unit Name'),
Flexible(
child: TextField(
textAlign: TextAlign.end,
onChanged: (value) {
unitName = value;
},
decoration: InputDecoration(
hintText: 'Enter Name',
hintStyle: context.bodyMedium.copyWith(color: Colors.grey),
border: InputBorder.none,
),
),
),
],
),
const Divider(
color: ColorsManager.greyColor,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const BodyMedium(text: 'Location '),
Flexible(
child: TextField(
textAlign: TextAlign.end,
onChanged: (value) {
location = value;
},
decoration: InputDecoration(
hintText: 'Set',
hintStyle: context.bodyMedium.copyWith(color: Colors.grey),
border: InputBorder.none,
),
),
),
],
),
],
))
],
),
),
//Rooms Info
const SizedBox(height: 10),
const Row(
children: [
SizedBox(
width: 25,
),
BodySmall(
text: 'Spaces',
fontWeight: FontWeight.bold,
),
],
),
const SizedBox(
height: 4,
),
DefaultContainer(
padding: const EdgeInsets.symmetric(
horizontal: 25,
vertical: 5,
),
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.start,
children: [
if (state is CreateRoomState)
if (state.roomList.isNotEmpty)
Column(
children: [
SizedBox(
height: (state.roomList.length) * 55,
child: ListView.separated(
physics:
const NeverScrollableScrollPhysics(), // Disable scrolling
separatorBuilder: (context, index) => const Divider(
color: ColorsManager.greyColor,
),
itemCount: state.roomList.length,
itemBuilder: (context, index) {
return Dismissible(
key: Key(state.roomList[index]),
background: Container(
padding: const EdgeInsets.only(right: 10),
alignment: AlignmentDirectional.centerEnd,
decoration: ShapeDecoration(
color: const Color(0xFFFF0000),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(index == 0 ? 20 : 0),
topRight: Radius.circular(index == 0 ? 20 : 0),
),
),
),
child: SvgPicture.asset(
Assets.assetsDeleteIcon,
width: 20,
height: 22,
),
),
direction: DismissDirection.endToStart,
onDismissed: (direction) {
String removedUnit = state.roomList[index];
BlocProvider.of<CreateUnitBloc>(context)
.add(RemoveRoomEvent(roomName: removedUnit));
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('$removedUnit removed')),
);
},
child: Container(
height: 50,
padding: const EdgeInsets.symmetric(vertical: 16),
child: Text(
state.roomList[index],
style: const TextStyle(
color: Color(0xFF5D5D5D),
fontSize: 15,
fontWeight: FontWeight.w400,
),
),
),
);
},
),
),
const Divider(
color: ColorsManager.greyColor,
),
],
),
Row(
children: [
TextButton(
onPressed: () {
if (textEditingController.text.isEmpty) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Please add the room name')),
);
return;
}
BlocProvider.of<CreateUnitBloc>(context)
.add(CreateRoomEvent(roomName: textEditingController.text));
},
child: const Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
BodyMedium(
text: 'Add Space',
fontColor: ColorsManager.primaryColor,
),
],
)),
Flexible(
child: TextField(
textAlign: TextAlign.end,
controller: textEditingController,
onChanged: (value) {
textEditingController.text = value;
},
decoration: InputDecoration(
hintText: 'Set',
hintStyle: context.bodyMedium.copyWith(color: Colors.grey),
border: InputBorder.none,
),
),
),
],
)
],
),
),
],
),
),
),
],
),
);
}),
);
}
}