Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .flutter-plugins-dependencies

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions lib/screens/register/cubit/register_cubit.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import 'dart:io';

import 'package:bloc/bloc.dart';
import 'package:dio/dio.dart';
import 'package:flutter_osm_plugin/flutter_osm_plugin.dart';
import 'package:image_cropper/image_cropper.dart';
import 'package:image_picker/image_picker.dart';
import 'package:meta/meta.dart';
import 'package:watch_store/data/constants.dart';
import 'package:watch_store/data/model/user.dart';
Expand Down Expand Up @@ -53,4 +57,31 @@ class RegisterCubit extends Cubit<RegisterState> {
emit(ErrorState());
}
}



Future<void> pickAndCropImage({required ImageSource source}) async {
File _image;
try {
final image = await ImagePicker().pickImage(source: source);
if (image == null) {
emit(CancelImageState());
}
File? img = File(image!.path);
img = await _cropImage(imageFile: img);
_image = img!;
emit(IntialImageState(image: _image));
} catch (e) {
emit(CancelImageState());
}
}

Future<File?> _cropImage({required File imageFile}) async {
CroppedFile? croppedImage =
await ImageCropper().cropImage(sourcePath: imageFile.path);
if (imageFile == null){
emit(CancelImageState());
}
return File(croppedImage!.path);
}
}
6 changes: 6 additions & 0 deletions lib/screens/register/cubit/register_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ final class LoadingState extends RegisterState {}

final class ErrorState extends RegisterState {}

final class IntialImageState extends RegisterState {
File image;
IntialImageState({required this.image});
}

final class CancelImageState extends RegisterState {}
final class OkResponseState extends RegisterState {}

