0

我有显示可用航班列表的磁贴列表。我想更改所选图块边框的颜色。目前,它会更改所有图块的颜色,而不仅仅是选定的图块。

final flightState = Provider.of<AppState>(context);
return new Material(
    child: ListView.builder(
        itemCount: entityContainer.inbound.length,
        itemBuilder: (BuildContext context, i) => ListTile(
            title: new Row(
              children: <Widget>[
                new Results(
                  entityContainer.inbound[i].departureAirportName,
                  entityContainer.inbound[i].arrivalAirportName,
                  entityContainer.inbound[i].serviceProvider,
                  entityContainer.inbound[i].departureTime,
                  entityContainer.inbound[i].arrivalTime,

                ),
              ],
            ),
            onTap: () {
              if (flightState.getInboundIndex() == i) {
                flightState.updateInboundIndex(-1);
                flightState.unSelectTile();
              } else {
                flightState.updateInboundIndex(i);
                flightState.selectTile();

              }
            })));

我曾经provider保持状态

AppState.dart

class AppState with ChangeNotifier {
  int _inboundIndex;
  bool _selected;

  AppState(this._inboundIndex, this._selected);

  getInboundIndex() => _inboundIndex;
  getSelected() => _selected;

  void updateInboundIndex(i) {
    _inboundIndex = i;
    notifyListeners();
  }

  void selectTile() {
    _selected = true;
    notifyListeners();
  }

  void unSelectTile() {
    _selected = false;
    notifyListeners();
  }}

这是根据状态进行绘图的地方

结果.dart

  .....
  .....
  decoration: BoxDecoration(
    border: Border.all(
        color: flightState.getSelected() ? Colors.green : Colors.black26,
        width: flightState.getSelected() ? 8.0 : 1.0),
  ),
 .....
 .....
4

1 回答 1

0

如果有人有类似的问题。这就是我解决问题的方法:

我已经通过了索引如下

          new Results(
              entityContainer.inbound[i].departureAirportName,
              entityContainer.inbound[i].arrivalAirportName,
              entityContainer.inbound[i].serviceProvider,
              entityContainer.inbound[i].departureTime,
              entityContainer.inbound[i].arrivalTime,
              i
            ),

并且,将渲染更改如下:

    decoration: BoxDecoration(
    border: Border.all(
        color: (flightState.getSelected() &&
                currentIndex == flightState.getInboundIndex())
            ? Colors.green
            : Colors.black26,
        width: flightState.getSelected() ? 2.0 : 1.0),
    ),
于 2019-07-15T15:30:14.327 回答