简体   繁体   English

在 flutter 中制作 CheckBoxListTile 的 DropDownButton

[英]Making a DropDownButton of CheckBoxListTile in flutter

I want to make some DropdownButtons that filter data to show a specific result.我想制作一些 DropdownButtons 来过滤数据以显示特定结果。 I made the DropdownButton and insert the items as DropdownMenuItem, and the child of it it's CheckboxListTile..我制作了 DropdownButton 并将项目作为 DropdownMenuItem 插入,它的子项是 CheckboxListTile ..

But unfortunately it shows me this error但不幸的是它告诉我这个错误

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'

This is my code:这是我的代码:

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!;
                      });
                    },
                  ),
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}

So.. Is there anyway to solve this, or anything can help with a DropDownButton that has multiple choice options?所以.. 有没有办法解决这个问题,或者有什么可以帮助具有多种选择选项的 DropDownButton?

Here you go!, This should do the trick,给你!,这应该可以解决问题,

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;
                          });
                        }
                      }),
                ))
              ])
        ]));
  }
}

实际上,我通过制作一个特殊的 dropDownMenu 解决了这个问题,它具有自己制作的完整特殊选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM