flutter 带有int和字符串值的下拉按钮

gijlo24d  于 2023-05-23  发布在  Flutter
关注(0)|答案(1)|浏览(148)

我试图制作一个包含int和string值的下拉按钮,但我不知道如何同时调用string和int。

编码

int selectedValue = 10;

 List<DropdownMenuItem<int>> dropdownItems = [
  DropdownMenuItem(
   value: 10,
   child: Text(
     'Small',
     style: TextStyle(
       fontSize: 20,
       fontFamily: 'Inconsolata',
       fontWeight: FontWeight.bold,
       color: Color(0xCBFDC4AB),
     ),
   ),
 ),
 const DropdownMenuItem(
   value: 20,
   child: Text(
     'Medium',
     style: TextStyle(
       fontSize: 20,
       fontFamily: 'Inconsolata',
       fontWeight: FontWeight.bold,
       color: Color(0xCBFDC4AB),
     ),
   ),
 ),
 const DropdownMenuItem(
   value: 30,
   child: Text(
     'Large',
     style: TextStyle(
       fontSize: 20,
       fontFamily: 'Inconsolata',
       fontWeight: FontWeight.bold,
       color: Color(0xCBFDC4AB),
     ),
   ),
 ),
];'

//int值的文本

Text(
                          "\$$selectedValue",
                          style: const TextStyle(
                            fontSize: 40,
                            fontFamily: 'Inconsolata',
                            fontWeight: FontWeight.bold,
                            color: Color(0xADFFF0E8),
                          ),
                        ),

//字符串值的文本

Text (value),

// dropdownbutton

DropdownButton(
                              value: selectedValue,
                              items: dropdownItems,
                              dropdownColor: Colors.brown,
                              underline: Container(
                                height: 3,
                                color: const Color(0xCBFDC4AB),
                              ),
                              icon: Icon(
                                CupertinoIcons.arrowtriangle_down_fill,
                                color: const Color(0xCBFDC4AB),
                                size: 10,
                                shadows: [
                                  Shadow(
                                    color: Colors.yellow.withOpacity(0.9),
                                    blurRadius: 10,
                                  )
                                ],
                              ),

                              onChanged: (int? newValue) {
                                setState(() {
                                  selectedValue = newValue!;
                                });
                              },
                            ),

我希望子文本是字符串值,而dropdownitem的值是tre int value。
谢谢你。

zz2j4svz

zz2j4svz1#

import 'package:flutter/material.dart';

/// Flutter code sample for [DropdownButton].

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> {
  int dropdownValue = list.first;
  int? selectedValue;

  @override
  Widget build(BuildContext context) {
    /// use either option 1 or option 2

    /// options 1
    List<DropdownData> dropdownItems = [
      DropdownData(10, 'Small'),
      DropdownData(20, 'Medium'),
      DropdownData(30, 'Large'),
    ];

    /// options 2
    List<EnumSize> dropdownItems = [
      EnumSize.small,
      EnumSize.medium,
      EnumSize.large,
    ];

    return DropdownButton(
      value: selectedValue,
      items: dropdownItems
          .map<DropdownMenuItem<int>>((e) => DropdownMenuItem(
                value: e.value,
                child: Text(
                  e.label,
                  style: const TextStyle(
                    fontSize: 20,
                    fontFamily: 'Inconsolata',
                    fontWeight: FontWeight.bold,
                    color: Color(0xCBFDC4AB),
                  ),
                ),
              ))
          .toList(),
      dropdownColor: Colors.brown,
      underline: Container(
        height: 3,
        color: const Color(0xCBFDC4AB),
      ),
      icon: const Icon(Icons.arrow_downward),
      onChanged: (int? newValue) {
        setState(() {
          selectedValue = newValue!;
          print([newValue, newValue.runtimeType]);
        });
      },
    );
  }
}

class DropdownData {
  final int value;
  final String label;
  DropdownData(this.value, this.label);
}

enum EnumSize {
  small(10, 'Small'),
  medium(20, 'Medium'),
  large(30, 'Large');

  final int value;
  final String label;
  const EnumSize(this.value, this.label);
}
  • 解释一下
DropdownButton(
    items : ... /// require `List<DropdownMenuItem<int>>`
)
  • 所以基本上它是一个DropdownMenuItem的列表,值为int
DropdownButton(
    items : dropdownItems // loop through list `dropdownItems`
          .map<DropdownMenuItem<int>>((e) => DropdownMenuItem( /// return a `DropdownMenuItem`
                value: e.value, /// with value of `int`
                child: Text( /// and a child `Widget`
                  e.label, // label of child `Widget`
                  style: const TextStyle(
                    fontSize: 20,
                    fontFamily: 'Inconsolata',
                    fontWeight: FontWeight.bold,
                    color: Color(0xCBFDC4AB),
                  ),
                ),
              ))
          .toList(), because `.map()` return a `Iterable` but we need a `List`
)
  • 了解更多:你应该伸出DropdownButton的文档。他们用视频提供了一个例子和解释。

相关问题