updated create community dialog to mange edit/create

This commit is contained in:
hannathkadher
2024-12-04 10:36:41 +04:00
parent 7ee335ab1a
commit 0b628c85a5
4 changed files with 88 additions and 35 deletions

View File

@ -72,6 +72,7 @@ class _BlankCommunityWidgetState extends State<BlankCommunityWidget> {
showDialog(
context: parentContext,
builder: (context) => CreateCommunityDialog(
isEditMode: false,
communities: widget.communities,
onCreateCommunity: (String communityName, String description) {
parentContext.read<SpaceManagementBloc>().add(
@ -84,4 +85,5 @@ class _BlankCommunityWidgetState extends State<BlankCommunityWidget> {
),
);
}
}

View File

@ -1,10 +1,12 @@
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.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/pages/spaces_management/widgets/dialogs/create_community_dialog.dart';
import 'package:syncrow_web/utils/color_manager.dart';
import 'package:syncrow_web/utils/constants/assets.dart';
class CommunityStructureHeader extends StatelessWidget {
class CommunityStructureHeader extends StatefulWidget {
final String? communityName;
final bool isEditingName;
final bool isSave;
@ -13,19 +15,28 @@ class CommunityStructureHeader extends StatelessWidget {
final VoidCallback onDelete;
final VoidCallback onEditName;
final ValueChanged<String> onNameSubmitted;
final List<CommunityModel> communities;
final CommunityModel? community;
const CommunityStructureHeader({
Key? key,
required this.communityName,
required this.isSave,
required this.isEditingName,
required this.nameController,
required this.onSave,
required this.onDelete,
required this.onEditName,
required this.onNameSubmitted,
}) : super(key: key);
const CommunityStructureHeader(
{super.key,
required this.communityName,
required this.isSave,
required this.isEditingName,
required this.nameController,
required this.onSave,
required this.onDelete,
required this.onEditName,
required this.onNameSubmitted,
this.community,
required this.communities});
@override
State<CommunityStructureHeader> createState() =>
_CommunityStructureHeaderState();
}
class _CommunityStructureHeaderState extends State<CommunityStructureHeader> {
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
@ -60,47 +71,62 @@ class CommunityStructureHeader extends StatelessWidget {
);
}
void _showCreateCommunityDialog(BuildContext parentContext) {
showDialog(
context: parentContext,
builder: (context) => CreateCommunityDialog(
isEditMode: true,
communities: widget.communities,
communityToEdit: widget.community,
onCreateCommunity: (String communityName, String description) {
widget.onNameSubmitted(communityName);
},
),
);
}
Widget _buildCommunityInfo(ThemeData theme, double screenWidth) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Community Structure',
style: theme.textTheme.headlineLarge?.copyWith(color: ColorsManager.blackColor),
style: theme.textTheme.headlineLarge
?.copyWith(color: ColorsManager.blackColor),
),
if (communityName != null)
if (widget.communityName != null)
Row(
children: [
Expanded(
child: Row(
children: [
if (!isEditingName)
if (!widget.isEditingName)
Flexible(
child: Text(
communityName!,
style:
theme.textTheme.bodyLarge?.copyWith(color: ColorsManager.blackColor),
widget.communityName!,
style: theme.textTheme.bodyLarge
?.copyWith(color: ColorsManager.blackColor),
overflow: TextOverflow.ellipsis,
maxLines: 1,
),
),
if (isEditingName)
if (widget.isEditingName)
SizedBox(
width: screenWidth * 0.1,
child: TextField(
controller: nameController,
controller: widget.nameController,
decoration: const InputDecoration(
border: InputBorder.none,
isDense: true,
),
style:
theme.textTheme.bodyLarge?.copyWith(color: ColorsManager.blackColor),
onSubmitted: onNameSubmitted,
style: theme.textTheme.bodyLarge
?.copyWith(color: ColorsManager.blackColor),
onSubmitted: widget.onNameSubmitted,
),
),
const SizedBox(width: 2),
GestureDetector(
onTap: onEditName,
onTap: () => _showCreateCommunityDialog(context),
child: SvgPicture.asset(
Assets.iconEdit,
width: 16,
@ -110,7 +136,7 @@ class CommunityStructureHeader extends StatelessWidget {
],
),
),
if (isSave) ...[
if (widget.isSave) ...[
const SizedBox(width: 8),
_buildActionButtons(theme),
],
@ -127,8 +153,9 @@ class CommunityStructureHeader extends StatelessWidget {
children: [
_buildButton(
label: "Save",
icon: const Icon(Icons.save, size: 18, color: ColorsManager.spaceColor),
onPressed: onSave,
icon: const Icon(Icons.save,
size: 18, color: ColorsManager.spaceColor),
onPressed: widget.onSave,
theme: theme),
],
);
@ -159,7 +186,8 @@ class CommunityStructureHeader extends StatelessWidget {
Flexible(
child: Text(
label,
style: theme.textTheme.bodySmall?.copyWith(color: ColorsManager.blackColor),
style: theme.textTheme.bodySmall
?.copyWith(color: ColorsManager.blackColor),
overflow: TextOverflow.ellipsis,
maxLines: 1,
),

View File

@ -119,7 +119,9 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
crossAxisAlignment: CrossAxisAlignment.start,
children: [
CommunityStructureHeader(
communities: widget.communities,
communityName: widget.selectedCommunity?.name,
community: widget.selectedCommunity,
isSave: isSave(spaces),
isEditingName: isEditingName,
nameController: _nameController,

View File

@ -7,9 +7,16 @@ import 'package:syncrow_web/utils/color_manager.dart';
class CreateCommunityDialog extends StatefulWidget {
final Function(String name, String description) onCreateCommunity;
final List<CommunityModel> communities;
final bool isEditMode;
final CommunityModel? communityToEdit;
const CreateCommunityDialog(
{super.key, required this.onCreateCommunity, required this.communities});
const CreateCommunityDialog({
super.key,
required this.onCreateCommunity,
required this.communities,
required this.isEditMode,
this.communityToEdit,
});
@override
CreateCommunityDialogState createState() => CreateCommunityDialogState();
@ -19,6 +26,16 @@ class CreateCommunityDialogState extends State<CreateCommunityDialog> {
String enteredName = '';
bool isNameFieldExist = false;
bool isNameEmpty = false;
late TextEditingController nameController;
@override
void initState() {
super.initState();
// Initialize fields for edit mode or set defaults for create mode
nameController = TextEditingController(
text: widget.isEditMode ? widget.communityToEdit?.name ?? '' : '',
);
}
@override
Widget build(BuildContext context) {
@ -52,9 +69,9 @@ class CreateCommunityDialogState extends State<CreateCommunityDialog> {
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'Community Name',
style: TextStyle(
Text(
widget.isEditMode ? 'Edit Community Name' : 'Community Name',
style: const TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
),
@ -62,11 +79,14 @@ class CreateCommunityDialogState extends State<CreateCommunityDialog> {
const SizedBox(height: 16),
// Input field for the community name
TextField(
controller: nameController,
onChanged: (value) {
setState(() {
enteredName = value.trim();
isNameFieldExist = widget.communities.any(
(community) => community.name == enteredName,
(community) =>
community.name == enteredName &&
widget.communityToEdit != community,
);
if (value.isEmpty) {
isNameEmpty = true;
@ -99,7 +119,8 @@ class CreateCommunityDialogState extends State<CreateCommunityDialog> {
borderSide: BorderSide(
color: isNameFieldExist || isNameEmpty
? ColorsManager.red
: ColorsManager.boxColor, width: 1),
: ColorsManager.boxColor,
width: 1),
borderRadius: BorderRadius.circular(10),
),
focusedBorder: OutlineInputBorder(