Search code examples
flutterflutter-getximagepicker

Flutter: How do i shift my code to another page and access it here


I am very new to Dart, and coding in general. I have produced this code after watching tutorials on YouTube. For the most part, I have been able to troubleshoot most of my problems on my own, here I feel I need some help. I have written code to upload photograph, but I wanted to shift the method to another and access the functionality.

Here is my code

class EditProfile extends StatefulWidget {
      EditProfile({Key? key}) : super(key: key);
    
      @override
      State<EditProfile> createState() => _EditProfileState();
    }
    
    class _EditProfileState extends State<EditProfile> {
      File? image;
    
      Future PickedImage(ImageSource source) async {
        try {
          final image = await ImagePicker()
              .pickImage(source: source, maxWidth: 160, maxHeight: 160);
          if (image == null) return;
          setState(() {
            final _imgTemp = File(image.path);
          });
        } on PlatformException catch (e) {
          print('failed to Upload $e');
        }
      }
    
      @override
      Widget build(BuildContext context) {
        var _textController = ProfileEdit();
    
        return Scaffold(
            body: GetX<ProfileController>(
          init: Get.put<ProfileController>(ProfileController()),
          builder: (ProfileController profileController) {
            return Container(
              child: ListView.builder(
                  itemCount: profileController.profiles.length,
                  itemBuilder: (BuildContext context, int i) {
                    final _profileModel = profileController.profiles[i];
                    setTextEditControllerValue(_textController, _profileModel);
    
                    return SafeArea(
                      child: Container(
                        padding: EdgeInsets.all(20),
                        child: Form(
                          child: Column(
                            children: [
                              const SizedBox(
                                height: 20,
                              ),
                              GestureDetector(
                                onTap: () {
                                  photoBottomSheet();
                                },
                                child: CircleAvatar(
                                  radius: 100,
                                  backgroundImage: NetworkImage(
                                      'https://www.alchinlong.com/wp-content/uploads/2015/09/sample-profile.png'),
                                ),
                              ),
                              const SizedBox(
                                height: 40,
                              ),
                              TextFormField(
                                  decoration: const InputDecoration(
                                    labelText: 'First Name',
                                    border: OutlineInputBorder(
                                        borderSide: BorderSide()),
                                  ),
                                  controller: _textController.fNameController),
                              SizedBox(
                                height: 10,
                              ),
                              TextFormField(
                                decoration: const InputDecoration(
                                  labelText: 'First Name',
                                  border:
                                      OutlineInputBorder(borderSide: BorderSide()),
                                ),
                                controller: _textController.lNameController,
                              ),
                              SizedBox(
                                height: 10,
                              ),
                              TextFormField(
                                decoration: const InputDecoration(
                                  labelText: 'Address',
                                  border:
                                      OutlineInputBorder(borderSide: BorderSide()),
                                ),
                                controller: _textController.adressController,
                              ),
                              SizedBox(
                                height: 10,
                              ),
                              TextFormField(
                                decoration: const InputDecoration(
                                  labelText: 'Phone Numbaer',
                                  border:
                                      OutlineInputBorder(borderSide: BorderSide()),
                                ),
                                controller: _textController.phoneController,
                              ),
                              SizedBox(
                                height: 10,
                              ),
                              TextFormField(
                                decoration: const InputDecoration(
                                  labelText: 'School Name',
                                  border:
                                      OutlineInputBorder(borderSide: BorderSide()),
                                ),
                                controller: _textController.sclNameController,
                              ),
                              SizedBox(
                                height: 10,
                              ),
                              TextFormField(
                                decoration: const InputDecoration(
                                  labelText: 'Student Class',
                                  border:
                                      OutlineInputBorder(borderSide: BorderSide()),
                                ),
                                controller: _textController.stdClassController,
                              ),
                              SizedBox(
                                height: 10,
                              ),
                              ElevatedButton(
                                  onPressed: () async {
                                    CircularProgressIndicator();
    
                                    ProfileModel profileModel =
                                        profileModelVal(_textController);
                                    await FirestoreDb.updateProfile(profileModel);
                                    Get.offAll(ProfileScreen());
                                  },
                                  child: Text('Update'))
                            ],
                          ),
                        ),
                      ),
                    );
                  }),
            );
          },
        ));
      }
    
      void photoBottomSheet() {
        Get.bottomSheet(
          Container(
            //color: Colors.white,
            height: 150,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment:
                  CrossAxisAlignment.center,
              children: [
                //Text('Select from source'),
                GestureDetector(
                  onTap: () =>
                      PickedImage(ImageSource.camera),
                  child: Row(
                    mainAxisAlignment:
                        MainAxisAlignment.center,
                    children: [
                      Icon(
                        Icons.camera,
                        size: 25,
                        color: Colors.white,
                      ),
                      SizedBox(
                        width: 10,
                      ),
                      Text(
                        'Camera',
                        style: TextStyle(fontSize: 25, color: Colors.white),
                      ),
                    ],
                  ),
                ),
                SizedBox(
                  height: 10,
                ),
                Divider(
                  thickness: 0.5, color: Colors.white,
                ),
                SizedBox(
                  height: 10,
                ),
                GestureDetector(
                  onTap: () =>
                      PickedImage(ImageSource.gallery),
                  child: Row(
                    mainAxisAlignment:
                        MainAxisAlignment.center,
                    children: [
                      Icon(
                        Icons.image,
                        size: 25,
                        color: Colors.white,
                      ),
                      SizedBox(
                        width: 10,
                      ),
                      Text(
                        'Gallery',
                        style: TextStyle(fontSize: 25, color: Colors.white),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
          backgroundColor: Color(0xff2AA8A1),
          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
    
        );
      }
    
      ProfileModel profileModelVal(ProfileEdit _textController) {
        final profileModel = ProfileModel(
            firstName: _textController.fNameController.text.trim(),
            lastName: _textController.lNameController.text.trim(),
            parentName: _textController.fatherNameController.text.trim(),
            phoneNumber: _textController.phoneController.text.trim(),
            address: _textController.adressController.text.trim(),
            schoolName: _textController.sclNameController.text.trim(),
            stdClass: _textController.stdClassController.text.trim());
        return profileModel;
      }
    
      void setTextEditControllerValue(
          ProfileEdit _textController, ProfileModel _profileModel) {
        _textController.fNameController.value =
            TextEditingValue(text: _profileModel.firstName);
        _textController.lNameController.value =
            TextEditingValue(text: _profileModel.lastName);
        _textController.fatherNameController.value =
            TextEditingValue(text: _profileModel.parentName);
        _textController.adressController.value =
            TextEditingValue(text: _profileModel.address);
        _textController.phoneController.value =
            TextEditingValue(text: _profileModel.phoneNumber);
        _textController.sclNameController.value =
            TextEditingValue(text: _profileModel.schoolName);
        _textController.stdClassController.value =
            TextEditingValue(text: _profileModel.stdClass);
      }
    
      void clearTextController(ProfileEdit _textController) {
        _textController.fNameController.clear();
        _textController.lNameController.clear();
        _textController.fatherNameController.clear();
        _textController.adressController.clear();
        _textController.phoneController.clear();
        _textController.sclNameController.clear();
        _textController.stdClassController.clear();
      }
    }

Now the method photoBottomSheet() has to be shifted to another file and I should use it in any page I want. Please help me Here.


Solution

  • create a dart file called photo_bottom_sheet and add the method there, then where ever you want to add it you will just have to import that file on the top of the dart file like so :

    import 'package:app/photo_bottom_sheet';
    

    and then add the method wwhere you need it

    photoBotomSheet()
    

    where you want it.