flutterdart

Make flutter DropDownMenu text visible


How can I make this flutter DropDownMenu text visible? These two photos show the desired look and how they currently look:

Desired look

Design

Actual look

Outcome

My main issue is that the text in the drop down menu is covered by the background of the left icon and the right drop down arrow.

I have made it smaller to fit using the answer: Make Flutter DropdownMenu Smaller

DropdownMenu<int>(
  inputDecorationTheme: InputDecorationTheme(
    fillColor: BrandingColours.background,
    filled: true,
    border: OutlineInputBorder(
      borderRadius: BorderRadius.circular(15),
      borderSide: BorderSide(color: BrandingColours.foreground, width: 3),
    ),
    enabledBorder: OutlineInputBorder(
      borderRadius: BorderRadius.circular(15),
      borderSide: BorderSide(color: BrandingColours.foreground, width: 3),
    ),
    focusedBorder: OutlineInputBorder(
      borderRadius: BorderRadius.circular(15),
      borderSide: BorderSide(color: BrandingColours.foreground, width: 3),
    ),
    contentPadding: EdgeInsets.symmetric(horizontal: 0, vertical: 0),
      constraints: BoxConstraints.tight(const Size.fromHeight(50))
  ),
  initialSelection: widget.recipe.servings,
  leadingIcon: Icon(Icons.star_rate, color: BrandingColours.white,),
  menuHeight: 200,
  dropdownMenuEntries: [-1,1,2,3,4,5,6,7,8,9,10].map((v) {
    if (v != -1) {
      return DropdownMenuEntry(
        value: v,
        label: v.toString(),
      );
    } else {
      return DropdownMenuEntry(value: -1, label: 'N/A');
    }
  }).toList(),
  textStyle: Branding.R20,
),

I've tried changing the background colour to transparent but it just makes it darker. I've also tried making the icons smaller and messing around with the inputDecorationTheme but haven't had any luck.


Solution

  • The solution would be to play around with the values, and set both:

    Here's an image, (with very small dimensions) you can see that the button label "1" is still there, in the green button. vs the red button, where the lable "1" isn't:

    enter image description here

    Here's a code example demonstrating the above image:

    import 'package:flutter/material.dart';
    
    void main() {
      runApp(const MyApp());
    }
    
    class MyApp extends StatelessWidget {
      const MyApp({super.key});
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          home: Scaffold(
            body: Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  SizedBox(
                    width: 200,
                    child: DropdownMenu<int>(
                      inputDecorationTheme: InputDecorationTheme(
                        fillColor: Colors.red,
                        isDense: true,
                        filled: true,
                        border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(15),
                          borderSide: BorderSide(color: Colors.red, width: 3),
                        ),
                        enabledBorder: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(15),
                          borderSide: BorderSide(color: Colors.red, width: 3),
                        ),
                        focusedBorder: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(15),
                          borderSide: BorderSide(color: Colors.red, width: 3),
                        ),
                        contentPadding: EdgeInsets.symmetric(horizontal: 12, vertical: 8),
                        // constraints: BoxConstraints.tight(const Size.fromHeight(50)),
                      ),
                      initialSelection: 1,
                      leadingIcon: Icon(Icons.star_rate, color: Colors.black),
                      menuHeight: 200,
                      dropdownMenuEntries: [-1,1,2,3,4,5,6,7,8,9,10].map((v) {
                        if (v != -1) {
                          return DropdownMenuEntry(value: v, label: v.toString());
                        } else {
                          return DropdownMenuEntry(value: -1, label: 'N/A');
                        }
                      }).toList(),
                      textStyle: TextStyle(fontSize: 20),
                    ),
                  ),
    
                  // Solution Example
                  SizedBox(
                    width: 200,
                    child: DropdownMenu<int>(
                      menuStyle: MenuStyle(
                        padding: WidgetStatePropertyAll<EdgeInsetsGeometry>(
                          EdgeInsets.symmetric(horizontal: 12),
                        ),
                      ),
                      inputDecorationTheme: InputDecorationTheme(
                        fillColor: Colors.green,
                        isDense: true,
                        filled: true,
                        contentPadding: EdgeInsets.symmetric(horizontal: 12),
                        prefixIconConstraints: const BoxConstraints(minWidth: 24, maxWidth: 24, minHeight: 24, maxHeight: 24),
                        suffixIconConstraints: const BoxConstraints(minWidth: 30, maxWidth: 30, minHeight: 30, maxHeight: 30),
                        border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(15),
                          borderSide: BorderSide(color: Colors.green, width: 3),
                        ),
                        enabledBorder: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(15),
                          borderSide: BorderSide(color: Colors.green, width: 3),
                        ),
                        focusedBorder: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(15),
                          borderSide: BorderSide(color: Colors.green, width: 3),
                        ),
                      ),
                      initialSelection: 1,
                      leadingIcon: Icon(Icons.star_rate, color: Colors.black, size: 16),
                      menuHeight: 200,
                      dropdownMenuEntries: [-1,1,2,3,4,5,6,7,8,9,10].map((v) {
                        if (v != -1) {
                          return DropdownMenuEntry(value: v, label: v.toString(),);
                        } else {
                          return DropdownMenuEntry(value: -1, label: 'N/A');
                        }
                      }).toList(),
                      textStyle: TextStyle(fontSize: 20),
                    ),
                  ),
                ],
              ),
            ),
          ),
        );
      }
    }