如何在flutter中将子级的可点击按钮添加到DropdownMenuItem中?

时间:2019-09-13 09:58:16

标签: flutter

我正在尝试将DateTimePicker添加到dropdown menu,以便用户可以通过它选择特定的dateDateTimePickerdropDropdownMenuItem后如何显示clicked?我已经在下面添加了代码。

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

class CustomDropDown extends StatefulWidget {
  DateTime selectedEndDate = new DateTime.now();

  @override
  _CustomDropDownState createState() => _CustomDropDownState();
}

class _CustomDropDownState extends State<CustomDropDown> {
  var _value = '1';

  DateTime selectedDate = DateTime.now();

  Future<Null> _selectDate(BuildContext context) async {
    final DateTime picked = await showDatePicker(
        context: context,
        initialDate: selectedDate,
        firstDate: DateTime(2015, 8),
        lastDate: DateTime(2101));
    if (picked != null && picked != selectedDate)
      setState(() {
        selectedDate = picked;
        widget.selectedEndDate = picked;
      });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: DropdownButton(
      items: [
        DropdownMenuItem(
          value: "1",
          child: InkWell(
            child: Text(
              'Forever',
              style: TextStyle(color: Colors.teal, fontSize: 23),
            ),
            onTap: () => (widget.selectedEndDate = DateTime(2100)),
          ),
        ),
        DropdownMenuItem(
          value: "2",
          child: InkWell(
            onTap: () => _selectDate(context),
            child: Text(
              'Select end date: ${selectedDate.day}/${selectedDate.month}/${selectedDate.year}',
              style: TextStyle(color: Colors.teal, fontSize: 23),
            ),
          ),
        ),
      ],
      onChanged: (val) => setState(() {
        _value = val;
      }),
      value: _value,
      style: TextStyle(color: Colors.teal, fontSize: 23),
      isExpanded: true,
    ));
  }
}

1 个答案:

答案 0 :(得分:0)

您可以检查在onChange()中选择的值:

onChanged: (val) => 
   setState(() {
   if (val == "2") {
     //Display select date
   }
   _value = val;
})