【问题标题】:how to select ,deselect and disable items?如何选择、取消选择和禁用项目?
【发布时间】:2021-10-29 05:58:02
【问题描述】:

我的应用程序中有卡片,每张卡片有 4 个灰色项目,如果选择该项目,则其颜色变为蓝色,其他卡片中的相同项目应该禁用,我无法使用地图集合执行此操作。所以我有 IT、DEV、TEST、HR 等项目,如果在任何一张卡中选择了 IT,那么其余卡中的其余 IT 项目应该禁用。 我将在下面附上应用程序的屏幕截图和代码。任何人都可以帮助我,请。提前致谢。

    import 'package:flutter/material.dart';

void main() {
  runApp(App());
}

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: CardWithTextformfield(),
    );
  }
}

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

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

class _CardWithTextformfieldState extends State<CardWithTextformfield> with AutomaticKeepAliveClientMixin {
  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;

  var name =<TextEditingController>[];
  var id =<TextEditingController>[];

  var addCard =1;

  void incrementcard(){
    setState(() {
      if(addCard >=4){
        return;
      }
      addCard++;
    });
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
      appBar: AppBar(
        title: Text('Card with TextformField'),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: incrementcard,
        child: Icon(Icons.add),
      ),
      body: Container(
        child:ListView.builder(
          itemCount: addCard,
            itemBuilder: (context,index){
            return cardslist(index);
            }
        ),
      ),
    );
  }
  Widget cardslist(int index){
    if(name.length <= index){
      name.add(TextEditingController());
      id.add(TextEditingController());
    }
    return Card(
      margin: EdgeInsets.all(10),
      child: Container(
        child: Column(
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Container(
                    margin: EdgeInsets.all(10),
                    child: Text('Team Name: ')),
                Expanded(child: TextFormField(
                  controller: name[index],
                    decoration: InputDecoration(hintText: 'Team Name'),
                ),),
                Container(
                  margin: EdgeInsets.all(10),
                  child: Text('Team Id: '),),
                Expanded(child: TextFormField(
                  controller: id[index],
                    decoration: InputDecoration(hintText: 'Team Id'),
                ),),
              ],
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                GestureDetector(
                  child: Container(
                    width: 50,height: 50,
                    margin: EdgeInsets.all(10),
                    decoration: BoxDecoration(
                      shape: BoxShape.rectangle,
                      color: Colors.grey,
                    ),
                    child: Center(child: Text('IT'),),
                  ),
                ),
                GestureDetector(
                  child: Container(
                    width: 50,height: 50,
                    margin: EdgeInsets.all(10),
                    decoration: BoxDecoration(
                      shape: BoxShape.rectangle,
                      color: Colors.grey,
                    ),
                    child: Center(child: Text('DEV'),),
                  ),
                ),
                GestureDetector(
                  child: Container(
                    width: 50,height: 50,
                    margin: EdgeInsets.all(10),
                    decoration: BoxDecoration(
                      shape: BoxShape.rectangle,
                      color: Colors.grey,
                    ),
                    child: Center(child: Text('TEST'),),
                  ),
                ),
                GestureDetector(
                  child: Container(
                    width: 50,height: 50,
                    margin: EdgeInsets.all(10),
                    decoration: BoxDecoration(
                      shape: BoxShape.rectangle,
                      color: Colors.grey,
                    ),
                    child: Center(child: Text('HR'),),
                  ),
                ),
              ],
            )
          ],
        ),
      ),
    );
  }

}

【问题讨论】:

    标签: flutter flutter-layout maps flutter-card flutter-radiobutton


    【解决方案1】:

    据我了解,您想比较 map

    内部值的值

    声明一个全局 -> bool 变量(final bool isSelected = false)

    • 将值存储在您要比较的 variable

    • 使用forLoop迭代map中的值

    • 检查值是否存在,设置isSelected = true

    • 使用isSelected设置颜色

               GestureDetector(
                 child: Container(
                   width: 50,height: 50,
                   margin: EdgeInsets.all(10),
                   decoration: BoxDecoration(
                     shape: BoxShape.rectangle,
                     color: isSelected == true ? Colors.blue : Colors.grey,
                   ),
                   child: Center(child: Text('HR'),),
                 ),
               ),
      

    请查看帖子一次以了解地图迭代 - Add/Update list of K,V pair to empty declared map = List<Map<dynamic, dynamic>>

    希望它能解决问题?

    【讨论】:

    • 谢谢@indrajeet,但我的问题不仅在于颜色,还在于实现逻辑。因此,如果在一张卡片中选择了一个项目,则其余卡片中的项目应禁用(不可选择,更改为浅白色)。灰色表示可选择,蓝色表示已选择,浅白色表示不可选择。希望你能理解我的问题。
    • 您好,您可以告诉我,您如何定义要选择的项目?
    • 嘿,@indrajeet 除了地图集来解决问题吗?
    • 嗨@shanmkha,你有解决方案吗,如果没有,请解释我how are you defining which item to select
    • 嘿@indrajeet 我没有找到解决方案,我没有处理这个问题
    猜你喜欢
    • 1970-01-01
    • 2012-04-06
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-13
    相关资源
    最近更新 更多