简体   繁体   English

Flutter 使用 ListView.Builder:如何在我选择的所有项目上仅更改一项的背景颜色

[英]Flutter Using ListView.Builder: how can i change background color of only one item on all items with i selected it

  • I used ListViewBuilder to render a list of time slots, and What i need is to be able to select only one item of them, and when i selected it i need to change the background of only this item and then appear the reservation details, but what actually done is that the all items background changed to another color我使用 ListViewBuilder 来呈现时间段列表,我需要的是只能选择其中的一项,当我选择它时,我只需要更改此项的背景,然后显示预订详细信息,但是实际所做的是将所有项目的背景更改为另一种颜色

[![This is image that before select a one time slot][1]][1][![And this when i try to select one item all items are selected and then the reservation details appear when i preesed the time slot][1]][1]

[![And this when i try to select one item all items are selected and then the reservation details appear when i preesed the time slot][1]][1]
class _ReservationSlotsWidgetState extends State<ReservationSlotsWidget> {
  var _isExpanded = false;
  var slots = [
    '09:00AM',
    '10:00AM',
    '11:00AM',
    '12:00PM',
    '01:00PM',
    '02:00PM',
    '03:00PM',
    '04:00PM',
  ];

  @override
  Widget build(BuildContext context) {
    final deviceSize = MediaQuery.of(context).size;
    return Scaffold(
      extendBodyBehindAppBar: true,

....

 Expanded(
                  child: ListView.builder(
                itemCount: slots.length,
                scrollDirection: Axis.horizontal,
                itemBuilder: (ctx, index) {
                  var slot = slots[index];
                  return GestureDetector(
                    onTap: () {
                      setState(() {
                        _isExpanded = !_isExpanded;
                      });
                    },
                    child: Container(
                        padding: EdgeInsets.all(4),
                        margin: EdgeInsets.all(4),
                        decoration: BoxDecoration(
                            color: _isExpanded
                                ? Theme.of(context).primaryColor
                                : parseColor('#2A2E43'),
                            borderRadius:
                                BorderRadius.all(Radius.circular(10))),
                        child: SlotsWidget())
                        );
                        }
              )),
            ],
          ),
        ),

 SizedBox(
          height: deviceSize.height * 0.00025,
        ),
        if (_isExpanded)
          Container(
              padding: EdgeInsets.all(16),
              margin: EdgeInsets.all(16),
              height: deviceSize.height * 0.2,
              decoration: BoxDecoration(
                  color: Theme.of(context).primaryColorDark,
                  borderRadius: BorderRadius.all(Radius.circular(10))),
              child: Column(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: <Widget>[
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            Row(
                              children: <Widget>[
                                Icon(
                                  Icons.person_add,
                                  color: Colors.white,
                                ),
                                SizedBox(
                                  width: 5,
                                ),
                                Text('Number of persons',
                                    style: TextStyle(
                                      color: Colors.white,
                                    )),
                                SizedBox(
                                  height: 5,
                                ),
                              ],
                            ),
                            Row(
                              children: <Widget>[
                                Icon(
                                  Icons.person_add,
                                  color: Colors.transparent,
                                ),
                                SizedBox(
                                  width: 5,
                                ),
                                Text('(max 7 persons)',
                                    style: TextStyle(
                                      color: Colors.white,
                                    )),
       
....

So the key to your question is the index of your ListView Builder .所以你的问题的关键是你的ListView Builder的索引。 The way you are currently doing it, is you are setting a single boolean to be true or false, which applies to every element of your list.您目前的做法是将单个布尔值设置为 true 或 false,这适用于列表中的每个元素 What you need to do, is save the index of your expanded widget.您需要做的是保存扩展小部件的索引。

Change改变

onTap: () {
     setState(() {
         _isExpanded = !_isExpanded;
     });
},

To

int _expandedIndex;
...
onTap: () {
    setState(() {
       _expandedIndex = index
    });
},

And change并改变

color: _isExpanded
       ? Theme.of(context).primaryColor
       : parseColor('#2A2E43'),

To

color: _expandedIndex == index
       ? Theme.of(context).primaryColor
       : parseColor('#2A2E43'),

Create separate class like创建单独的类

class Slot {
  String time;
  bool isSelected;

  Slot(this.time, this.isSelected);
}

Create a slot list of the above class创建上述类的插槽列表

 var slots = [
    Slot('09:00AM', false),
    Slot('10:00AM', false),
    Slot('11:00AM', false),
    Slot('12:00PM', false),
    //... Rest of the items
  ];

Get the slot object from the list(This should be inside Listview.Builder )从列表中获取插槽对象(这应该在Listview.Builder

var slot = slots[index];
var time = slot.time; // Get the time from slot object

Mark isSelected as true on click of the list item单击列表项时将isSelected标记为true

slot.isSelected = true;

OR或者

slot.isSelected = !slot.isSelected; // Vice versa

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

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