Take the return value of a Datepicker on a texFormField with flutter

Asked

Viewed 351 times

-1

Good afternoon!

I’m starting my studies in Flutter and I created a simple form, where when clicking on a date field, it displays the datepicker, but when selecting the date the field is not filled. The code is like this:

class _FormScreenState extends State<FormScreen> {
DateTime _date = DateTime.now();

Future<Null> _selectcDate(BuildContext context) async {
final DateTime picked = await showDatePicker(
  context: context,
  initialDate: _date,
  firstDate: DateTime(1990),
  lastDate: DateTime(2030),
);
if (picked != null && picked != _date) {
  setState(() {
    _date = picked;
    print(_date.toString());
  });
   }
 }

 @override
 Widget build(BuildContext context) {
return Scaffold(
  appBar: AppBar(
    title: Text('Formulário'),
  ),
  drawer: AppDrawer(),
  body: Padding(
    padding: const EdgeInsets.all(15.0),
    child: Form(
      child: ListView(
        children: <Widget>[
          TextFormField(
            decoration: InputDecoration(
              contentPadding: EdgeInsets.only(top: 20),
              isDense: true,
              hintText: "Data",
              prefixIcon: Padding(
                padding: EdgeInsets.only(top: 15),
                child: Icon(Icons.alarm),
              )
            ),
            onTap: () {
              _selectcDate(context);
            },
          ),
        ],
         ),
       ),
     ),
   );
 }
}

1 answer

1


Try adding a controller as follows

class _FormScreenState extends State<FormScreen> {
    DateTime _date = DateTime.now();
  TextEditingController dateCtl = TextEditingController();

  Future<Null> _selectcDate(BuildContext context) async {
    final DateTime picked = await showDatePicker(
      context: context,
          initialDate: _date,
      firstDate: DateTime(1990),
      lastDate: DateTime(2030),
    );
    if (picked != null && picked != _date) {
      setState(() {
        dateCtl.text = picked.toIso8601String();
      });
    }
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Formulário'),
      ),
      drawer: AppDrawer(),
      body: Padding(
        padding: const EdgeInsets.all(15.0),
        child: Form(
          child: ListView(
            children: <Widget>[
              TextFormField(
                decoration: InputDecoration(labelText: 'Nome'),
                textInputAction: TextInputAction.next,
              ),
              TextFormField(
                decoration: InputDecoration(labelText: 'Preço'),
                textInputAction: TextInputAction.next,
                keyboardType: TextInputType.numberWithOptions(decimal: true),
              ),
              TextFormField(
                controller: dateCtl,
                decoration: InputDecoration(                  
                  contentPadding: EdgeInsets.only(top: 20),
                  isDense: true,
                  hintText: "Data",
                  prefixIcon: Padding(
                    padding: EdgeInsets.only(top: 15),
                    child: Icon(Icons.alarm),
                  ),
                ),
                onTap: () {
                  FocusScope.of(context).requestFocus(new FocusNode());
                  _selectcDate(context);
                },
              ),
            ],
          ),
        ),
      ),
    );
      }
   }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.