Home > Mobile >  Flutter DropdownMenuItem delete
Flutter DropdownMenuItem delete

Time:02-14

I'm developing a combobox using Flutter. combobox needs to be this month and next months. should write month name in combobox and return the number of the selected month. How can I do this in the shortest way? I developed 12 months as DropdownMenuItem. I gave the current month as the default value. So far everything is fine. but how can I make the items of the past months invisible?

CodePudding user response:

I used this code for a similar problem. This solution includes intl package but you can change the code in list generation for your purpose.

import 'package:intl/intl.dart';

void main(){
  var today = DateTime.now();
  
  var length = 12 - today.month   1;  // I added  1 because I want to include current month too
  
  List validMonths = List.generate(length,(index){
    return DateFormat('MMMM').format(DateTime(0, (today.month   index)));
  });
  
  print(validMonths);
}
  • Related