selcukaydin07tr
selcukaydin07tr

Reputation: 7

Flutter passing values between two classes

I have o class named DocUploadScreen;

   class DocUploadScreen extends StatefulWidget {

   DocUploadScreen({Key key, this.emosicilno, this.trafono});

   TextEditingController emosicilno = new TextEditingController();
   
   TextEditingController trafono = new TextEditingController();

I want to use this emosicilno and trafono inside different class Uploader;

  class _UploaderState extends State<Uploader> {
  final FirebaseStorage _storage =
      FirebaseStorage(storageBucket: 'gs://emo-is0.appspot.com');

  StorageUploadTask _uploadTask;

  _startUpload() {

    String filePath = 'uploadphotos/${DateTime.now()}.png';
    setState(() {
      _uploadTask = _storage.ref().child(filePath).putFile(widget.file);
    });
  }

here I want form like this;

'uploadphotos/${emosicilno}/${trafono}/${DateTime.now()}.png';

Here I am defining emosicilno and trafono inside DocUploadScreen but ı want to use them inside Uploader

Please advice

Upvotes: 0

Views: 63

Answers (1)

ahmed
ahmed

Reputation: 454

your question is not clear,answer for the question is to make a constructor then pass the value to the constructor you asked how i can pass the value so here is the example of it

class TwoTextField extends StatefulWidget {
  TwoTextField({Key key}) : super(key: key);

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

class _TwoTextFieldState extends State<TwoTextField> {
  TextEditingController _docName = TextEditingController();

  TextEditingController _docDesc = TextEditingController();
  final _formKey = GlobalKey<FormState>();
  final TheTwoTextFieldModel _theTwoTextFieldModel = TheTwoTextFieldModel();

  @override
  Widget build(BuildContext context) {
    var size = MediaQuery.of(context).size;
    var _hieght = size.height;
    var _width = size.width;

    return Container(
      child: Container(
        padding: EdgeInsets.all(16.0),
        width: _width / 3,
        height: _hieght / 3,
        color: Colors.grey[200],
        child: Form(
          key: _formKey,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisSize: MainAxisSize.max,
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              TextFormField(
                controller: _docName,
                decoration: InputDecoration(hintText: 'Doc Name'),
                validator: (val) => val.isEmpty ? 'please write a name' : null,
              ),
              TextFormField(
                controller: _docDesc,
                decoration: InputDecoration(hintText: 'Doc Description'),
                validator: (val) => val.isEmpty ? 'please write a desc' : null,
              ),
              RaisedButton.icon(
                onPressed: () async {
                  if (_formKey.currentState.validate()) {
                    String _nameText = _docName.text;
                    String _descText = _docDesc.text;

                    bool result =
                        _theTwoTextFieldModel.uploader(_nameText, _descText);
                    if (result == false) {
                      setState(() {
                        print('failed');
                      });
                    } else {
                      print('success');
                    }
                  }
                },
                icon: Icon(Icons.upload_file),
                label: Text('Upload'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

class TheTwoTextFieldModel {
  bool uploader(String docName, String docDesc) {
    String name = docName;
    String desc = docDesc;
    bool isUploaded = false;
    if (name.isNotEmpty && desc.isNotEmpty) {
      DateTime dateTime = DateTime.now();
      String readyDoc = 'uploadphotos/$name/$desc/$dateTime.png';
      print(readyDoc);
      return isUploaded = true;
    } else {
      return isUploaded;
    }
  }
}

i made this code in a simple way so that you can understand but there are better ways to use constructor, if you want to know more about constructure search for it on internet

Upvotes: 1

Related Questions