Skip to content

Instantly share code, notes, and snippets.

@btastic
Created June 4, 2019 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save btastic/14a3784a2b62f57911066ea7d30363ff to your computer and use it in GitHub Desktop.
Save btastic/14a3784a2b62f57911066ea7d30363ff to your computer and use it in GitHub Desktop.
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:flutter_native_image/flutter_native_image.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
Future _getImageGallery() async {
File imageFile = await ImagePicker.pickImage(source: ImageSource.gallery);
FocusScope.of(context).requestFocus(new FocusNode());
if (imageFile != null) {
await imageUpload(imageFile);
}
}
imageUpload(imageFile) async {
ImageProperties properties = await FlutterNativeImage.getImageProperties(imageFile.path);
File compressedFile = await FlutterNativeImage.compressImage(imageFile.path,
quality: 100,
targetWidth: 300,
targetHeight: (properties.height * 300 / properties.width).round());
String fileName = DateTime.now().millisecondsSinceEpoch.toString() + '.jpg';
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () async => await _getImageGallery(),
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment