4

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

mirkancal
  • 4,762
  • 7
  • 37
  • 75

3 Answers3

3

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

Samuel Olufemi
  • 715
  • 2
  • 12
0

You can use Overlay widget to create your custom overlay. I found this guide for you.

Tuan
  • 2,033
  • 1
  • 9
  • 16
0

Try below code

import 'package:flutter/material.dart';

void main() {
runApp(
 MaterialApp(
  debugShowCheckedModeBanner: false,
  
  home: MyApp(),
   ),
  );
}

class MyApp extends StatelessWidget {
 @override
 Widget build(BuildContext context) {
  return HomeScreen();
 }
}

class HomeScreen extends StatefulWidget {
 @override
 HomeScreenState createState() => HomeScreenState();
}

class HomeScreenState extends State<HomeScreen> {
 String dropdownValue;
 List<Product> products = [
  Product(name: 'Chamipons leauge', type: 'europe'),
  Product(name: 'Europa league', type: 'europe'),
  Product(name: 'Permier league', type: 'england'),
  Product(name: 'Championship', type: 'england'),
  Product(name: 'league one', type: 'england'),
  Product(name: 'league two', type: 'england'),
  Product(name: 'national league', type: 'england'),
 ];

 @override
void initState() {
 super.initState();
 }

 @override
 Widget build(BuildContext context) {
 return Scaffold(
  appBar: AppBar(title: Text('text')),
   body: Column(
    children: [
      Text('test'),
      Expanded(
        child: DropdownButton<String>(
          value: dropdownValue,
          items: products.map((value) {
            return DropdownMenuItem(
              value: value.name,
              child: value.type == 'data'
                  ? Text(value.name)
                  : Divider(
                      color: Colors.red,
                      thickness: 3,
                    ),
            );
          }).toList(),
          onChanged: (newValue) {
            
            setState(() {
              
                dropdownValue = newValue;
              
            });
            print('$newValue $dropdownValue');
            
          },
        ),
      ),
    ],
  ),
);
}
}

class Product {
 String name;
 String type;

Product({this.name, this.type});
}
Piyush Kumar
  • 460
  • 2
  • 7