在下面的部分中,我想用什么方法来创建一些东西?

enter image description here

推荐答案

你可以试着用这种方式实现它

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: MyHomePage(title: 'DropDown'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String? dropdownValue;
  List<Product> products = [
    Product(name: 'Europe', type: 'sep'),
    Product(name: 'Championship league', type: 'data'),
    Product(name: 'Europa league', type: 'data'),
    Product(name: 'England', type: 'sep'),
    Product(name: 'Premier league', type: 'data'),
    Product(name: 'league one', type: 'data'),
    Product(name: 'league Two', type: 'data'),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: SizedBox(
        width: 500.0,
        child: DropdownButtonHideUnderline(
          child: ButtonTheme(
            alignedDropdown: true,
            child: DropdownButton<String>(
              hint: const Text('Championship'),
              value: dropdownValue,
              items: products.map((value) {
                return DropdownMenuItem(
                    enabled: value.type == 'sep' ? false : true,
                    value: value.name,
                    child: value.type == 'data'
                        ? Text(value.name)
                        : DropdownMenuItemSeparator(name: value.name));
              }).toList(),
              onChanged: (newValue) {
                setState(() {
                  dropdownValue = newValue!;
                });
              },
            ),
          ),
        ),
      ),
    );
  }
}

class Product {
  final String name;
  final String type;

  Product({required this.name, required this.type});
}


class DropdownMenuItemSeparator<T> extends DropdownMenuItem<T> {
  final String name;

  DropdownMenuItemSeparator({required this.name, Key? key})
      : super(
          key: key,
          child: Text(
            name,
            style: const TextStyle(
                fontSize: 18, fontWeight: FontWeight.bold, color: Colors.black),
          ), // Trick the assertion.
        );
}

enter image description here

Flutter相关问答推荐

Riverpod状态管理-在调用请求时更新用户界面

如何在表格日历标题下方添加文本?

来自FirebaseAuth的错误DisplayName和邮箱在Flutter应用程序中给出错误

如何在Ffltter 3.16中设置标准的提升按钮主题?

如何使 dart 函数变得通用?

请求已在 flutter 中发送到 django 服务器,并且已返回响应,但条件仍然为 false

如何在flutter中更改showModalBottomSheet小部件中CheckboxListTile小部件的值?

没有为类型 'Widget' 定义运算符 '[]' .try 定义运算符[]

使用Flutter项目设置Firebase遇到困难?这些解决方案或许能帮到你!

FutureProvider 不返回数据给 UI

切换 Firebase 项目后 Flutter FirebaseAuthException 'configuration-not-found'

应用程序包含嵌入式私钥或密钥库文件

如何将 void 函数放入单独的 dart 文件的单独页面中

无法将参数类型Future Function(String?)分配给参数类型void Function(NotificationResponse)?

仅使用 Flutter 在本地环境中托管 Web 服务器

为什么 orientation == Orientation.portrait 总是正确的,尽管我的设备已经处于横向

如何在手势检测器中获得涟漪效应

Flutter:滚动一个ListWheelScrollView,无法获取选中项的状态

Flutter Desktop,如何获取windows用户配置文件名称?

在 Flutter 应用程序中全局使用 assets 文件夹