You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
HMG_Patient_App_New/lib/widgets/dropdown/dropdown_widget.dart

159 lines
5.2 KiB
Dart

2 months ago
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart' show Icons, PopupMenuItem, showMenu, Colors;
import 'package:hmg_patient_app_new/core/utils/size_utils.dart';
2 months ago
import 'package:hmg_patient_app_new/core/utils/utils.dart';
import 'package:hmg_patient_app_new/extensions/widget_extensions.dart';
2 months ago
import 'package:hmg_patient_app_new/theme/colors.dart';
2 months ago
class DropdownWidget extends StatelessWidget {
final String labelText;
final String hintText;
final List<String> dropdownItems;
final String? selectedValue;
final Function(String?)? onChange;
final bool isEnable;
final bool isBorderAllowed;
final bool isAllowRadius;
final EdgeInsetsGeometry? padding;
final bool hasSelectionCustomIcon;
final String? selectionCustomIcon;
2 months ago
final String? leadingIcon;
final Color? labelColor;
2 months ago
const DropdownWidget(
{Key? key,
required this.labelText,
required this.hintText,
required this.dropdownItems,
this.selectedValue,
this.onChange,
this.isEnable = true,
this.isBorderAllowed = true,
this.isAllowRadius = true,
this.padding,
this.hasSelectionCustomIcon = false,
this.selectionCustomIcon,
this.leadingIcon,
this.labelColor})
: super(key: key);
2 months ago
@override
Widget build(BuildContext context) {
2 months ago
Widget content = Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [_buildLabelText(labelColor), _buildDropdown(context)],
2 months ago
);
2 months ago
return Container(
padding: padding,
2 months ago
alignment: Alignment.center, // This might need adjustment based on layout
2 months ago
decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
color: Colors.white,
borderRadius: isAllowRadius ? 15.h : null,
side: isBorderAllowed ? BorderSide(color: const Color(0xffefefef), width: 1) : null,
2 months ago
),
2 months ago
child: Row(
// Wrap with a Row
crossAxisAlignment: CrossAxisAlignment.center, // Align items vertically in the center
2 months ago
children: [
2 months ago
if (leadingIcon != null) ...[
_buildLeadingIcon(),
SizedBox(width: 3.h),
],
Expanded(child: content),
2 months ago
],
),
);
}
2 months ago
Widget _buildLeadingIcon() {
return Container(
height: 40.h,
width: 40.h,
margin: EdgeInsets.only(right: 10.h),
padding: EdgeInsets.all(8.h),
decoration: RoundedRectangleBorder().toSmoothCornerDecoration(borderRadius: 10.h, color: AppColors.greyColor),
child: Utils.buildSvgWithAssets(icon: leadingIcon!));
}
Widget _buildLabelText(Color? labelColor) {
2 months ago
return Text(
labelText,
style: TextStyle(
fontSize: 12.fSize,
2 months ago
fontWeight: FontWeight.w500,
color: labelColor ?? Color(0xff898A8D),
2 months ago
letterSpacing: -0.2,
height: 18 / 12,
),
);
}
Widget _buildDropdown(BuildContext context) {
return GestureDetector(
onTap: isEnable
? () async {
final renderBox = context.findRenderObject() as RenderBox;
final offset = renderBox.localToGlobal(Offset.zero);
final selected = await showMenu<String>(
context: context,
position: RelativeRect.fromLTRB(
offset.dx,
offset.dy + renderBox.size.height,
offset.dx + renderBox.size.width,
0,
),
items: dropdownItems
.map(
(e) => PopupMenuItem<String>(
value: e,
child: Text(
e,
style: TextStyle(
fontSize: 14.fSize,
height: 21 / 14,
fontWeight: FontWeight.w500,
letterSpacing: -0.2,
),
),
),
)
.toList(),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
color: AppColors.scaffoldBgColor
);
2 months ago
if (selected != null && onChange != null) {
onChange!(selected);
}
}
2 months ago
: null,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Expanded(
child: Text(
(selectedValue == null || selectedValue!.isEmpty) ? hintText : selectedValue!,
2 months ago
textAlign: TextAlign.left,
textDirection: TextDirection.ltr,
style: TextStyle(
fontSize: 14.fSize,
2 months ago
height: 21 / 14,
fontWeight: FontWeight.w500,
color: (selectedValue != null && selectedValue!.isNotEmpty) ? const Color(0xff2E3039) : const Color(0xffB0B0B0),
2 months ago
letterSpacing: -0.2,
),
),
),
if (hasSelectionCustomIcon && selectionCustomIcon != null) Utils.buildSvgWithAssets(icon: selectionCustomIcon!) else const Icon(Icons.keyboard_arrow_down_outlined),
2 months ago
],
),
);
}
}