final class LocationPickedState extends RegisterState {
Expand Down
110 changes: 96 additions & 14 deletions lib/screens/register/register_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/container.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:image_picker/image_picker.dart';
import 'package:watch_store/component/text_style.dart';
import 'package:watch_store/component/button_style.dart';
import 'package:watch_store/data/model/user.dart';
import 'package:watch_store/res/dimens.dart';
import 'package:watch_store/res/strings.dart';
import 'package:watch_store/screens/register/cubit/register_cubit.dart';
import 'package:watch_store/utils/image_handler.dart';
import 'package:watch_store/widgets/app_text_field.dart';
import 'package:watch_store/widgets/avatar.dart';
import 'package:watch_store/widgets/main_button.dart';
import 'package:watch_store/component/extention.dart';
import 'package:watch_store/widgets/registeration_app_bar.dart';

import 'package:geocoding/geocoding.dart';
import '../../component/text_style.dart';
import '../../res/colors.dart';
import '../../route/names.dart';

//sasansafari_dev
Expand All @@ -34,8 +35,7 @@ class _RegisterScreenState extends State<RegisterScreen> {
TextEditingController _controllerLocation = TextEditingController();
double lat = 0.0;
double lng = 0.0;

ImageHandler imageHandler = ImageHandler();
var image = null;

@override
Widget build(BuildContext context) {
Expand All @@ -54,11 +54,24 @@ class _RegisterScreenState extends State<RegisterScreen> {
crossAxisAlignment: CrossAxisAlignment.center,
children: [
AppDimens.large.height,
Avatar(
onTap: () async => await imageHandler
.pickAndCropImage(source: ImageSource.gallery)
.then((value) => setState(() {})),
file: imageHandler.getImage),
BlocConsumer<RegisterCubit, RegisterState>(
listener: (contxt, state) {
if (state is IntialImageState) {
image = state.image;
}
if (state is CancelImageState) {
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text('Cancel')));
}
},
builder: (contxt, state) {
return Avatar(
onTap: () {
galleryOrcamera(context, size, contxt);
},
file: image);
},
),
AppTextField(
lable: AppStrings.nameLastName,
hint: AppStrings.hintNameLastName,
Expand All @@ -76,11 +89,10 @@ class _RegisterScreenState extends State<RegisterScreen> {
hint: AppStrings.hintPostalCode,
controller: _controllerPostalCode),
BlocConsumer<RegisterCubit, RegisterState>(
listener: (context, state) {
listener: (context, state) async {
if (state is LocationPickedState) {
if (state.location != null) {
_controllerLocation.text =
'${state.location!.latitude} - ${state.location!.longitude}';
_controllerLocation.text =await getAddress(state.location!.longitude ,state.location!.latitude);

lat = state.location!.latitude;
lng = state.location!.longitude;
Expand Down Expand Up @@ -126,7 +138,7 @@ class _RegisterScreenState extends State<RegisterScreen> {
address: _controllerAddress.text,
postalCode: _controllerPostalCode.text,
image: await MultipartFile.fromFile(
imageHandler.getImage!.path),
image.path),
lat: lat,
lng: lng);

Expand All @@ -146,4 +158,74 @@ class _RegisterScreenState extends State<RegisterScreen> {
),
);
}
Future<String> getAddress(double lng, double lat) async {
String address = '';
try {
await placemarkFromCoordinates(lat,lng,localeIdentifier: 'fa')
.then((value) {
Placemark first = value.first;

address = '${first.street}' '${first.locality}' '${first.country}';
});
print(lat.toString());
print(lng.toString());
return address;
} catch (e) {
return 'آدرس یافت نشد';
}
}

Future<dynamic> galleryOrcamera(
BuildContext context, Size size, BuildContext contxt) {
return showModalBottomSheet(
context: context,
builder: (context) => Container(
height: size.height / 5,
width: double.infinity,
decoration: const BoxDecoration(
borderRadius: BorderRadius.only(
topRight: Radius.circular(30), topLeft: Radius.circular(30)),
color: Colors.white),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
width: size.width / 1.8,
child: ElevatedButton(
style: AppButtonStyles.mainButtonStyle.copyWith(
backgroundColor: const MaterialStatePropertyAll(
AppColors.primaryColor)),
onPressed: () async =>
await BlocProvider.of<RegisterCubit>(contxt)
.pickAndCropImage(source: ImageSource.gallery),
child: const Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Icon(Icons.photo_library, color: Colors.white),
Text('انتخاب عکس از گالری',
style: AppTextStyles.mainbuttn),
],
))),
SizedBox(
width: size.width / 1.8,
child: ElevatedButton(
style: AppButtonStyles.mainButtonStyle.copyWith(
backgroundColor: const MaterialStatePropertyAll(
AppColors.primaryColor)),
onPressed: () async =>
await BlocProvider.of<RegisterCubit>(contxt)
.pickAndCropImage(source: ImageSource.camera),
child: const Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Icon(Icons.camera_alt_outlined, color: Colors.white),
Text('انتخاب عکس از دوربین',
style: AppTextStyles.mainbuttn),
],
)),
),
],
)),
);
}
}
32 changes: 32 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,38 @@ packages:
url: "https://pub.dev"
source: hosted
version: "3.2.0"
geocoding:
dependency: "direct main"
description:
name: geocoding
sha256: e1dc0ac56666d9ed1d5a9ae5543ce9eb5986db6209cc7600103487d09192059c
url: "https://pub.dev"
source: hosted
version: "2.1.1"
geocoding_android:
dependency: transitive
description:
name: geocoding_android
sha256: "609db1d71bc364dd9d0616f72a41c01e0c74f3a3807efb85e0d5a67e57baf50f"
url: "https://pub.dev"
source: hosted
version: "2.1.2"
geocoding_ios:
dependency: transitive
description:
name: geocoding_ios
sha256: "8f79e380abb640ef4d88baee8bb65390058c802601158d0813dc990b36b189d2"
url: "https://pub.dev"
source: hosted
version: "2.1.1"
geocoding_platform_interface:
dependency: transitive
description:
name: geocoding_platform_interface
sha256: "8848605d307d844d89937cdb4b8ad7dfa880552078f310fa24d8a460f6dddab4"
url: "https://pub.dev"
source: hosted
version: "2.0.1"
glob:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ dependencies:
image_cropper: ^4.0.1
image_picker: ^1.0.7
flutter_osm_plugin: ^0.70.4
geocoding: ^2.1.1



Expand Down