Flutter:制作选定的卡片

时间:2019-12-29 07:30:30

标签: flutter dart

我有一个简单的水平ListviewBuilder,在里面有可以选择的卡片。但是问题是,当我选择一张卡片时,所有卡片都被选中。 我想要的是我只能选择1张卡片,如果我选择另一张卡片,则删除旧选择并创建新选择的新卡片。 这该怎么做 ?

  

类似这样的东西:

enter image description here

  

我的失败结果

enter image description here

这是我的源代码:

import 'package:flutter/material.dart';

class IconMenu {
  final IconData iconName;
  final String titleIcon;
  IconMenu({this.iconName, this.titleIcon});
}

List<IconMenu> iconList = [
  IconMenu(iconName: Icons.ac_unit, titleIcon: "AC Unit"),
  IconMenu(iconName: Icons.access_alarm, titleIcon: "Alarm"),
  IconMenu(iconName: Icons.accessibility_new, titleIcon: "accessiblity"),
  IconMenu(iconName: Icons.add, titleIcon: "Add"),
];

class TestingScreen extends StatefulWidget {
  static const routeName = "/testing-page";

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

class _TestingScreenState extends State<TestingScreen> {
  bool selectedList = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Container(
            height: 200,
            child: ListView.builder(
              scrollDirection: Axis.horizontal,
              itemCount: iconList.length,
              itemBuilder: (BuildContext context, int index) {
                return InkWell(
                  onTap: () => setState(() => selectedList = !selectedList),
                  child: Container(
                    width: 150,
                    child: Card(
                      shape: selectedList
                          ? RoundedRectangleBorder(
                              side: BorderSide(color: Colors.green))
                          : null,
                      elevation: 5,
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        children: <Widget>[
                          Icon(iconList[index].iconName),
                          Text(iconList[index].titleIcon)
                        ],
                      ),
                    ),
                  ),
                );
              },
            ),
          )
        ],
      ),
    );
  }
}

1 个答案:

答案 0 :(得分:1)

对于所选索引,您应该使用int

int selectedIndex=-1

然后,您可以检查每张卡的position是否等于selectedIndex,然后再选择它。

class _TestingScreenState extends State<TestingScreen> {
  int selectedIndex = -1;

  @override
  Widget build(BuildContext context) {

        return Scaffold(
          body: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Container(
                height: 200,
                child: ListView.builder(
                  scrollDirection: Axis.horizontal,
                  itemCount: iconList.length,
                  itemBuilder: (BuildContext context, int position) {
                    return InkWell(
                      onTap: () => setState(() => selectedIndex=position),
                      child: Container(
                        width: 150,
                        child: Card(
                          shape: (selectedIndex==position)
                              ? RoundedRectangleBorder(
                                  side: BorderSide(color: Colors.green))
                              : null,
                          elevation: 5,
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.spaceAround,
                            children: <Widget>[
                              Icon(iconList[position].iconName),
                              Text(iconList[position].titleIcon)
                            ],
                          ),
                        ),
                      ),
                    );
                  },
                ),
              )
            ],
          ),
        );
  }   
}