如何将用户界面与代码中的数据更改同步

时间:2019-07-03 22:01:15

标签: android flutter dart

代码正在正确运行,但是问题是我看不到UI根据数据进行更改。从您的代码中可能会注意到,在_buildWidget函数中创建了我的卡,并调用了堆栈中的shareTag函数,但是首先,因为变量不为true,所以容器不可见,但是在用户单击其中一张卡后,会期望isSelected变为true,然后我可以看到该堆栈中的容器,但那没有发生……我调试了代码,并注意到每次用户点击时,isSelected变量都正确地右移。卡,但我在用户界面上看不到任何变化

  Activity activity;
  List<double> _cost;
  ActivityPeopleCard({this.activity});

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

class _ActivityPeopleCardState extends State<ActivityPeopleCard> {
  int _peopleIndex = -1;
  List<int> _peopleIndexes = new List();
  List<int> _longPressed = new List();
  var _vlContainer;
  @override
  void initState() {
    super.initState();
    _vlContainer = myListContainer();
  }

  @override
  Widget build(BuildContext context) {
    return _vlContainer;
  }

  myListContainer() {
    return Container(
      height: height / 2.6,
      child: new GridView.builder(
        itemCount: widget.activity.peopleInvolved.length,
        gridDelegate:
            new SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3),
        itemBuilder: (BuildContext context, int index) {
          return new GestureDetector(

              onTap: () => setState(() => {
               _priceSet(index),

              _buildWidget(index)
}),
              child: _buildWidget(index));
        },
      ),
    );
  }

  _buildWidget(int index) {
    bool isSelected = _peopleIndexes.contains(index) ? true : false;

    return new Card(
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(30)),
        elevation: 5.0,
        child: Stack(children: <Widget>[
          new Container(
            height: height / 4,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Container(
                  width: height / 15,
                  height: height / 15,

                  decoration: new BoxDecoration(
                    borderRadius: BorderRadius.circular(height / 20),
                    border: Border.all(color: Colors.white, width: 2),
                  ),
                  child: ClipRRect(
                      borderRadius: BorderRadius.circular(height / 15),
                      child: Image.asset(
                          widget.activity.peopleInvolved[index].imagePath,
                          fit: BoxFit.fill)), //CircleAvatar(
                ),
                Center(child: Text(widget.activity.peopleInvolved[index].name))
              ],
            ),
          ),
          shareTag(index)
        ]));
  }

  shareTag(index) {
    return GestureDetector(
        onLongPress: () => {
              enterPrice(index),
            },
        child: Visibility(
          visible: isSelected ? true : false,
          child: new Container(
              height: height / 2,
              decoration: new BoxDecoration(
                borderRadius: BorderRadius.circular(height / 20),
                color: Colors.red.shade500.withOpacity(0.7),
              ),
              child: Center(
                  child: SizedBox(
                width: width / 4,
                height: height / 15,
                child: Center(
                  child: AutoSizeText(
                    "\$${ActivityPeopleCard._cost[index]}",
                    style: TextStyle(
                        fontWeight: FontWeight.bold,
                        color: Colors.white,
                        fontFamily: 'Oxygen',
                        fontSize: height / 25),
                    maxLines: 1,
                    minFontSize: 5,
                    textAlign: TextAlign.center,
                  ),
                ),
              ))),
        ));
  }

  enterPrice(int index) {
    //print("Long Pressed");
    showDialog(
        context: context,
        builder: (context) {
          return AlertDialog(
            shape:
                RoundedRectangleBorder(borderRadius: BorderRadius.circular(25)),
            backgroundColor: Colors.white,
            content: Form(
              //key: _formKey,
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  new TextFormField(
                    initialValue: ActivityPeopleCard._cost[index].toString(),
                    keyboardType: TextInputType.number,
                    cursorColor: secondColor,
                    style: TextStyle(
                      letterSpacing: 1,
                    ),
                    autofocus: true,
                    decoration: new InputDecoration(
                      focusedBorder: new OutlineInputBorder(
                          borderRadius: new BorderRadius.circular(15.0),
                          borderSide: new BorderSide(color: secondColor)),
                      labelStyle: TextStyle(color: firstColor
                          //decorationColor: Colors.yellow
                          ),
                      prefixIcon: Icon(
                        Icons.attach_money,
                        color: firstColor,
                      ),
                      labelText: "Cost",
                      border: new OutlineInputBorder(
                        borderRadius: new BorderRadius.circular(15.0),
                        borderSide: new BorderSide(color: Colors.black87),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          );
        });
  }
}

尽管我在onTap函数上再次调用了_buildWidget,并且isSelected为true,但用户界面未更新

0 个答案:

没有答案