簡體   English   中英

如何即使AlertDialog消失后仍保留列表值

[英]how to keep list values even after AlertDialog disappear

我在有狀態窗口小部件類中列出了一個列表,當用戶選擇卡片時,該列表將填充一些值。我在應用程序的按鈕上具有showDialog函數,每次單擊該表單時,都會向用戶顯示一個表單。 問題是用戶通過單擊“確定”按鈕從表單返回表單后,將重新創建有狀態的類並清理我所有列表的值(再次調用ActivityPeopleCard類)...即使在從showDialog形式回來?

class ActivityPeopleCard extends StatefulWidget{

  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();


  @override
  Widget build(BuildContext context) {

    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(
              behavior: HitTestBehavior.translucent,
              onTap: ()=> setState(() => {

              }


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

       ),

    );
  }

  _buildWidget(int index, BuildContext context){

    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, isSelected, context)
                    ])
              );
  }

  shareTag(index, isSelected, BuildContext context){
           return GestureDetector(
                      onLongPress: ()=>{

                        enterPrice(index, context),

                    },
                    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, BuildContext context){
    //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
                               ),
                          ),

                        ),


                      ),

                          ],
                        ),
                      ),
                    );
                });
  }
}

用戶從表單返回后,_cost列表將再次使用null初始化,並顯示null值而不是以前的值

在初始狀態下創建一個返回容器的變量。 然后在您的構建方法中返回您的變量。 每次調用build方法時,都會停止刷新/重置列表

class ActivityPeopleCard extends StatefulWidget {
  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(
              behavior: HitTestBehavior.translucent,
              onTap: () => setState(() => {}),
              child: _buildWidget(index, context));
        },
      ),
    );
  }

  _buildWidget(int index, BuildContext context) {
    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, isSelected, context)
        ]));
  }

  shareTag(index, isSelected, BuildContext context) {
    return GestureDetector(
        onLongPress: () => {
              enterPrice(index, context),
            },
        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, BuildContext context) {
    //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),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          );
        });
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM