I use Flutter and added Image Picker and Share Plus. Image Picker working but doesnt work Share Plus? I dont understand why doesnt working share plus. Can you looking?
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:image_picker/image_picker.dart';
import 'package:share_plus/share_plus.dart';
void main() {
runApp(share1());
}
class share1 extends StatelessWidget {
const share1({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Image Share",
home: share2(),
);
}
}
class share2 extends StatefulWidget {
const share2({Key? key}) : super(key: key);
@override
State<share2> createState() => _share2State();
}
class _share2State extends State<share2> {
@override
void initState() {
// TODO: implement initState
super.initState();
}
File? _image;
Future getImage(ImageSource source) async {
try {
final image=await ImagePicker().pickImage(source: source);
if(image==null) return;
final imageTemporary = File(image.path);
setState(() {
this._image=imageTemporary;
Share.share(_image);
});
} on PlatformException catch (e) {
print('Failed to pick image : $e');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(centerTitle:true,title: Text("Image Share"),),
body: Center(child: Column(children: [
SizedBox(height: 40,),
_image != null ? Image.file(_image!,width: 250,height: 250,fit: BoxFit.cover,)
: Image.network('https://picsum.photos/250?image=9'),
SizedBox(height: 40,),
ElevatedButton(onPressed: ()=> getImage(ImageSource.gallery), child: Text("Resim Seç")),
Text("Image Share"),
],),),
);
}
}
Purpose of the project : Adding and sharing pictures from the gallery. However, I couldn't get it to run.Any idea where is wrong with my code and what should I do?