// Copyright 2014 The Flutter Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // Flutter code sample for DropdownButton import 'package:flutter/material.dart'; const List list = ['One', 'Two', 'Three', 'Four']; void main() => runApp(const DropdownButtonApp()); class DropdownButtonApp extends StatelessWidget { const DropdownButtonApp({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: const Text('DropdownButton Sample')), body: const Center( child: DropdownButtonExample(), ), ), ); } } class DropdownButtonExample extends StatefulWidget { const DropdownButtonExample({Key? key}) : super(key: key); @override State createState() => _DropdownButtonExampleState(); } class _DropdownButtonExampleState extends State { String dropdownValue = list.first; @override Widget build(BuildContext context) { return DropdownButton( value: dropdownValue, icon: const Icon(Icons.arrow_downward), elevation: 16, style: const TextStyle(color: Colors.deepPurple), underline: Container( height: 2, color: Colors.deepPurpleAccent, ), onChanged: (String? value) { // This is called when the user selects an item. setState(() { dropdownValue = value!; }); }, items: list.map>((String value) { return DropdownMenuItem( value: value, child: Text(value), ); }).toList(), ); } }