dropdown_button.selected_item_builder.0.dart 2.7 KB
Newer Older
1 2 3 4
// 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.

5
/// Flutter code sample for [DropdownButton.selectedItemBuilder].
6 7 8

import 'package:flutter/material.dart';

9 10 11 12 13 14 15
Map<String, String> cities = <String, String>{
  'New York': 'NYC',
  'Los Angeles': 'LA',
  'San Francisco': 'SF',
  'Chicago': 'CH',
  'Miami': 'MI',
};
16

17
void main() => runApp(const DropdownButtonApp());
18

19
class DropdownButtonApp extends StatelessWidget {
20
  const DropdownButtonApp({super.key});
21 22 23 24 25

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
26 27
        appBar: AppBar(title: const Text('DropdownButton Sample')),
        body: const Center(child: DropdownButtonExample()),
28 29 30 31 32
      ),
    );
  }
}

33
class DropdownButtonExample extends StatefulWidget {
34
  const DropdownButtonExample({super.key});
35 36

  @override
37
  State<DropdownButtonExample> createState() => _DropdownButtonExampleState();
38 39
}

40 41
class _DropdownButtonExampleState extends State<DropdownButtonExample> {
  String selectedItem = cities.keys.first;
42 43 44

  @override
  Widget build(BuildContext context) {
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    return Center(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Text('Select a city:', style: Theme.of(context).textTheme.bodyLarge),
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 8.0),
            child: DropdownButton<String>(
              value: selectedItem,
              onChanged: (String? value) {
                // This is called when the user selects an item.
                setState(() => selectedItem = value!);
              },
              selectedItemBuilder: (BuildContext context) {
                return cities.values.map<Widget>((String item) {
                  // This is the widget that will be shown when you select an item.
                  // Here custom text style, alignment and layout size can be applied
                  // to selected item string.
                  return Container(
                    alignment:Alignment.centerLeft,
                    constraints: const BoxConstraints(minWidth: 100),
                    child: Text(
                      item,
                      style: const TextStyle(color: Colors.blue, fontWeight: FontWeight.w600),
                    ),
                  );
                }).toList();
              },
              items: cities.keys.map<DropdownMenuItem<String>>((String item) {
                return DropdownMenuItem<String>(
                  value: item,
                  child: Text(item),
                );
              }).toList(),
            ),
          ),
        ],
82 83 84 85
      ),
    );
  }
}