0

class MonthYearPicker extends StatefulWidget {
  @override
  _MonthYearPickerState createState() => _MonthYearPickerState();
}

class _MonthYearPickerState extends State<MonthYearPicker> {
  List<DropdownMenuItem> monthList = const [
    DropdownMenuItem(child: Text('January'), value: 1),
    DropdownMenuItem(child: Text('February'), value: 2)
  ];

final List<DropdownMenuItem> yearList = [
    DropdownMenuItem(child: Text((DateTime.now().year - 2).toString()), value: -2),
    DropdownMenuItem(child: Text((DateTime.now().year).toString()), value: 0),
  ];

  int _selectedMonth;
  int _selectedYear;

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceAround,
      children: <Widget>[
        DropdownButton(
          items: monthList,
          value: _selectedMonth,
          hint: Text('---Month---'),
      onChanged: (value) {
            _selectedMonth = value;
            setState(() {});
          },
    ),
        DropdownButton(
      items: yearList,
          value: _selectedYear,
          hint: Text('---Year---'),
          onChanged: (value) {
        _selectedYear = value;
            setState(() {});
          },
    ),
      ],
    );
  }
}

        body: Column(
          children: <Widget>[
        MonthYearPicker(),
            Center(
          child: RaisedButton(
                child: Text('Get Date'),
            onPressed: () {
              //How to access selected month and year from MonthYearPicker?
                },
          ),
        )
          ],

|

    1

    0

    MonthYearPicker((picked){
     setState((){_pickedMonth = picked});
    }
    

    |