访问小部件中的迭代列表以获取Flutter中的更新功能

时间:2019-04-18 09:14:20

标签: dart flutter

我正在研究某些数据的CRUD功能,其中有一个数据列表,我们可以从中选择一个数据并进行更新。我的问题是我无法访问可以完成更新的小部件中的单个数据列表。我有相同的小部件,用于bot添加和更新数据。每当我选择“编辑”按钮时,“添加”小部件都应在文本格式字段中填充相应的数据。我是这项技术的新手,对这项工作有些困惑。 任何帮助表示赞赏。等待回应。

view page build method

   return Scaffold(
       appBar: AppBar(
         iconTheme: IconThemeData(
           color: Theme.of(context).primaryColor,
         ),
         backgroundColor: Theme.of(context).canvasColor,
         titleSpacing: 0.0,
         title: Text(
           widget._member.firstName.substring(0, 1).toUpperCase() +
               widget._member.firstName.substring(1) +
               ' ' +
               widget._member.lastName.substring(0, 1).toUpperCase() +
               widget._member.lastName.substring(1),
           style: TextStyle(
               fontSize: 16.0,
               fontWeight: FontWeight.w800,
               color: Theme.of(context).primaryColor),
           textAlign: TextAlign.left,
         ),
       ),
       body: ScopedModelDescendant(
         builder: (BuildContext context, Widget child, MainModel model) {
return SingleChildScrollView(
             child: Container(
               padding: EdgeInsets.all(10.0),
               child: Card(
                 color: Color(0xffF7F7F7),
                 child: Column(
                   children: <Widget>[
Container(
                       padding: EdgeInsets.all(10.0),
                       width: MediaQuery.of(context).size.width,
                       child: Form(
                         child: Column(
                           children: <Widget>[
                             Container(
                               margin: EdgeInsets.all(5.0),
                               child: Row(
                                 mainAxisAlignment: MainAxisAlignment.start,
                                 children: <Widget>[
                                   Expanded(
                                     child: Text(
                                       _addNewNote ? 'Notes' : 'Add New Note',
                                       style:
                                           Theme.of(context).textTheme.body1,
                                     ),
                                   ),
                                   Align(
                                     child: FlatButton(
                                       child: Text(
                                           _addNewNote
                                               ? '+ Add New Note'
                                               : 'Cancel',
                                           style: TextStyle(
                                               fontSize: 14.0,
                                               color: Theme.of(context)
                                                   .primaryColor)),
                                       onPressed: () {
                                         setState(
                                             () => _addNewNote = !_addNewNote);
                                       },
                                     ),
                                   )
                                 ],
                               ),
                             ),
                             Container(
                               child: _addNewNote
                                   ? _buildListNoteSection(model)
                                   : _buildAddNewNoteSection(model, model.memberNotes)
                             ),
],
                         ),
                       ),
                     ),

buildListNoteSection

Widget _buildListNoteSection(MainModel model) {
    return Container(
      width: MediaQuery.of(context).size.width,
      height: MediaQuery.of(context).size.height * 0.3,
      child: ListView.builder(
        itemBuilder: (BuildContext context, int index) {
          return _listMemberNote(model.memberNotes[index], model);
        },
        itemExtent: 200.0,
        itemCount: model.memberNotes.length,
      ),
    );
  }

buildAddNewNoteSection

Widget _buildAddNewNoteSection(MainModel model, memberNote) {
    return Form(
      key: _formKey,
      child: Column(
        children: <Widget>[
          TextFormField(
            style: TextStyle(fontSize: 16.0),
            decoration: InputDecoration(
              labelText: 'Subject / Title',
              contentPadding: EdgeInsets.all(10.0),
              filled: true,
              fillColor: Theme.of(context).canvasColor,
              border: OutlineInputBorder(
                borderRadius: BorderRadius.all(
                  Radius.circular(10.0),
                ),
              ),
              enabledBorder: OutlineInputBorder(
                borderRadius: BorderRadius.all(Radius.circular(10.0)),
                borderSide: BorderSide(
                    color: Theme.of(context).canvasColor, width: 2.0),
              ),
              focusedBorder: OutlineInputBorder(
                borderRadius: BorderRadius.all(Radius.circular(10.0)),
                borderSide: BorderSide(
                    color: Theme.of(context).canvasColor, width: 2.0),
              ),
              errorBorder: OutlineInputBorder(
                borderRadius: BorderRadius.all(Radius.circular(10.0)),
                borderSide: BorderSide(color: Colors.red, width: 2.0),
              ),
              focusedErrorBorder: OutlineInputBorder(
                borderRadius: BorderRadius.all(Radius.circular(10.0)),
                borderSide: BorderSide(color: Colors.red, width: 2.0),
              ),
            ),
            validator: (String value) {
              if (value.isEmpty) {
                return 'Title field is required';
              }
            },
            onSaved: (String value) {
              _noteTitle = value;
            },
          ),
          SizedBox(
            height: 10.0,
          ),
          TextFormField(
            maxLines: 4,
            style: TextStyle(fontSize: 16.0),
            decoration: InputDecoration(
              labelText: 'Note',
              contentPadding: EdgeInsets.all(10.0),
              filled: true,
              fillColor: Theme.of(context).canvasColor,
              border: OutlineInputBorder(
                borderRadius: BorderRadius.all(
                  Radius.circular(10.0),
                ),
              ),
              enabledBorder: OutlineInputBorder(
                borderRadius: BorderRadius.all(Radius.circular(10.0)),
                borderSide: BorderSide(
                    color: Theme.of(context).canvasColor, width: 2.0),
              ),
              focusedBorder: OutlineInputBorder(
                borderRadius: BorderRadius.all(Radius.circular(10.0)),
                borderSide: BorderSide(
                    color: Theme.of(context).canvasColor, width: 2.0),
              ),
              errorBorder: OutlineInputBorder(
                borderRadius: BorderRadius.all(Radius.circular(10.0)),
                borderSide: BorderSide(color: Colors.red, width: 2.0),
              ),
              focusedErrorBorder: OutlineInputBorder(
                borderRadius: BorderRadius.all(Radius.circular(10.0)),
                borderSide: BorderSide(color: Colors.red, width: 2.0),
              ),
            ),
            validator: (String value) {
              if (value.isEmpty) {
                return 'Description field is required';
              }
            },
            onSaved: (String value) {
              _noteDescription = value;
            },
          ),
          SizedBox(
            height: 10.0,
          ),
          ScopedModelDescendant(
            builder: (BuildContext context, Widget child, MainModel model) {
              return ButtonTheme(
                minWidth: 400.0,
                height: 50.0,
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(7.0)),
                child: RaisedButton(
                  child: Text(
                    'Save',
                    style: Theme.of(context).textTheme.headline,
                  ),
                  color: Theme.of(context).buttonColor,
                  textColor: Theme.of(context).canvasColor,
                  onPressed: () {
                    if (!_formKey.currentState.validate()) {
                      return;
                    }
                    _formKey.currentState.save();

                    MemberController.addMemberNote(widget._member.memberId,
                            _noteTitle, _noteDescription, model)
                        .then((value) {
                      if (value['success']) {
                        showDialog(
                            context: context,
                            builder: (BuildContext context) {
                              return AlertDialog(
                                title: Text(''),
                                content: Text(value['message']),
                                actions: <Widget>[
                                  FlatButton(
                                    child: Text('Okay'),
                                    onPressed: () {
                                      setState(() => _addNewNote = !_addNewNote);
                                      Navigator.of(context).pop();
                                      MemberController.fetchMemberNoteList(model, widget._member.memberId.toString());
                                    },
                                  ),
                                ],
                              );
                            });
                      } else {
                        showDialog(
                            context: context,
                            builder: (BuildContext context) {
                              return AlertDialog(
                                title: Text('An Error Occured!'),
                                content: Text(value['message']),
                                actions: <Widget>[
                                  FlatButton(
                                    child: Text('Okay'),
                                    onPressed: () {
                                      Navigator.of(context).pop();
                                    },
                                  ),
                                ],
                              );
                            });
                      }
                    });
                  },
                ),
              );
            },
          )
        ],
      ),
    );
  }

我希望当我们单击编辑链接时,“添加”小部件应填充相应的数据并可以更新。

0 个答案:

没有答案