flutter 请支持更改扑动应用程序中的日期[已关闭]

v09wglhw  于 2023-02-13  发布在  Flutter
关注(0)|答案(1)|浏览(90)

15小时前关门了。
Improve this question

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

class EdidPickDate extends StatefulWidget {
EdidPickDate({
super.key,
this.onTap,
required this.hintText,
this.onChanged,
this.labelText,
 });

final void Function()? onTap;
final String? hintText;
Function(String)? onChanged;
final String? labelText;

@override
State<EdidPickDate> createState() => _EdidPickDateState();
}

class _EdidPickDateState extends State<EdidPickDate> {

最终文本编辑控制器?_日期=文本编辑控制器();函数(字符串)?onChanged;
@override小部件构建(BuildContext上下文){return填充(填充:边缘插入对称(垂直:8),子项:文本表单字段(控制器:_日期、装饰:输入装饰(图标:常量图标(图标,日历_今天_概述),标签文本:小部件。标签文本、提示文本:小部件. hint文本,启用边框:常量下划线输入边框(边框侧:边框(宽度:3,颜色:颜色.绿色强调色),),),on点击:()async {日期时间?pickeddate =等待显示日期选择器(上下文:上下文,姓名首字母缩写日期:www.example.com(),首次日期:DateTime.now日期时间(2300)); DateTime(1920), lastDate: DateTime(2300));

if (pickeddate != null) {
        setState(() {
        _date?.text = DateFormat('dd-MM-yyyy').format(pickeddate);
          
        });
      }
    },
    onChanged: onChanged,
  ),
);

}}
'类edid_Person_Body扩展了状态控件{const edid_Person_Body({Key?键,此. person是必需的,}):超级(键:键);最终PersonModel人员;
@override状态创建状态()=〉_edid_个人_身体状态();<edid_Person_Body>} }
扩展状态{文本编辑控制器控制器=文本编辑控制器();<edid_Person_Body> { TextEditingController controller = TextEditingController();
字符串?巴思戴;
@override小部件构建(BuildContext上下文){return表单(子:填充(填充:常量边缘插入。全部(8.0),子对象:列表视图(子视图:[填充(填充:仅常量边插入(右:20),儿童:行(子项:[列(子列:[

EdidPickDate(
          onChanged: (value) {
            barthday = value;
          },
          labelText: 'brithday',
          hintText: widget.person.barthday,
        ),
      

        CustomAddBotton(
          texet: 'eddid',
          onTap: () {
            
            widget.person.barthday = barthday ?? widget.person.barthday;
      

            widget.person.save();
            BlocProvider.of<PersonCubit>(context).fetchAllPerson();

            Navigator.pop(context);
            debugPrint("ok$barthday");
          },
        ),
      ],
    ),
  ),
);

}}

omjgkv6w

omjgkv6w1#

onChanged只有在用户通过键盘输入时才会被调用,因此您可以在onTap日期picked时调用onChanged
此外,您还在_EdidPickDateState中取消了onChanged的分配。

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

class EdidPickDate extends StatefulWidget {
  EdidPickDate({
    super.key,
    this.onTap,
    required this.hintText,
    this.onChanged,
    this.labelText,
  });

  final void Function()? onTap;
  final String? hintText;
  Function(String)? onChanged;
  final String? labelText;

  @override
  State<EdidPickDate> createState() => _EdidPickDateState();
}

class _EdidPickDateState extends State<EdidPickDate> {
  final TextEditingController _date = TextEditingController();

  @override Widget build(BuildContext context) {
    return Padding(padding: EdgeInsets.symmetric(vertical: 8),
      child: TextFormField(
        controller: _date,
        decoration: InputDecoration(icon: const Icon(Icons.calendar_today_outlined),
          labelText: widget.labelText,
          hintText: widget.hintText,
          enabledBorder: const UnderlineInputBorder(borderSide: BorderSide(width: 3, color: Colors.greenAccent),),),
        onTap: () async {
          DateTime? pickeddate = await showDatePicker(
              context: context, initialDate: DateTime.now(), firstDate: DateTime(1920), lastDate: DateTime(2300));

          if (pickeddate != null) {
            _date.text = DateFormat('dd-MM-yyyy').format(pickeddate);
          }
          widget.onChanged?.call(_date.text);
        },
      ),
    );
  }
}

相关问题