| // 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. | |
| import 'package:flutter/material.dart'; | |
| /// Flutter code sample for [DropdownButton]. | |
| const List<String> list = <String>['One', 'Two', 'Three', 'Four']; | |
| void main() => runApp(const DropdownButtonApp()); | |
| class DropdownButtonApp extends StatelessWidget { | |
| const DropdownButtonApp({super.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({super.key}); | |
| @override | |
| State<DropdownButtonExample> createState() => _DropdownButtonExampleState(); | |
| } | |
| class _DropdownButtonExampleState extends State<DropdownButtonExample> { | |
| String dropdownValue = list.first; | |
| @override | |
| Widget build(BuildContext context) { | |
| return DropdownButton<String>( | |
| 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<DropdownMenuItem<String>>((String value) { | |
| return DropdownMenuItem<String>(value: value, child: Text(value)); | |
| }).toList(), | |
| ); | |
| } | |
| } |