added validation for community name

This commit is contained in:
hannathkadher
2024-11-29 17:22:00 +04:00
parent a3c6421b0d
commit 3c543b2e0f
5 changed files with 73 additions and 61 deletions

View File

@ -2,12 +2,20 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/spaces_management/bloc/space_management_bloc.dart'; import 'package:syncrow_web/pages/spaces_management/bloc/space_management_bloc.dart';
import 'package:syncrow_web/pages/spaces_management/bloc/space_management_event.dart'; import 'package:syncrow_web/pages/spaces_management/bloc/space_management_event.dart';
import 'package:syncrow_web/pages/spaces_management/model/community_model.dart';
import 'package:syncrow_web/pages/spaces_management/widgets/dialogs/create_community_dialog.dart'; import 'package:syncrow_web/pages/spaces_management/widgets/dialogs/create_community_dialog.dart';
import 'package:syncrow_web/utils/color_manager.dart'; import 'package:syncrow_web/utils/color_manager.dart';
class BlankCommunityWidget extends StatelessWidget { class BlankCommunityWidget extends StatefulWidget {
const BlankCommunityWidget({Key? key}) : super(key: key); final List<CommunityModel> communities;
BlankCommunityWidget({required this.communities});
@override
_BlankCommunityWidgetState createState() => _BlankCommunityWidgetState();
}
class _BlankCommunityWidgetState extends State<BlankCommunityWidget> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Expanded( return Expanded(
@ -17,11 +25,10 @@ class BlankCommunityWidget extends StatelessWidget {
child: GridView.builder( child: GridView.builder(
padding: const EdgeInsets.only(left: 40.0, top: 20.0), padding: const EdgeInsets.only(left: 40.0, top: 20.0),
gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent( gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
maxCrossAxisExtent: 400, // Each item's width will be 400 or less maxCrossAxisExtent: 400,
mainAxisSpacing: 10, // Spacing between items mainAxisSpacing: 10,
crossAxisSpacing: 10, // Spacing between items crossAxisSpacing: 10,
childAspectRatio: childAspectRatio: 2.0,
2.0, // Aspect ratio for width:height (e.g., 300:150 = 2.0)
), ),
itemCount: 1, // Only one item itemCount: 1, // Only one item
itemBuilder: (context, index) { itemBuilder: (context, index) {
@ -48,9 +55,8 @@ class BlankCommunityWidget extends StatelessWidget {
), ),
), ),
), ),
const SizedBox(height: 9),
const SizedBox(height: 9), // Space between item and text Text('Blank',
Text('Blank', // Text below the item
style: Theme.of(context).textTheme.bodyLarge?.copyWith( style: Theme.of(context).textTheme.bodyLarge?.copyWith(
color: ColorsManager.blackColor, color: ColorsManager.blackColor,
)), )),
@ -66,6 +72,7 @@ class BlankCommunityWidget extends StatelessWidget {
showDialog( showDialog(
context: parentContext, context: parentContext,
builder: (context) => CreateCommunityDialog( builder: (context) => CreateCommunityDialog(
communities: widget.communities,
onCreateCommunity: (String communityName, String description) { onCreateCommunity: (String communityName, String description) {
parentContext.read<SpaceManagementBloc>().add( parentContext.read<SpaceManagementBloc>().add(
CreateCommunityEvent( CreateCommunityEvent(

View File

@ -24,12 +24,13 @@ class CommunityStructureArea extends StatefulWidget {
SpaceModel? selectedSpace; SpaceModel? selectedSpace;
final List<ProductModel>? products; final List<ProductModel>? products;
final ValueChanged<SpaceModel?>? onSpaceSelected; final ValueChanged<SpaceModel?>? onSpaceSelected;
final List<CommunityModel> communities;
final List<SpaceModel> spaces; final List<SpaceModel> spaces;
CommunityStructureArea({ CommunityStructureArea({
this.selectedCommunity, this.selectedCommunity,
this.selectedSpace, this.selectedSpace,
required this.communities,
this.products, this.products,
required this.spaces, required this.spaces,
this.onSpaceSelected, this.onSpaceSelected,
@ -97,7 +98,9 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
if (widget.selectedCommunity == null) { if (widget.selectedCommunity == null) {
return BlankCommunityWidget(); return BlankCommunityWidget(
communities: widget.communities,
);
} }
Size screenSize = MediaQuery.of(context).size; Size screenSize = MediaQuery.of(context).size;

View File

@ -1,37 +1,46 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:syncrow_web/pages/common/buttons/cancel_button.dart';
import 'package:syncrow_web/pages/common/buttons/default_button.dart';
import 'package:syncrow_web/pages/spaces_management/model/community_model.dart';
import 'package:syncrow_web/utils/color_manager.dart'; import 'package:syncrow_web/utils/color_manager.dart';
class CreateCommunityDialog extends StatefulWidget { class CreateCommunityDialog extends StatefulWidget {
final Function(String name, String description) onCreateCommunity; final Function(String name, String description) onCreateCommunity;
final List<CommunityModel> communities;
const CreateCommunityDialog({super.key, required this.onCreateCommunity}); const CreateCommunityDialog(
{super.key, required this.onCreateCommunity, required this.communities});
@override @override
CreateCommunityDialogState createState() => CreateCommunityDialogState(); CreateCommunityDialogState createState() => CreateCommunityDialogState();
} }
class CreateCommunityDialogState extends State<CreateCommunityDialog> { class CreateCommunityDialogState extends State<CreateCommunityDialog> {
String enteredName = ''; // Store the entered community name String enteredName = '';
bool isNameFieldExist = false;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final screenWidth = MediaQuery.of(context).size.width;
return Dialog( return Dialog(
shape: RoundedRectangleBorder( shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20), borderRadius: BorderRadius.circular(20),
), ),
backgroundColor: Colors.transparent, // Transparent for shadow effect backgroundColor:
ColorsManager.transparentColor, // Transparent for shadow effect
child: Stack( child: Stack(
children: [ children: [
// Background container with shadow and rounded corners // Background container with shadow and rounded corners
Container( Container(
width: 490, width: screenWidth * 0.3,
padding: const EdgeInsets.all(20), padding: const EdgeInsets.all(20),
decoration: BoxDecoration( decoration: BoxDecoration(
color: Colors.white, color: ColorsManager.whiteColors,
borderRadius: BorderRadius.circular(20), borderRadius: BorderRadius.circular(20),
boxShadow: [ boxShadow: [
BoxShadow( BoxShadow(
color: Colors.black.withOpacity(0.25), color: ColorsManager.blackColor.withOpacity(0.25),
blurRadius: 20, blurRadius: 20,
spreadRadius: 5, spreadRadius: 5,
offset: const Offset(0, 5), offset: const Offset(0, 5),
@ -53,10 +62,15 @@ class CreateCommunityDialogState extends State<CreateCommunityDialog> {
// Input field for the community name // Input field for the community name
TextField( TextField(
onChanged: (value) { onChanged: (value) {
enteredName = value; // Capture entered name setState(() {
enteredName = value.trim(); // Trim whitespace
isNameFieldExist = widget.communities.any(
(community) => community.name == enteredName,
);
});
}, },
style: const TextStyle( style: const TextStyle(
color: Colors.black, color: ColorsManager.blackColor,
), ),
decoration: InputDecoration( decoration: InputDecoration(
hintText: 'Please enter the community name', hintText: 'Please enter the community name',
@ -68,45 +82,51 @@ class CreateCommunityDialogState extends State<CreateCommunityDialog> {
fontWeight: FontWeight.w400, fontWeight: FontWeight.w400,
), ),
border: OutlineInputBorder( border: OutlineInputBorder(
borderSide: const BorderSide(color: ColorsManager.boxColor, width: 1), borderSide: const BorderSide(
color: ColorsManager.boxColor, width: 1),
borderRadius: BorderRadius.circular(10), borderRadius: BorderRadius.circular(10),
), ),
enabledBorder: OutlineInputBorder( enabledBorder: OutlineInputBorder(
borderSide: const BorderSide(color: ColorsManager.boxColor, width: 1), borderSide: const BorderSide(
color: ColorsManager.boxColor, width: 1),
borderRadius: BorderRadius.circular(10), borderRadius: BorderRadius.circular(10),
), ),
focusedBorder: OutlineInputBorder( focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(10), borderRadius: BorderRadius.circular(10),
borderSide: const BorderSide(color: ColorsManager.boxColor, width: 1), borderSide: const BorderSide(
color: ColorsManager.boxColor, width: 1),
), ),
), ),
), ),
if (isNameFieldExist)
Padding(
padding: const EdgeInsets.only(top: 8.0),
child: Text(
'*Name already exists.',
style: Theme.of(context)
.textTheme
.bodySmall
?.copyWith(color: ColorsManager.red),
),
),
const SizedBox(height: 24), const SizedBox(height: 24),
// Action buttons // Action buttons
Row( Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween, mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [ children: [
Expanded( Expanded(
child: TextButton( child: Expanded(
onPressed: () => Navigator.of(context).pop(), child: CancelButton(
style: TextButton.styleFrom( label: 'Cancel',
padding: const EdgeInsets.symmetric(vertical: 16), onPressed: () => Navigator.of(context).pop(),
backgroundColor: ColorsManager.boxColor,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
),
child: const Text(
'Cancel',
style: TextStyle(color: Colors.black),
), ),
), ),
), ),
const SizedBox(width: 16), const SizedBox(width: 16),
Expanded( Expanded(
child: ElevatedButton( child: DefaultButton(
onPressed: () { onPressed: () {
if (enteredName.isNotEmpty) { if (enteredName.isNotEmpty && !isNameFieldExist) {
widget.onCreateCommunity( widget.onCreateCommunity(
enteredName, enteredName,
"", "",
@ -114,14 +134,11 @@ class CreateCommunityDialogState extends State<CreateCommunityDialog> {
Navigator.of(context).pop(); Navigator.of(context).pop();
} }
}, },
style: ElevatedButton.styleFrom( backgroundColor: isNameFieldExist || enteredName.isEmpty
padding: const EdgeInsets.symmetric(vertical: 16), ? ColorsManager.lightGrayColor
backgroundColor: ColorsManager.secondaryColor, : ColorsManager.secondaryColor,
foregroundColor: Colors.white, borderRadius: 10,
shape: RoundedRectangleBorder( foregroundColor: ColorsManager.whiteColors,
borderRadius: BorderRadius.circular(10),
),
),
child: const Text('OK'), child: const Text('OK'),
), ),
), ),

View File

@ -43,6 +43,7 @@ class _LoadedStateViewState extends State<LoadedSpaceView> {
selectedSpace: widget.selectedSpace, selectedSpace: widget.selectedSpace,
spaces: widget.selectedCommunity?.spaces ?? [], spaces: widget.selectedCommunity?.spaces ?? [],
products: widget.products, products: widget.products,
communities: widget.communities,
), ),
], ],
), ),

View File

@ -49,22 +49,6 @@ class _SidebarWidgetState extends State<SidebarWidget> {
} }
} }
void _showCreateCommunityDialog(BuildContext parentContext) {
showDialog(
context: parentContext,
builder: (context) => CreateCommunityDialog(
onCreateCommunity: (String communityName, String description) {
parentContext.read<SpaceManagementBloc>().add(
CreateCommunityEvent(
name: communityName,
description: description,
),
);
},
),
);
}
// Function to filter communities based on the search query // Function to filter communities based on the search query
List<CommunityModel> _filterCommunities() { List<CommunityModel> _filterCommunities() {
if (_searchQuery.isEmpty) { if (_searchQuery.isEmpty) {