fix:日历选择交互
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:shadcn_ui/shadcn_ui.dart';
|
||||
|
||||
@@ -50,6 +51,39 @@ class _AssetEditPageState extends ConsumerState<AssetEditPage> {
|
||||
_channelCtrl = TextEditingController(text: _holding.channel ?? '');
|
||||
}
|
||||
|
||||
Future<void> pickDate(BuildContext context) async {
|
||||
final today = DateTime.now();
|
||||
final parsed = _parseDate(_dateCtrl.text.trim()) ?? _holding.purchaseDate;
|
||||
final initialDate = parsed == null
|
||||
? today
|
||||
: parsed.isBefore(DateTime(2000))
|
||||
? DateTime(2000)
|
||||
: parsed.isAfter(today)
|
||||
? today
|
||||
: parsed;
|
||||
final picked = await showDatePicker(
|
||||
context: context,
|
||||
initialDate: initialDate,
|
||||
firstDate: DateTime(2000),
|
||||
lastDate: today,
|
||||
initialEntryMode: DatePickerEntryMode.calendarOnly,
|
||||
locale: const Locale('zh'),
|
||||
);
|
||||
if (picked == null || !mounted) return;
|
||||
setState(() {
|
||||
_dateCtrl.text = _formatDate(picked);
|
||||
});
|
||||
}
|
||||
|
||||
String _formatDate(DateTime value) {
|
||||
return '${value.year}-${value.month.toString().padLeft(2, '0')}-${value.day.toString().padLeft(2, '0')}';
|
||||
}
|
||||
|
||||
DateTime? _parseDate(String value) {
|
||||
if (value.isEmpty) return null;
|
||||
return DateTime.tryParse(value);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_weightCtrl.dispose();
|
||||
@@ -214,6 +248,9 @@ class _AssetEditPageState extends ConsumerState<AssetEditPage> {
|
||||
keyboardType: const TextInputType.numberWithOptions(
|
||||
decimal: true,
|
||||
),
|
||||
inputFormatters: [
|
||||
FilteringTextInputFormatter.allow(RegExp(r'[0-9.]')),
|
||||
],
|
||||
textAlign: TextAlign.right,
|
||||
decoration: const InputDecoration(
|
||||
border: InputBorder.none,
|
||||
@@ -227,6 +264,8 @@ class _AssetEditPageState extends ConsumerState<AssetEditPage> {
|
||||
label: '购买日期',
|
||||
child: TextField(
|
||||
controller: _dateCtrl,
|
||||
readOnly: true,
|
||||
onTap: () => pickDate(context),
|
||||
textAlign: TextAlign.right,
|
||||
decoration: const InputDecoration(
|
||||
border: InputBorder.none,
|
||||
|
||||
Reference in New Issue
Block a user