import
'dart:convert'
;
import
'dart:io'
;
import
'package:flutter/material.dart'
;
import
'package:image_picker/image_picker.dart'
;
import
'package:flutter_image_compress/flutter_image_compress.dart'
;
import
'package:mongo_dart/mongo_dart.dart'
show Db, GridFS;
void
main() => runApp(MyApp());
class
MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return
MaterialApp(
title:
'Geeks Demo'
,
debugShowCheckedModeBanner:
false
,
theme: ThemeData(
primarySwatch: Colors.green,
),
home: MyHomePage(title:
'GeeksforGeeks'
),
);
}
}
class
MyHomePage extends StatefulWidget {
MyHomePage({Key key,
this
.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class
_MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin{
final url = [
"mongodb:
replicaSet=<MySet>&authSource=admin&retryWrites=
true
&w=majority",
"mongodb:
replicaSet=<MySet>&authSource=admin&retryWrites=
true
&w=majority",
"mongodb:
replicaSet=<MySet>&authSource=admin&retryWrites=
true
&w=majority"
];
final picker = ImagePicker();
File _image;
GridFS bucket;
AnimationController _animationController;
Animation<Color> _colorTween;
ImageProvider provider;
var flag =
false
;
@override
void
initState() {
_animationController = AnimationController(
duration: Duration(milliseconds: 1800),
vsync:
this
,
);
_colorTween = _animationController.drive(ColorTween(begin: Colors.green, end: Colors.deepOrange));
_animationController.repeat();
super.initState();
connection();
}
Future getImage() async{
final pickedFile = await picker.getImage(source: ImageSource.gallery);
if
(pickedFile!=null){
var _cmpressed_image;
try
{
_cmpressed_image = await FlutterImageCompress.compressWithFile(
pickedFile.path,
format: CompressFormat.heic,
quality: 70
);
}
catch
(e) {
_cmpressed_image = await FlutterImageCompress.compressWithFile(
pickedFile.path,
format: CompressFormat.jpeg,
quality: 70
);
}
setState(() {
flag =
true
;
});
Map<String,dynamic> image = {
"_id"
: pickedFile.path.split(
"/"
).last,
"data"
: base64Encode(_cmpressed_image)
};
var res = await bucket.chunks.insert(image);
var img = await bucket.chunks.findOne({
"_id"
: pickedFile.path.split(
"/"
).last
});
setState(() {
provider = MemoryImage(base64Decode(img[
"data"
]));
flag =
false
;
});
}
}
@override
Widget build(BuildContext context) {
return
Scaffold(
appBar: AppBar(
title: Text(widget.title),
backgroundColor: Colors.green,
),
body: SingleChildScrollView(
child: Center(
child: Column(
children: [
SizedBox(
height: 20,
),
provider == null ? Text(
'No image selected.'
) : Image(image: provider,),
SizedBox(height: 10,),
if
(flag==
true
)
CircularProgressIndicator(valueColor: _colorTween),
SizedBox(height: 20,),
RaisedButton(
onPressed: getImage,
textColor: Colors.white,
padding:
const
EdgeInsets.all(0.0),
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: <Color>[
Colors.green,
Colors.green[200],
Colors.green[900],
],
),
),
padding:
const
EdgeInsets.all(10.0),
child:
const
Text(
'Select Image'
,
style: TextStyle(fontSize: 20)
),
),
),
],
),
)
)
);
}
Future connection () async{
Db _db =
new
Db.pool(url);
await _db.open(secure:
true
);
bucket = GridFS(_db,
"image"
);
}
}