繁体   English   中英

使用可重复使用的小部件,我如何将 state 传递给该小部件

[英]using reusable Widget how can i pass state to that widget

在这个小部件中有对 setState 的调用,但它给了我错误,因为这是一个全局小部件,我只是在其中传递所需的数据我还想传递 state 以便它可以访问 setState 方法我该如何实现? 提前致谢....


 
 Widget postWidget(Map<dynamic,dynamic> homeMap,index,context){
   return Padding(
       padding: EdgeInsets.all(MediaQuery.of(context).size.width * 0.02),
       child: Card(
           elevation: 4.0,
           shape: RoundedRectangleBorder(
             borderRadius: BorderRadius.circular(10.0),
           ),
           child: Stack(children: <Widget>[
             Column(crossAxisAlignment: CrossAxisAlignment.start, children: <
                 Widget>[
               SizedBox(
                 height: 1,
               ),
 
               commonProfileData(homeMap, index),
           SizedBox(
                 height: height * 0.005,
               ),
               homeMap['homeModelDataList'][index].description != ""
                   ? Padding(
                       padding: EdgeInsets.only(
                           bottom: MediaQuery.of(context).size.height * 0.008,
                           left: MediaQuery.of(context).size.height * 0.025,
                           right: MediaQuery.of(context).size.height * 0.025),
                       child: Text(
                         homeMap['homeModelDataList'][index].description,
                         textAlign: TextAlign.justify,
                       ),
                     )
                   : SizedBox(
                       height: 0,
                     ),
               SizedBox(
                 height: height * 0.005,
               ),
             homeMap['homeModelDataList'][index].photo != ""
                   ? Container(
                       margin: EdgeInsets.only(
                           right: MediaQuery.of(context).size.height * 0.02,
                           left: MediaQuery.of(context).size.height * 0.02),
                       child: Container(
                         clipBehavior: Clip.antiAlias,
                         height: MediaQuery.of(context).size.height * 0.25,
                         decoration: BoxDecoration(
                           borderRadius: BorderRadius.circular(10),
                           color: Colors.red,
                         ),
                         child: Image.network(
                             homeMap['homeModelDataList'][index].photo,
                             fit: BoxFit.fill),
                       ),
                     )
                   : SizedBox(
                       height: 0,
                     ),
 
 
               ////////////////////////////   ICON BUTTON   //////////////////////////////////
               
 
                     commonButtons(homeMap, index),
 
 
               openCommentBox &&
                       postId ==
                           homeMap['homeModelDataList'][index].postId.toString()
                   ? Container(
                       child: Column(
                         crossAxisAlignment: CrossAxisAlignment.start,
                         children: <Widget>[
                           Row(children: <Widget>[
                             Container(
                               margin: EdgeInsets.only(
                                 left:
                                     MediaQuery.of(context).size.height * 0.015,
                               ),
                               height: MediaQuery.of(context).size.height * 0.05,
                               width: MediaQuery.of(context).size.height * 0.25,
                               child: TextFormField(
                                 minLines: 1,
                                 maxLines: 1,
                                 controller: commentDataController,
                                 focusNode: commentDescriptionFocus,
                                 onFieldSubmitted: (value) {
                                   commentDescriptionFocus.unfocus();
                                 },
                                 decoration: InputDecoration(
                                     hintText: "Add Comment",
                                     hintStyle: TextStyle(
                                         color: isCommentDescriptionEmpty
                                             ? Colors.red
                                             : Colors.grey),
                                     enabledBorder: OutlineInputBorder(
                                         borderRadius: BorderRadius.circular(
                                             MediaQuery.of(context).size.width *
                                                 0.03)),
                                     focusedBorder: OutlineInputBorder(
                                         borderSide: const BorderSide(
                                             color: Colors.grey),
                                         borderRadius: BorderRadius.circular(
                                             MediaQuery.of(context).size.width *
                                                 0.03))),
                               ),
                             ),
                            
                             Container(
                               clipBehavior: Clip.antiAlias,
                               width: MediaQuery.of(context).size.width * 0.3,
                               height: MediaQuery.of(context).size.width * 0.1,
                               margin: EdgeInsets.only(
                                   right:
                                       MediaQuery.of(context).size.height * 0.01,
                                   left: MediaQuery.of(context).size.height *
                                       0.01),
                               decoration: BoxDecoration(
                                   color: Colors.red,
                                   borderRadius: BorderRadius.circular(10)),
                               child: RaisedButton(
                                   child: Text("Add Comment",
                                       textAlign: TextAlign.center,
                                       style: TextStyle(fontSize: 12.2)),
                                   onPressed: () async {
                                     if (commentDataController.text
                                             .trim()
                                             .length ==
                                         0) {
                                       isCommentDescriptionEmpty = true;
                                       print("i am here");
                                     } else {
                                       isCommentDescriptionEmpty = false;
 
                                       //progressDialog.show();
                                       await sendCommentDataToServer(
                                           commentDataController.text);
                                       //progressDialog.hide();
 
                                       (commentInfoMap["status"] == "Success")
                                           ? toast.showToastMessage(
                                               message:
                                                   "Comment posted Successfully !")
                                           : toast.showToastMessage(
                                               message: "Comment post Failed !");
 
                                   
                                     }
                                   }),
                             )
                           ]),
                           InkWell(
                               child: Padding(
                                 padding: EdgeInsets.only(
                                   top:
                                       MediaQuery.of(context).size.height * 0.01,
                                   left: MediaQuery.of(context).size.height *
                                       0.018,
                                   bottom: MediaQuery.of(context).size.height *
                                       0.015,
                                 ),
                                 child: RichText(
                                     text: TextSpan(
                                         style: TextStyle(
                                             fontSize: 13, color: Colors.black),
                                         children: [
                                       TextSpan(
                                         text: showPostCommentsBox
                                             ? "Hide "
                                             : "Show ",
                                         style: TextStyle(
                                           color: Colors.blueAccent,
                                         ),
                                       ),
                                       TextSpan(
                                         text: "Comments",
                                         style: TextStyle(color: Colors.black),
                                       ),
                                     ])),
                               ),
                               onTap: () {
                                 setState(() {
                                                                   showPostCommentsBox = !showPostCommentsBox;
                                                                 });
                                                                 print(
                                                                     "button pressed state : $showPostCommentsBox");
                                                               }),
                                                         ],
                                                       ),
                                                     )
                                                   : SizedBox(
                                                       height: 0,
                                                     ),
                                 
                                               showPostCommentsBox &&
                                                       postId ==
                                                           homeMap['homeModelDataList'][index].postId.toString()
                                                   ? Container(
                                                       // color: Colors.red,
                                                       margin: EdgeInsets.only(
                                                           right: MediaQuery.of(context).size.height * 0.01,
                                                           left: MediaQuery.of(context).size.height * 0.01),
                                                       height: height * 0.15,
                                                       child: Column(
                                                         children: <Widget>[
                                                           Expanded(
                                                             child: ListView.builder(
                                                                 physics: BouncingScrollPhysics(),
                                                                 itemCount: listOfComment.length,
                                                                 itemBuilder:
                                                                     (BuildContext context, int position) {
                                                                   return Padding(
                                                                     padding: EdgeInsets.only(
                                                                       right: height * 0.01,
                                                                       left: height * 0.003,
                                                                     ),
                                                                     child: Card(
                                                                       color: Colors.grey[100],
                                                                       child: ListTile(
                                                                           title: Text(listOfComment[position][4]
                                                                               .toString()),
                                                                           trailing: Text("temp")),
                                                                     ),
                                                                   );
                                                                 }),
                                                           )
                                                         ],
                                                       ),
                                                     )
                                                   : Container(),
                                 
                                          
                                             ]
                                             )
                                           ])),
                                     );
                                 
                                 
                             
 
 
                                 }
                                 
                            

在这个小部件中有对 setState 的调用,但它给了我错误,因为这是一个全局小部件,我只是在其中传递所需的数据我还想传递 state 以便它可以访问 setState 方法我该如何实现? 提前致谢....

可以通过 state 你必须将传递给全局小部件。

例如:

从状态 class 调用:

userDrawer(this);

这是全局 class:

userDrawer(var state){ 
return Container( 
child: RaisedButton(
child: Text("Click"), 
onPressed:(){ 
state.setState(() { 
value = newValue;
}) }
)

传递按下按钮时执行的 function

  userDrawer((){setState(){});

在全球 class


  userDrawer(Function onPressed){
    return FlatButton(onPressed: onPressed, child: Text('Click'));
  }

或者如果你想设置一个值

  userDrawer(Function onPressed){
    return FlatButton(onPressed: (){
      value = newValue;
      onPressed();
    }, child: Text('Click'));
  }

暂无
暂无

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

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