0

i want to make a function for init openDropDown(); when page loaded or tap on other widget

I found this in stackoverfow

but its not working maybe its outdated. how can i achieve. with flutter 2.10 here is my dropdown

DropdownButton(
                    isExpanded: true,
                    value: selectedValue,
                    items: [
                      for (var i = 0; i < this.clients.length; i++)
                        DropdownMenuItem(
                            child: Text(this.clients[i]['name'].toString() +
                                ' (' +
                                this.clients[i]['wallet'].toString() +
                                ') '),
                            value: this.clients[i]['id'].toString())
                    ],
                    onChanged: (String? newValue) {
                      setState(() {
                        selectedValue = newValue!;
                      });
                    },
                  ),
sid heart
  • 1,140
  • 1
  • 9
  • 38

1 Answers1

0

You can take advantage of GlobalKey to get this job done... Also use SchedulerBinding because in initState key would be null so you won't be able to open DropDown from null key... here is the proper code how you do that task

import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter DropDownButton',
      theme: ThemeData(
        primarySwatch: Colors.green,
      ),
      home: const MyHomePage(),
      debugShowCheckedModeBanner: false,
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final GlobalKey _dropdownButtonKey = GlobalKey();
  final Intent _intent = const ActivateIntent();

// Initial Selected Value
  String dropdownvalue = 'Item 1';

// List of items in our dropdown menu
  var items = [
    'Item 1',
    'Item 2',
    'Item 3',
    'Item 4',
    'Item 5',
  ];

  @override
  void initState() {
    super.initState();
    SchedulerBinding.instance?.addPostFrameCallback((_) {
      if (_dropdownButtonKey.currentContext != null) {
        _dropdownButtonKey.currentContext?.visitChildElements((element) {
          if (element.widget is Semantics) {
            element.visitChildElements((element) {
              if (element.widget is Actions) {
                element.visitChildElements((element) {
                  Actions.invoke(element, _intent);
                  return;
                });
              }
            });
          }
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            DropdownButton(
              autofocus: true,
              key: _dropdownButtonKey,
              isDense: true,
              isExpanded: true,
              value: dropdownvalue,
              icon: const Icon(Icons.keyboard_arrow_down),
              items: items.map((String items) {
                return DropdownMenuItem(
                  value: items,
                  child: Text(items),
                );
              }).toList(),
              onChanged: (String? newValue) {
                setState(() {
                  dropdownvalue = newValue!;
                });
              },
            ),
          ],
        ),
      ),
    );
  }
}


Arslan Kaleem
  • 1,410
  • 11
  • 25