首页 > 解决方案 > 在颤动中制作 CheckBoxListTile 的 DropDownButton

问题描述

我想制作一些过滤数据以显示特定结果的下拉按钮。我制作了 DropdownButton 并将项目插入为 DropdownMenuItem,它的子项是 CheckboxListTile ..

但不幸的是,它向我显示了这个错误

There should be exactly one item with [DropdownButton]'s value: checked. 
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value
'package:flutter/src/material/dropdown.dart':
Failed assertion: line 882 pos 15: 'items == null || items.isEmpty || value == null ||
              items.where((DropdownMenuItem<T> item) {
                return item.value == value;
              }).length == 1'

这是我的代码:

import 'package:flutter/material.dart';

class SendingMessages extends StatefulWidget {
  const SendingMessages({Key? key}) : super(key: key);

  @override
  _SendingMessagesState createState() => _SendingMessagesState();
}

class _SendingMessagesState extends State<SendingMessages> {
  String checkBoxDropped = 'Azaz';
  bool isChecked = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          'إرسال إشعارات',
          style: TextStyle(fontSize: 24.0),
        ),
        centerTitle: true,
      ),
      body: ListView(
        children: [
          Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Padding(padding: EdgeInsets.only(top: 20.0)),
              Container(
                child: Center(
                  child: DropdownButton<String>(
                    icon: const Icon(Icons.arrow_circle_down_sharp),
                    items: [
                      DropdownMenuItem(
                        child: CheckboxListTile(
                          value: isChecked,
                          onChanged: (newValue) {
                            isChecked = newValue!;
                          },
                        ),
                        value: 'Azaz',
                      ),
                      DropdownMenuItem(
                        child: CheckboxListTile(
                          value: isChecked,
                          onChanged: (newValue) {
                            isChecked = newValue!;
                          },
                        ),
                        value: 'Sarmada',
                      ),
                    ],
                    value: checkBoxDropped,
                    onChanged: (value) {
                      setState(() {
                        checkBoxDropped = value!;
                      });
                    },
                  ),
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}

所以.. 有没有办法解决这个问题,或者任何可以帮助具有多个选择选项的 DropDownButton 的东西?

标签: androidflutterdrop-down-menucheckboxlist

解决方案


给你!,这应该可以解决问题,

class SendingMessages extends StatefulWidget {
  const SendingMessages({Key? key}) : super(key: key);

  @override
  _SendingMessagesState createState() => _SendingMessagesState();
}

class _SendingMessagesState extends State<SendingMessages> {
  List<String> items = ['Azaz', 'Sarmada'];
  String? _selectedItem;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(
            'إرسال إشعارات',
            style: TextStyle(fontSize: 24.0),
          ),
          centerTitle: true,
        ),
        body: ListView(children: [
          Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Padding(padding: EdgeInsets.only(top: 20.0)),
                Container(
                    child: Center(
                  child: DropdownButton<String>(
                      icon: const Icon(Icons.arrow_circle_down_sharp),
                      value: _selectedItem,
                      items:
                          items.map<DropdownMenuItem<String>>((String value) {
                        return DropdownMenuItem<String>(
                          value: value,
                          child: Container(
                              width: 100,
                              child: Text(
                                value,
                                style: TextStyle(
                                  fontSize: 15,
                                ),
                              )),
                        );
                      }).toList(),
                      onChanged: (String? value) {
                        if (value != null) {
                          setState(() {
                            _selectedItem = value;
                          });
                        }
                      }),
                ))
              ])
        ]));
  }
}

推荐阅读