首页 > 解决方案 > 在颤动中使用 AlertDialog 从数据库中显示选定的数据

问题描述

嗨,我正在尝试使用警报对话框显示数据库中的特定或选定数据,但是我想显示的数据不会显示到 alertDialog,因为我为我的数据库使用 floor 的方式是我非常感谢任何建议或想法.

这是我的代码

Widget buildAboutDialog(BuildContext context,
      AddContactCallback _contactscreen, bool isEdit, ContactObject _contactObject)  {
      final databases = $FloorContactDatabase.databaseBuilder('Contacts.cb').build();
      if(contactObject != null) {
        this.contactObject = _contactObject;
        tfirstname.text = _contactObject.firstname;
        tlastname.text = _contactObject.lastname;
        tbirthday.text = _contactObject.birthday;
        tcontactnumber = _contactObject.contactnumber as TextEditingController ;
        tprofilepicture = _contactObject.profilepicture as File;
      } else {
        print('ERROR');
        print(tfirstname.value);
      }
      return new AlertDialog(
        title: new Text(isEdit ? 'Contact!' : 'Contact'),
        content: new SingleChildScrollView(
          child: new Column(
            mainAxisSize: MainAxisSize.max,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              SizedBox(
              height: 10.0,
              ),
              Align(
                alignment: Alignment.center,
                child: CircleAvatar(
                radius: 70,
                backgroundColor: Color(0xff476cfb),
                child: ClipOval(
                  child: new SizedBox(
                    width: 180.0,
                    height: 180.0,
                    child: (_image != null) ? Image.file(_image, // Image.file(File('${_contactsDao.profilepicture}'),
                      fit: BoxFit.fill,
                    ) : Image.network(
                      "https://images.unsplash.com/photo-1502164980785-f8aa41d53611?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60",
                      fit: BoxFit.fill,
                    ),
                  ),
                ),
              ),
              ),
              getTextFieled("First Name", tfirstname),
              getTextFieled("Last Name", tlastname),
              getTextFieled("Birth day", tbirthday),
              getTextFieled("Contact No", tcontactnumber),
            ],
          ),
        ),
      );
  }

这是函数和实例

    Widget getTextFieled(
      String inputData, TextEditingController inputTextController) {
      var btm = new Padding(
          padding: const EdgeInsets.all(5.0),
          child:  new  TextFormField(
            controller: inputTextController,
            decoration: new InputDecoration(
              hintText: inputData,
            ),
          ),
        );
      return btm;
 }

 Widget getAppButton (String buttonLabel, EdgeInsets margin){
    var btm = new Container(
      margin: margin,
      padding: EdgeInsets.all(8.0),
      alignment: FractionalOffset.center,
      decoration: new BoxDecoration(
        border: Border.all(color: const Color(0xFF28324E)),
        borderRadius: new BorderRadius.all(const Radius.circular(6.0)),
      ),
      child: new Text(
          buttonLabel,
          style: new TextStyle(
            color: const Color(0xFF28324E),
            fontSize: 20.0,
            fontWeight: FontWeight.w300,
            letterSpacing: 0.3,
          ),
      ),
    );
    return btm;
 }

 ContactObject getdata(bool isEdit) {
    return new ContactObject(
      firstname: tfirstname.text,
     lastname: tfirstname.text,
     birthday: tbirthday.text,
     contactnumber: int.parse(tcontactnumber.text),
      profilepicture: tprofilepicture.readAsStringSync(),
    );
 }

 onTap(bool isEdit, AddContactCallback addContactCallback, BuildContext context) {
    if(isEdit) {
      addContactCallback.updateContacts(getdata(isEdit));
      Navigator.of(context).pop();
      print('ANG PUMASOK');
      print(tfirstname);
    } else {
      Fluttertoast.showToast(
          msg: 'ERROR ERROR',
          toastLength: Toast.LENGTH_SHORT,
          timeInSecForIosWeb: 1,
          gravity: ToastGravity.BOTTOM
      );
    }
 }


}

abstract class AddContactCallback {
    void updateContacts(ContactObject contactObject);
}

这是我的想法的截图

这是我的想法的截图

标签: flutterdartandroid-alertdialog

解决方案


TextFormField我认为在设置它之后 需要将值设置TextEditingController为。
在返回小部件之前尝试,设置一个值;

Widget getTextFieled(
      String inputData, TextEditingController inputTextController) {
      var btm = new Padding(
          padding: const EdgeInsets.all(5.0),
          child:  new  TextFormField(
            controller: inputTextController,
            decoration: new InputDecoration(
              hintText: inputData,
            ),
         ),
      );
      inputTextController.text = "Some Value Test";
      return btm;
 }

推荐阅读