feat:增加记录对话框内容
This commit is contained in:
46
lib/widget/dropdown_select.dart
Normal file
46
lib/widget/dropdown_select.dart
Normal file
@@ -0,0 +1,46 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:getwidget/getwidget.dart';
|
||||
|
||||
class DropdownSelect extends StatelessWidget {
|
||||
final String value;
|
||||
final ValueChanged<String?> onChanged;
|
||||
final List<String> items;
|
||||
|
||||
const DropdownSelect({
|
||||
Key? key,
|
||||
required this.value,
|
||||
required this.onChanged,
|
||||
required this.items,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
|
||||
return Container(
|
||||
width: 150,
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
border: Border.all(color: colors.primary, width: 1.5),
|
||||
),
|
||||
child: DropdownButtonHideUnderline(
|
||||
child: GFDropdown(
|
||||
itemHeight: 32,
|
||||
padding: const EdgeInsets.all(0),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
value: value,
|
||||
onChanged: onChanged,
|
||||
items:
|
||||
items
|
||||
.map(
|
||||
(value) => DropdownMenuItem(
|
||||
value: value,
|
||||
child: Text(value, style: const TextStyle(fontSize: 16)),
|
||||
),
|
||||
)
|
||||
.toList(),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
131
lib/widget/number_input.dart
Normal file
131
lib/widget/number_input.dart
Normal file
@@ -0,0 +1,131 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class NumberInput extends StatefulWidget {
|
||||
final num? value;
|
||||
final ValueChanged<num?>? onChanged;
|
||||
final double min;
|
||||
final double max;
|
||||
final double step;
|
||||
final int precision;
|
||||
final String unit;
|
||||
|
||||
const NumberInput({
|
||||
Key? key,
|
||||
this.value,
|
||||
this.onChanged,
|
||||
this.min = double.negativeInfinity,
|
||||
this.max = double.infinity,
|
||||
this.step = 1,
|
||||
this.precision = 2,
|
||||
this.unit = ''
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<NumberInput> createState() => _NumberInputState();
|
||||
}
|
||||
|
||||
class _NumberInputState extends State<NumberInput> {
|
||||
late TextEditingController _controller;
|
||||
late num _currentValue;
|
||||
|
||||
final Color disabledColor = Colors.grey.shade400;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_currentValue = widget.value ?? 0.0;
|
||||
_controller = TextEditingController(text: _formatValue(_currentValue));
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(NumberInput oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (widget.value != oldWidget.value && widget.value != _currentValue) {
|
||||
_currentValue = widget.value ?? 0.0;
|
||||
_controller.text = _formatValue(_currentValue);
|
||||
}
|
||||
}
|
||||
|
||||
String _formatValue(num value) {
|
||||
if (widget.precision == 0) return value.toInt().toString();
|
||||
return value.toStringAsFixed(widget.precision);
|
||||
}
|
||||
|
||||
void _handleValueChange(num newValue) {
|
||||
num clampedValue = newValue.clamp(widget.min, widget.max);
|
||||
if (widget.precision > 0) {
|
||||
clampedValue = double.parse(
|
||||
clampedValue.toStringAsFixed(widget.precision),
|
||||
);
|
||||
}
|
||||
|
||||
setState(() {
|
||||
_currentValue = clampedValue;
|
||||
_controller.text = _formatValue(clampedValue);
|
||||
});
|
||||
widget.onChanged?.call(clampedValue);
|
||||
}
|
||||
|
||||
Widget _buildControlButton({
|
||||
required IconData icon,
|
||||
required VoidCallback? onPressed,
|
||||
required bool disabled,
|
||||
}) {
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
|
||||
return Container(
|
||||
width: 32,
|
||||
height: 32,
|
||||
child: Material(
|
||||
color: disabled ? disabledColor : colors.primary.withAlpha(100),
|
||||
child: InkWell(
|
||||
onTap: disabled ? null : onPressed,
|
||||
borderRadius: BorderRadius.circular(4),
|
||||
child: Icon(icon, size: 18, color: colors.onSurface),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
|
||||
return Container(
|
||||
width: 150,
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
border: Border.all(color: colors.primary, width: 1.5),
|
||||
),
|
||||
child: IntrinsicWidth(
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
_buildControlButton(
|
||||
icon: Icons.remove,
|
||||
onPressed: () => _handleValueChange(_currentValue - widget.step),
|
||||
disabled: _currentValue <= widget.min,
|
||||
),
|
||||
Expanded(
|
||||
child: Text(
|
||||
'${_currentValue} ${widget.unit}',
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
),
|
||||
_buildControlButton(
|
||||
icon: Icons.add,
|
||||
onPressed: () => _handleValueChange(_currentValue + widget.step),
|
||||
disabled: _currentValue >= widget.max,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_controller.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
37
lib/widget/record/common.dart
Normal file
37
lib/widget/record/common.dart
Normal file
@@ -0,0 +1,37 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
Widget buildCardTitle({
|
||||
required BuildContext context,
|
||||
required String title,
|
||||
required IconData icon,
|
||||
}) {
|
||||
return Row(
|
||||
children: [
|
||||
buildCircleIcon(context: context, icon: icon),
|
||||
SizedBox(width: 6),
|
||||
Text(
|
||||
title,
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Theme.of(context).colorScheme.primary,
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget buildCircleIcon({
|
||||
required BuildContext context,
|
||||
required IconData icon,
|
||||
}) {
|
||||
return Container(
|
||||
width: 40,
|
||||
height: 40,
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.primary.withAlpha(50),
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
),
|
||||
child: Icon(icon, color: Theme.of(context).colorScheme.primary, size: 18),
|
||||
);
|
||||
}
|
||||
95
lib/widget/record/goal_progress.dart
Normal file
95
lib/widget/record/goal_progress.dart
Normal file
@@ -0,0 +1,95 @@
|
||||
import 'package:fitnote/modesl/health.dart';
|
||||
import 'package:fitnote/widget/record/common.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_common/widget/common_widget.dart';
|
||||
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
|
||||
import 'package:getwidget/getwidget.dart';
|
||||
|
||||
class GoalProgress extends StatefulWidget {
|
||||
const GoalProgress({super.key});
|
||||
|
||||
@override
|
||||
State<GoalProgress> createState() => _GoalProgressState();
|
||||
}
|
||||
|
||||
class _GoalProgressState extends State<GoalProgress> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return CommonCard(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
buildCardTitle(
|
||||
context: context,
|
||||
title: '本月目标进度',
|
||||
icon: FontAwesomeIcons.trophy,
|
||||
),
|
||||
const SizedBox(height: 28),
|
||||
_buildProgressItem(
|
||||
title: '目标体重',
|
||||
current: '65.2',
|
||||
target: '60',
|
||||
unit: 'kg',
|
||||
progress: 0.85,
|
||||
color: RecordType.weight.color,
|
||||
),
|
||||
const SizedBox(height: 28),
|
||||
_buildProgressItem(
|
||||
title: '运动总时长',
|
||||
current: '450',
|
||||
target: '600',
|
||||
unit: '分钟',
|
||||
progress: 0.75,
|
||||
color: RecordType.sport.color,
|
||||
),
|
||||
const SizedBox(height: 28),
|
||||
_buildProgressItem(
|
||||
title: '日均睡眠',
|
||||
current: '7.2',
|
||||
target: '8',
|
||||
unit: '小时',
|
||||
progress: 0.9,
|
||||
color: RecordType.sleep.color,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildProgressItem({
|
||||
required String title,
|
||||
required String current,
|
||||
required String target,
|
||||
required String unit,
|
||||
required double progress,
|
||||
required Color color,
|
||||
}) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Text(title),
|
||||
Text(
|
||||
'$current/$target $unit',
|
||||
style: TextStyle(fontWeight: FontWeight.w500, color: color),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
GFProgressBar(
|
||||
animation: true,
|
||||
lineHeight: 20,
|
||||
percentage: progress,
|
||||
progressBarColor: color,
|
||||
child: Text(
|
||||
'${progress * 100}%',
|
||||
textAlign: TextAlign.end,
|
||||
style: TextStyle(color: Colors.white),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
359
lib/widget/record/today_record.dart
Normal file
359
lib/widget/record/today_record.dart
Normal file
@@ -0,0 +1,359 @@
|
||||
import 'package:fitnote/modesl/health.dart';
|
||||
import 'package:fitnote/widget/dropdown_select.dart';
|
||||
import 'package:fitnote/widget/number_input.dart';
|
||||
import 'package:fitnote/widget/record/common.dart';
|
||||
import 'package:fitnote/widget/select_input.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_common/widget/common_widget.dart';
|
||||
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
|
||||
import 'package:getwidget/getwidget.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:numberpicker/numberpicker.dart';
|
||||
|
||||
class TodayRecord extends StatefulWidget {
|
||||
const TodayRecord({super.key});
|
||||
|
||||
@override
|
||||
State<TodayRecord> createState() => _TodayRecordState();
|
||||
}
|
||||
|
||||
class _TodayRecordState extends State<TodayRecord> {
|
||||
final record = HealthRecord(
|
||||
id: 1,
|
||||
sportProject: '羽毛球',
|
||||
sportDuration: 20,
|
||||
sleepStartTime: '23:00',
|
||||
sleepEndTime: '24:00',
|
||||
sleepDuration: 60,
|
||||
weight: 0,
|
||||
date: '2025-11-28',
|
||||
);
|
||||
|
||||
final recordForm = HealthRecord(
|
||||
id: 1,
|
||||
sportProject: '羽毛球',
|
||||
sportDuration: 20,
|
||||
sleepStartTime: '23:00',
|
||||
sleepEndTime: '24:00',
|
||||
sleepDuration: 60,
|
||||
weight: 70,
|
||||
date: '2025-11-28',
|
||||
);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return CommonCard(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
buildCardTitle(
|
||||
context: context,
|
||||
title: '今日记录',
|
||||
icon: FontAwesomeIcons.calendarCheck,
|
||||
),
|
||||
_buildTodayTag(),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
ListView.separated(
|
||||
physics: const NeverScrollableScrollPhysics(),
|
||||
shrinkWrap: true,
|
||||
itemCount: RecordType.values.length,
|
||||
separatorBuilder: (context, index) => SizedBox(height: 8),
|
||||
itemBuilder: (context, index) {
|
||||
final recordType = RecordType.values[index];
|
||||
return _buildRecordListTile(recordType);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildTodayTag() {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFFF1F5F9),
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
),
|
||||
child: Text(
|
||||
DateFormat('MM月dd日').format(DateTime.now()),
|
||||
style: const TextStyle(
|
||||
fontSize: 12,
|
||||
color: Color(0xFF64748B),
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildRecordListTile(RecordType type) {
|
||||
return Container(
|
||||
decoration: BoxDecoration(
|
||||
gradient: LinearGradient(
|
||||
begin: Alignment.topLeft,
|
||||
end: Alignment.bottomRight,
|
||||
colors: [type.color.withAlpha(10), Colors.white.withAlpha(200)],
|
||||
),
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.black.withAlpha(10),
|
||||
blurRadius: 12,
|
||||
offset: const Offset(0, 4),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: ListTile(
|
||||
leading: Container(
|
||||
width: 48,
|
||||
height: 48,
|
||||
decoration: BoxDecoration(
|
||||
color: type.color.withAlpha(30),
|
||||
borderRadius: BorderRadius.circular(24),
|
||||
),
|
||||
child: Icon(type.icon, color: type.color, size: 22),
|
||||
),
|
||||
title: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Text(
|
||||
type.title,
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: type.color,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
_buildRecordContent(type),
|
||||
],
|
||||
),
|
||||
trailing: GestureDetector(
|
||||
onTap: () => onTapRecord(type),
|
||||
child: Container(
|
||||
width: 40,
|
||||
height: 40,
|
||||
decoration: BoxDecoration(
|
||||
color: type.color.withAlpha(30),
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
),
|
||||
child: Icon(
|
||||
_checkHasRecordValue(type)
|
||||
? FontAwesomeIcons.pencil
|
||||
: FontAwesomeIcons.plus,
|
||||
color: type.color,
|
||||
size: 18,
|
||||
),
|
||||
),
|
||||
),
|
||||
splashColor: Colors.transparent,
|
||||
contentPadding: const EdgeInsets.symmetric(
|
||||
horizontal: 20,
|
||||
vertical: 12,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
bool _checkHasRecordValue(RecordType type) {
|
||||
switch (type) {
|
||||
case RecordType.weight:
|
||||
return record.weight != null;
|
||||
case RecordType.sport:
|
||||
return record.sportProject != null;
|
||||
case RecordType.sleep:
|
||||
return record.sleepStartTime != null;
|
||||
}
|
||||
}
|
||||
|
||||
Widget _buildDialogButton(RecordType type) {
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
|
||||
return Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: GFButton(
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
color: colors.secondary.withAlpha(100),
|
||||
text: "取消",
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: GFButton(onPressed: () {}, color: colors.primary, text: "确定"),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
// void _saveRecordData(RecordType type) {
|
||||
// setState(() {
|
||||
// switch (type) {
|
||||
// case RecordType.weight:
|
||||
// record.weight = _dialogWeight;
|
||||
// break;
|
||||
// case RecordType.sport:
|
||||
// record.sportProject = _dialogSportProject;
|
||||
// record.sportDuration = _dialogSportDuration.toInt();
|
||||
// break;
|
||||
// case RecordType.sleep:
|
||||
// // 处理睡眠数据
|
||||
// break;
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
|
||||
Widget _buildDialogTitle(String title) {
|
||||
return Text(
|
||||
title,
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Theme.of(context).colorScheme.primary,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildFiledTitle(String title) {
|
||||
return Text(title, style: TextStyle(fontSize: 16));
|
||||
}
|
||||
|
||||
Widget _buildDialogContent(RecordType type) {
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
|
||||
switch (type) {
|
||||
case RecordType.weight:
|
||||
return Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
_buildDialogTitle('记录体重'),
|
||||
SizedBox(height: 20),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
_buildFiledTitle('体重:'),
|
||||
SizedBox(width: 10),
|
||||
NumberInput(
|
||||
value: recordForm.weight!,
|
||||
min: 0,
|
||||
max: 100,
|
||||
step: 0.1,
|
||||
unit: 'Kg',
|
||||
onChanged:
|
||||
(value) => setState(() => recordForm.weight = value!),
|
||||
),
|
||||
],
|
||||
),
|
||||
SizedBox(height: 20),
|
||||
_buildDialogButton(type),
|
||||
],
|
||||
);
|
||||
case RecordType.sport:
|
||||
return Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
_buildDialogTitle('记录运动'),
|
||||
SizedBox(height: 24),
|
||||
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
_buildFiledTitle('运动类型:'),
|
||||
SizedBox(width: 10),
|
||||
DropdownSelect(
|
||||
value: recordForm.sportProject!,
|
||||
items: ['羽毛球', '乒乓球', '篮球', '跑步'],
|
||||
onChanged: (newValue) {
|
||||
setState(() {
|
||||
recordForm.sportProject = newValue!;
|
||||
});
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
SizedBox(height: 20),
|
||||
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
// 运动时长
|
||||
_buildFiledTitle('运动时长:'),
|
||||
SizedBox(width: 10),
|
||||
NumberInput(
|
||||
value: recordForm.sportDuration,
|
||||
min: 5,
|
||||
max: 240,
|
||||
step: 5,
|
||||
unit: '分钟',
|
||||
onChanged:
|
||||
(value) =>
|
||||
setState(() => recordForm.sportDuration = value!),
|
||||
),
|
||||
],
|
||||
),
|
||||
SizedBox(height: 24),
|
||||
_buildDialogButton(type),
|
||||
],
|
||||
);
|
||||
case RecordType.sleep:
|
||||
return Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
_buildFiledTitle('记录睡眠'),
|
||||
SizedBox(height: 20),
|
||||
// 这里添加睡眠相关的输入控件
|
||||
SizedBox(height: 20),
|
||||
_buildDialogButton(type),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void onTapRecord(RecordType type) {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder:
|
||||
(context) => Dialog(
|
||||
child: Padding(
|
||||
padding: EdgeInsets.all(20),
|
||||
child: _buildDialogContent(type),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildRecordContent(RecordType type) {
|
||||
if (!_checkHasRecordValue(type)) {
|
||||
return Text(
|
||||
"尚未记录今日数据,点击添加",
|
||||
style: TextStyle(fontSize: 12, color: Colors.grey[500]),
|
||||
);
|
||||
}
|
||||
|
||||
late String title;
|
||||
switch (type) {
|
||||
case RecordType.weight:
|
||||
title = "${record.weight} kg";
|
||||
case RecordType.sport:
|
||||
title = "${record.sportProject} · ${record.sportDuration}分钟";
|
||||
case RecordType.sleep:
|
||||
title =
|
||||
"入睡 ${record.sleepStartTime} · 起床 ${record.sleepEndTime} · ${record.sleepDuration}分钟";
|
||||
}
|
||||
|
||||
return Text(
|
||||
title,
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: Colors.grey[800],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
188
lib/widget/select_input.dart
Normal file
188
lib/widget/select_input.dart
Normal file
@@ -0,0 +1,188 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class SelectInputField extends StatefulWidget {
|
||||
final List<String> options;
|
||||
final String? value;
|
||||
final ValueChanged<String?>? onChanged;
|
||||
final String hintText;
|
||||
final String labelText;
|
||||
final bool enabled;
|
||||
final EdgeInsetsGeometry? padding;
|
||||
final InputBorder? border;
|
||||
|
||||
const SelectInputField({
|
||||
Key? key,
|
||||
required this.options,
|
||||
this.value,
|
||||
this.onChanged,
|
||||
this.hintText = '请选择或输入',
|
||||
this.labelText = '',
|
||||
this.enabled = true,
|
||||
this.padding,
|
||||
this.border,
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<SelectInputField> createState() => _SelectInputFieldState();
|
||||
}
|
||||
|
||||
class _SelectInputFieldState extends State<SelectInputField> {
|
||||
final TextEditingController _controller = TextEditingController();
|
||||
final FocusNode _focusNode = FocusNode();
|
||||
bool _showDropdown = false;
|
||||
String? _selectedValue;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_selectedValue = widget.value;
|
||||
_controller.text = widget.value ?? '';
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(SelectInputField oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (widget.value != oldWidget.value) {
|
||||
_selectedValue = widget.value;
|
||||
_controller.text = widget.value ?? '';
|
||||
}
|
||||
}
|
||||
|
||||
void _onTextChanged(String text) {
|
||||
setState(() {
|
||||
_selectedValue = text.isEmpty ? null : text;
|
||||
_showDropdown = text.isNotEmpty;
|
||||
});
|
||||
widget.onChanged?.call(text.isEmpty ? null : text);
|
||||
}
|
||||
|
||||
void _onOptionSelected(String option) {
|
||||
setState(() {
|
||||
_selectedValue = option;
|
||||
_controller.text = option;
|
||||
_showDropdown = false;
|
||||
});
|
||||
widget.onChanged?.call(option);
|
||||
_focusNode.unfocus();
|
||||
}
|
||||
|
||||
void _onFieldTap() {
|
||||
setState(() {
|
||||
_showDropdown = !_showDropdown;
|
||||
});
|
||||
}
|
||||
|
||||
void _onFocusChange(bool hasFocus) {
|
||||
setState(() {
|
||||
_showDropdown = hasFocus;
|
||||
});
|
||||
}
|
||||
|
||||
List<String> get _filteredOptions {
|
||||
if (_controller.text.isEmpty) {
|
||||
return widget.options;
|
||||
}
|
||||
return widget.options
|
||||
.where((option) =>
|
||||
option.toLowerCase().contains(_controller.text.toLowerCase()))
|
||||
.toList();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
if (widget.labelText.isNotEmpty) ...[
|
||||
Text(
|
||||
widget.labelText,
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: Colors.grey[700],
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
],
|
||||
Stack(
|
||||
children: [
|
||||
TextField(
|
||||
controller: _controller,
|
||||
focusNode: _focusNode,
|
||||
onChanged: _onTextChanged,
|
||||
onTap: _onFieldTap,
|
||||
enabled: widget.enabled,
|
||||
decoration: InputDecoration(
|
||||
hintText: widget.hintText,
|
||||
border: widget.border ?? const OutlineInputBorder(),
|
||||
contentPadding: const EdgeInsets.symmetric(
|
||||
horizontal: 12,
|
||||
vertical: 14,
|
||||
),
|
||||
suffixIcon: IconButton(
|
||||
icon: Icon(
|
||||
_showDropdown ? Icons.arrow_drop_up : Icons.arrow_drop_down,
|
||||
color: Colors.grey[600],
|
||||
),
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
_showDropdown = !_showDropdown;
|
||||
});
|
||||
if (_showDropdown) {
|
||||
_focusNode.requestFocus();
|
||||
} else {
|
||||
_focusNode.unfocus();
|
||||
}
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
if (_showDropdown && _filteredOptions.isNotEmpty)
|
||||
Positioned(
|
||||
top: 60,
|
||||
left: 0,
|
||||
right: 0,
|
||||
child: _buildDropdownList(),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildDropdownList() {
|
||||
return Material(
|
||||
elevation: 4,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
child: Container(
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.white,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(color: Colors.grey[300]!),
|
||||
),
|
||||
constraints: const BoxConstraints(maxHeight: 200),
|
||||
child: ListView.builder(
|
||||
shrinkWrap: true,
|
||||
padding: EdgeInsets.zero,
|
||||
itemCount: _filteredOptions.length,
|
||||
itemBuilder: (context, index) {
|
||||
final option = _filteredOptions[index];
|
||||
return ListTile(
|
||||
title: Text(option),
|
||||
onTap: () => _onOptionSelected(option),
|
||||
dense: true,
|
||||
visualDensity: VisualDensity.compact,
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_controller.dispose();
|
||||
_focusNode.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user