by @kodeazy
flutter How to create a dropdown in dart file
-
Below is the sample code for creating and selecting the desired dropdown in flutter.
DropdownButton( value: dropdownvalue, icon: const Icon(Icons.keyboard_arrow_down), items: items1.map((String items1) { return DropdownMenuItem( value: items1, child: Text(items1), ); }).toList(), onChanged: (String? selectedValue) { setState(() { dropdownvalue = selectedValue!; }); })
-
Below is the example program of creating dropdown using flutter.
import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { // TODO: implement build return MaterialApp( title: 'Flutter DropDown Example', theme: ThemeData(primarySwatch: Colors.green), home: DropDownExample(), ); } } class DropDownExample extends StatefulWidget { @override State<DropDownExample> createState() => SampleDropDown(); // TODO: implement createState } class SampleDropDown extends State<DropDownExample> { String dropdownvalue = 'Audi'; var items1 = [ 'Audi', 'BMW', 'Ford', 'Ferrari', ]; Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("DropDown Example"), ), body: Center( child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [ DropdownButton( value: dropdownvalue, icon: const Icon(Icons.keyboard_arrow_down), items: items1.map((String items1) { return DropdownMenuItem( value: items1, child: Text(items1), ); }).toList(), onChanged: (String? selectedValue) { setState(() { dropdownvalue = selectedValue!; }); }) ])), ); } }
Output: