1// Copyright 2014 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import 'package:flutter/material.dart';
6
7/// Flutter code sample for [DropdownButton.style].
8
9void main() => runApp(const DropdownButtonApp());
10
11class DropdownButtonApp extends StatelessWidget {
12 const DropdownButtonApp({super.key});
13
14 @override
15 Widget build(BuildContext context) {
16 return MaterialApp(
17 home: Scaffold(
18 appBar: AppBar(title: const Text('DropdownButton Sample')),
19 body: const DropdownButtonExample(),
20 ),
21 );
22 }
23}
24
25class DropdownButtonExample extends StatefulWidget {
26 const DropdownButtonExample({super.key});
27
28 @override
29 State<DropdownButtonExample> createState() => _DropdownButtonExampleState();
30}
31
32class _DropdownButtonExampleState extends State<DropdownButtonExample> {
33 List<String> options = <String>['One', 'Two', 'Three', 'Four'];
34 String dropdownValue = 'One';
35
36 @override
37 Widget build(BuildContext context) {
38 return Container(
39 alignment: Alignment.center,
40 color: Colors.blue,
41 child: DropdownButton<String>(
42 value: dropdownValue,
43 onChanged: (String? value) {
44 // This is called when the user selects an item.
45 setState(() {
46 dropdownValue = value!;
47 });
48 },
49 style: const TextStyle(color: Colors.blue),
50 selectedItemBuilder: (BuildContext context) {
51 // This is the widget that will be shown when you select an item.
52 // Here custom text style, alignment and layout size can be applied
53 // to selected item string.
54 return options.map((String value) {
55 return Align(
56 alignment: Alignment.centerLeft,
57 child: Text(dropdownValue, style: const TextStyle(color: Colors.white)),
58 );
59 }).toList();
60 },
61 items:
62 options.map<DropdownMenuItem<String>>((String value) {
63 return DropdownMenuItem<String>(value: value, child: Text(value));
64 }).toList(),
65 ),
66 );
67 }
68}
69

Provided by KDAB

Privacy Policy
Learn more about Flutter for embedded and desktop on industrialflutter.com