Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a dropdown menu with sections? [duplicate]

Tags:

flutter

I'm trying to create something like below, what's the best way to create those sections inside dropdown with Flutter?

enter image description here

like image 786
mirkancal Avatar asked Nov 02 '25 06:11

mirkancal


1 Answers

you could try implementing it this way

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: MyHomePage(title: 'DropDown'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String? dropdownValue;
  List<Product> products = [
    Product(name: 'Europe', type: 'sep'),
    Product(name: 'Championship league', type: 'data'),
    Product(name: 'Europa league', type: 'data'),
    Product(name: 'England', type: 'sep'),
    Product(name: 'Premier league', type: 'data'),
    Product(name: 'league one', type: 'data'),
    Product(name: 'league Two', type: 'data'),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: SizedBox(
        width: 500.0,
        child: DropdownButtonHideUnderline(
          child: ButtonTheme(
            alignedDropdown: true,
            child: DropdownButton<String>(
              hint: const Text('Championship'),
              value: dropdownValue,
              items: products.map((value) {
                return DropdownMenuItem(
                    enabled: value.type == 'sep' ? false : true,
                    value: value.name,
                    child: value.type == 'data'
                        ? Text(value.name)
                        : DropdownMenuItemSeparator(name: value.name));
              }).toList(),
              onChanged: (newValue) {
                setState(() {
                  dropdownValue = newValue!;
                });
              },
            ),
          ),
        ),
      ),
    );
  }
}

class Product {
  final String name;
  final String type;

  Product({required this.name, required this.type});
}


class DropdownMenuItemSeparator<T> extends DropdownMenuItem<T> {
  final String name;

  DropdownMenuItemSeparator({required this.name, Key? key})
      : super(
          key: key,
          child: Text(
            name,
            style: const TextStyle(
                fontSize: 18, fontWeight: FontWeight.bold, color: Colors.black),
          ), // Trick the assertion.
        );
}

enter image description here

like image 138
Samuel Olufemi Avatar answered Nov 03 '25 21:11

Samuel Olufemi