mirror of
https://git.meshkee.com/applications/cssd.git
synced 2026-10-13 06:08:56 +03:30
commit
This commit is contained in:
@@ -0,0 +1,313 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:camera/camera.dart';
|
||||
import 'package:cssd/providers/test_results.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class CaptureImage extends StatefulWidget {
|
||||
@override
|
||||
_CaptureImageState createState() {
|
||||
return _CaptureImageState();
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a suitable camera icon for [direction].
|
||||
IconData getCameraLensIcon(CameraLensDirection direction) {
|
||||
switch (direction) {
|
||||
case CameraLensDirection.back:
|
||||
return Icons.camera_rear;
|
||||
case CameraLensDirection.front:
|
||||
return Icons.camera_front;
|
||||
case CameraLensDirection.external:
|
||||
return Icons.camera;
|
||||
}
|
||||
throw ArgumentError('Unknown lens direction');
|
||||
}
|
||||
|
||||
void logError(String code, String message) =>
|
||||
print('Error: $code\nError Message: $message');
|
||||
|
||||
class _CaptureImageState extends State<CaptureImage>
|
||||
with WidgetsBindingObserver {
|
||||
CameraController controller;
|
||||
String imagePath;
|
||||
// String videoPath;
|
||||
VoidCallback videoPlayerListener;
|
||||
bool isInit = true;
|
||||
int curCamera = 0;
|
||||
var cameras;
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
WidgetsBinding.instance.addObserver(this);
|
||||
}
|
||||
|
||||
@override
|
||||
void didChangeDependencies() async {
|
||||
if (isInit) {
|
||||
cameras = await availableCameras();
|
||||
onNewCameraSelected(cameras[curCamera]);
|
||||
setState(() {
|
||||
isInit = false;
|
||||
});
|
||||
}
|
||||
super.didChangeDependencies();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
WidgetsBinding.instance.removeObserver(this);
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
void didChangeAppLifecycleState(AppLifecycleState state) {
|
||||
// App state changed before we got the chance to initialize.
|
||||
if (controller == null || !controller.value.isInitialized) {
|
||||
return;
|
||||
}
|
||||
if (state == AppLifecycleState.inactive) {
|
||||
controller?.dispose();
|
||||
} else if (state == AppLifecycleState.resumed) {
|
||||
if (controller != null) {
|
||||
onNewCameraSelected(controller.description);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return SafeArea(
|
||||
child: Scaffold(
|
||||
key: _scaffoldKey,
|
||||
// appBar: AppBar(
|
||||
// title: const Text('Take Picture'),
|
||||
// ),
|
||||
body: isInit
|
||||
? Center(
|
||||
child: CircularProgressIndicator(),
|
||||
)
|
||||
: Stack(children: <Widget>[
|
||||
_cameraPreviewWidget(),
|
||||
Positioned(
|
||||
bottom: 100, left: 100, child: _captureControlRowWidget()),
|
||||
Positioned(
|
||||
bottom: 100,
|
||||
right: 100,
|
||||
child: InkWell(
|
||||
onTap: () {
|
||||
if (curCamera == 0) {
|
||||
try {
|
||||
onNewCameraSelected(cameras[1]);
|
||||
setState(() {
|
||||
curCamera = 1;
|
||||
});
|
||||
} catch (e) {}
|
||||
} else {
|
||||
onNewCameraSelected(cameras[0]);
|
||||
setState(() {
|
||||
curCamera = 0;
|
||||
});
|
||||
}
|
||||
},
|
||||
child: Icon(
|
||||
curCamera == 0 ? Icons.camera_rear : Icons.camera_front,
|
||||
size: 40,
|
||||
color: Colors.white,
|
||||
),
|
||||
),
|
||||
),
|
||||
// _cameraTogglesRowWidget(cameras),
|
||||
]),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// Display the preview from the camera (or a message if the preview is not available).
|
||||
Widget _cameraPreviewWidget() {
|
||||
if (controller == null || !controller.value.isInitialized) {
|
||||
return const Text(
|
||||
'Tap a camera',
|
||||
style: TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 24.0,
|
||||
fontWeight: FontWeight.w900,
|
||||
),
|
||||
);
|
||||
} else {
|
||||
return Container(
|
||||
width: double.infinity,
|
||||
child: CameraPreview(controller),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Toggle recording audio
|
||||
|
||||
/// Display the control bar with buttons to take pictures and record videos.
|
||||
Widget _captureControlRowWidget() {
|
||||
return Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
||||
mainAxisSize: MainAxisSize.max,
|
||||
children: <Widget>[
|
||||
IconButton(
|
||||
icon: const Icon(
|
||||
Icons.camera_alt,
|
||||
size: 40,
|
||||
color: Colors.white,
|
||||
),
|
||||
// color: Colors.blue,
|
||||
onPressed: controller != null &&
|
||||
controller.value.isInitialized &&
|
||||
!controller.value.isRecordingVideo
|
||||
? onTakePictureButtonPressed
|
||||
: null,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
getCameras() async {
|
||||
return await availableCameras();
|
||||
}
|
||||
|
||||
/// Display a row of toggle to select the camera (or a message if no camera is available).
|
||||
// Widget _cameraTogglesRowWidget(var cameras) {
|
||||
// final List<Widget> toggles = <Widget>[];
|
||||
|
||||
// if (cameras.isEmpty) {
|
||||
// return const Text('No camera found');
|
||||
// } else {
|
||||
// for (CameraDescription cameraDescription in cameras) {
|
||||
// print("${cameraDescription.name}");
|
||||
// toggles.add(
|
||||
// SizedBox(
|
||||
// width: 90.0,
|
||||
// child: RadioListTile<CameraDescription>(
|
||||
// title: Icon(getCameraLensIcon(cameraDescription.lensDirection)),
|
||||
// groupValue: controller?.description,
|
||||
// value: cameraDescription,
|
||||
// onChanged: controller != null && controller.value.isRecordingVideo
|
||||
// ? null
|
||||
// : onNewCameraSelected,
|
||||
// ),
|
||||
// ),
|
||||
// );
|
||||
// }
|
||||
// }
|
||||
|
||||
// return Row(children: toggles);
|
||||
// }
|
||||
|
||||
String timestamp() => DateTime.now().millisecondsSinceEpoch.toString();
|
||||
|
||||
void showInSnackBar(String message) {
|
||||
_scaffoldKey.currentState.showSnackBar(SnackBar(content: Text(message)));
|
||||
}
|
||||
|
||||
void onNewCameraSelected(CameraDescription cameraDescription) async {
|
||||
print(cameraDescription.lensDirection.toString());
|
||||
if (controller != null) {
|
||||
await controller.dispose();
|
||||
}
|
||||
controller = CameraController(
|
||||
cameraDescription,
|
||||
ResolutionPreset.medium,
|
||||
// enableAudio: enableAudio,
|
||||
);
|
||||
|
||||
// If the controller is updated then update the UI.
|
||||
controller.addListener(() {
|
||||
if (mounted) setState(() {});
|
||||
if (controller.value.hasError) {
|
||||
showInSnackBar('Camera error ${controller.value.errorDescription}');
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
await controller.initialize();
|
||||
} on CameraException catch (e) {
|
||||
_showCameraException(e);
|
||||
}
|
||||
|
||||
if (mounted) {
|
||||
setState(() {});
|
||||
}
|
||||
}
|
||||
|
||||
void onTakePictureButtonPressed() {
|
||||
takePicture().then((String filePath) {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
imagePath = filePath;
|
||||
var profileProviderObject =
|
||||
Provider.of<TestResultsProvider>(context, listen: false);
|
||||
List<File> files = profileProviderObject.catchImages;
|
||||
files.add(File(imagePath));
|
||||
profileProviderObject.setCatchImages(files);
|
||||
profileProviderObject.setImages();
|
||||
print(imagePath);
|
||||
});
|
||||
if (filePath != null) {
|
||||
Navigator.of(context).pop();
|
||||
// showInSnackBar('Picture saved to $filePath');
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Future<String> takePicture() async {
|
||||
if (!controller.value.isInitialized) {
|
||||
showInSnackBar('Error: select a camera first.');
|
||||
return null;
|
||||
}
|
||||
final Directory extDir = await getApplicationDocumentsDirectory();
|
||||
final String dirPath = '${extDir.path}/Pictures/flutter_test';
|
||||
await Directory(dirPath).create(recursive: true);
|
||||
final String filePath = '$dirPath/${timestamp()}.jpg';
|
||||
|
||||
if (controller.value.isTakingPicture) {
|
||||
// A capture is already pending, do nothing.
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
await controller.takePicture(filePath);
|
||||
} on CameraException catch (e) {
|
||||
_showCameraException(e);
|
||||
return null;
|
||||
}
|
||||
return filePath;
|
||||
}
|
||||
|
||||
void _showCameraException(CameraException e) {
|
||||
logError(e.code, e.description);
|
||||
showInSnackBar('Error: ${e.code}\n${e.description}');
|
||||
}
|
||||
}
|
||||
|
||||
// class CameraApp extends StatelessWidget {
|
||||
// @override
|
||||
// Widget build(BuildContext context) {
|
||||
// return MaterialApp(
|
||||
// home: CaptureImage(),
|
||||
// );
|
||||
// }
|
||||
// }
|
||||
|
||||
// List<CameraDescription> cameras = [];
|
||||
|
||||
// Future<void> main() async {
|
||||
// // Fetch the available cameras before initializing the app.
|
||||
// try {
|
||||
// WidgetsFlutterBinding.ensureInitialized();
|
||||
// } on CameraException catch (e) {
|
||||
// logError(e.code, e.description);
|
||||
// }
|
||||
// runApp(CameraApp());
|
||||
// }
|
||||
@@ -1,8 +1,16 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:cssd/config/routes.dart';
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/rendering.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:image_picker_gallery_camera/image_picker_gallery_camera.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
import 'package:multiselect_formfield/multiselect_formfield.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../../../providers/test_results.dart';
|
||||
|
||||
class FormCreator extends StatefulWidget {
|
||||
const FormCreator({
|
||||
@@ -60,7 +68,82 @@ class _FormCreatorState extends State<FormCreator> {
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if (item['type'] == 'Image') {
|
||||
final testProviderObject = Provider.of<TestResultsProvider>(context);
|
||||
final mediaData = MediaQuery.of(context);
|
||||
final deviceWidth = mediaData.size.width;
|
||||
final _deviceBlockSize = deviceWidth / 100;
|
||||
listWidgets.insert(
|
||||
3,
|
||||
Container(
|
||||
// height: 70,
|
||||
child: Directionality(
|
||||
textDirection: TextDirection.rtl,
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
crossAxisAlignment: CrossAxisAlignment.end,
|
||||
children: <Widget>[
|
||||
Container(
|
||||
margin: EdgeInsets.only(top: 20),
|
||||
height: testProviderObject.catchImages.length != 0 ? 60 : 0,
|
||||
child: ImageBox(
|
||||
deviceBlockSize: _deviceBlockSize,
|
||||
testProvider: testProviderObject,
|
||||
),
|
||||
),
|
||||
SizedBox(
|
||||
height: testProviderObject.catchImages.length != 0 ? 10 : 0,
|
||||
),
|
||||
InkWell(
|
||||
onTap: () async {
|
||||
await loadAssets(context);
|
||||
},
|
||||
child: Container(
|
||||
width: MediaQuery.of(context).size.width - 40,
|
||||
padding:
|
||||
EdgeInsets.symmetric(vertical: 10, horizontal: 30),
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(5),
|
||||
color: Colors.black,
|
||||
),
|
||||
child: Text(
|
||||
'+ Add an image or file from device',
|
||||
textDirection: TextDirection.ltr,
|
||||
style: TextStyle(color: Colors.white),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
),
|
||||
),
|
||||
SizedBox(
|
||||
height: 10,
|
||||
),
|
||||
InkWell(
|
||||
onTap: () async {
|
||||
await Navigator.of(context).pushNamed(Routes.captureImageScreen);
|
||||
// await captureImage(context);
|
||||
},
|
||||
child: Container(
|
||||
width: MediaQuery.of(context).size.width - 40,
|
||||
padding:
|
||||
EdgeInsets.symmetric(vertical: 10, horizontal: 30),
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(5),
|
||||
color: Colors.black,
|
||||
),
|
||||
child: Text(
|
||||
'+ Add an image from camera',
|
||||
textDirection: TextDirection.ltr,
|
||||
style: TextStyle(color: Colors.white),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
if (item['type'] == "Label") {
|
||||
listWidgets.add(
|
||||
Container(
|
||||
@@ -113,26 +196,39 @@ class _FormCreatorState extends State<FormCreator> {
|
||||
}
|
||||
|
||||
formItems[count]['value'] = formItems[count]['value'] ?? startValue;
|
||||
int divisions = (endValue - startValue).round();
|
||||
// int divisions = (endValue - startValue).round();
|
||||
listWidgets.add(
|
||||
Padding(
|
||||
padding: EdgeInsets.only(top: 35),
|
||||
child: Slider(
|
||||
min: startValue,
|
||||
max: endValue,
|
||||
value: formItems[count]['value'],
|
||||
label: '${formItems[count]['value'].round()}',
|
||||
divisions: divisions,
|
||||
onChanged: (double value) {
|
||||
setState(() {
|
||||
formItems[count]['value'] = value;
|
||||
});
|
||||
},
|
||||
semanticFormatterCallback: (double newValue) {
|
||||
return '${newValue.round()}';
|
||||
},
|
||||
),
|
||||
),
|
||||
padding: EdgeInsets.only(top: 35, left: 10, right: 10),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: <Widget>[
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: <Widget>[
|
||||
Text(item['type']),
|
||||
Text(formItems[count]['value'].toString()),
|
||||
],
|
||||
),
|
||||
Slider(
|
||||
min: startValue,
|
||||
max: endValue,
|
||||
value: formItems[count]['value'],
|
||||
// label: '${formItems[count]['value'].round()}',
|
||||
// divisions: divisions,
|
||||
onChanged: (double value) {
|
||||
setState(() {
|
||||
formItems[count]['value'] =
|
||||
double.parse(value.toStringAsFixed(2));
|
||||
});
|
||||
// print(formItems[count]['value']);
|
||||
},
|
||||
// semanticFormatterCallback: (double newValue) {
|
||||
// return '${newValue.round()}';
|
||||
// },
|
||||
),
|
||||
],
|
||||
)),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -353,9 +449,51 @@ class _FormCreatorState extends State<FormCreator> {
|
||||
// ),
|
||||
// );
|
||||
}
|
||||
|
||||
return listWidgets;
|
||||
}
|
||||
|
||||
Future captureImage(BuildContext context) async {
|
||||
var profileProviderObject =
|
||||
Provider.of<TestResultsProvider>(context, listen: false);
|
||||
List<File> files = profileProviderObject.catchImages;
|
||||
try {
|
||||
files.add(
|
||||
await ImagePickerGC.pickImage(
|
||||
context: context,
|
||||
source: ImgSource.Camera,
|
||||
cameraIcon: Icon(
|
||||
Icons.camera_alt,
|
||||
color: Colors.red,
|
||||
), //cameraIcon and galleryIcon can change. If no icon provided default icon will be present
|
||||
cameraText: Text(
|
||||
"From Camera",
|
||||
style: TextStyle(color: Colors.red),
|
||||
),
|
||||
),
|
||||
);
|
||||
} catch (e) {}
|
||||
profileProviderObject.setCatchImages(files);
|
||||
profileProviderObject.setImages();
|
||||
}
|
||||
|
||||
Future<void> loadAssets(BuildContext context) async {
|
||||
var profileProviderObject =
|
||||
Provider.of<TestResultsProvider>(context, listen: false);
|
||||
List<File> files = profileProviderObject.catchImages;
|
||||
try {
|
||||
files += await FilePicker.getMultiFile(
|
||||
type: FileType.custom,
|
||||
allowedExtensions: ['jpg', 'png', 'bmp', 'jpeg', 'pdf', 'docx', 'doc'],
|
||||
);
|
||||
// print(files);
|
||||
} catch (_) {}
|
||||
|
||||
profileProviderObject.setCatchImages(files);
|
||||
profileProviderObject.setImages();
|
||||
// profileProviderObject.addImage();
|
||||
}
|
||||
|
||||
_FormCreatorState(this.formItems);
|
||||
|
||||
void _handleChanged() {
|
||||
@@ -377,3 +515,110 @@ class _FormCreatorState extends State<FormCreator> {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ImageBox extends StatelessWidget {
|
||||
const ImageBox({
|
||||
Key key,
|
||||
@required double deviceBlockSize,
|
||||
@required TestResultsProvider testProvider,
|
||||
}) : _deviceBlockSize = deviceBlockSize,
|
||||
_testProvider = testProvider,
|
||||
super(key: key);
|
||||
|
||||
final double _deviceBlockSize;
|
||||
final TestResultsProvider _testProvider;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ListView.separated(
|
||||
shrinkWrap: true,
|
||||
scrollDirection: Axis.horizontal,
|
||||
itemBuilder: (ctx, index) {
|
||||
bool isPdf = _testProvider.catchImages[index].path.endsWith('pdf');
|
||||
bool isWord = _testProvider.catchImages[index].path.endsWith('docx') ||
|
||||
_testProvider.catchImages[index].path.endsWith('doc');
|
||||
|
||||
// print(_testProvider.catchImages[index].path.endsWith('jpg'));
|
||||
// .substring(_testProvider.catchImages[index].path.length - 4));
|
||||
return ClipRRect(
|
||||
borderRadius: BorderRadius.circular(5),
|
||||
child: Container(
|
||||
width: 60,
|
||||
height: 60,
|
||||
child: Stack(
|
||||
children: <Widget>[
|
||||
Positioned(
|
||||
top: 10,
|
||||
left: 0,
|
||||
child: GestureDetector(
|
||||
onTap: () => isPdf
|
||||
? print('pdf')
|
||||
: isWord
|
||||
? print('word')
|
||||
: Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (_) => Container(
|
||||
color: Colors.white,
|
||||
child: Center(
|
||||
child: Image.file(
|
||||
_testProvider.catchImages[index],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
child: isPdf
|
||||
? Image.asset(
|
||||
'assets/Images/pdf.png',
|
||||
width: 60,
|
||||
height: 60,
|
||||
fit: BoxFit.fill,
|
||||
)
|
||||
: isWord
|
||||
? Image.asset(
|
||||
'assets/Images/word.png',
|
||||
width: 60,
|
||||
height: 60,
|
||||
fit: BoxFit.fill,
|
||||
)
|
||||
: Image.file(
|
||||
_testProvider.catchImages[index],
|
||||
// asset: _images[index],
|
||||
width: 60,
|
||||
height: 60,
|
||||
fit: BoxFit.fill,
|
||||
),
|
||||
),
|
||||
),
|
||||
Positioned(
|
||||
top: 0,
|
||||
right: 0,
|
||||
child: GestureDetector(
|
||||
onTap: () {
|
||||
// setState(() {
|
||||
_testProvider.removeImage(index);
|
||||
// });
|
||||
},
|
||||
child: CircleAvatar(
|
||||
backgroundColor: Colors.red,
|
||||
radius: 10,
|
||||
child: Icon(
|
||||
Icons.close,
|
||||
size: 12,
|
||||
color: Colors.white,
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
itemCount: _testProvider.catchImages.length,
|
||||
separatorBuilder: (BuildContext context, int index) => SizedBox(
|
||||
width: 3 * _deviceBlockSize,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,7 +82,7 @@ class _TestResultAddSelectorBoxState extends State<TestResultAddSelectorBox> {
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.w700,
|
||||
color: Colors.black,
|
||||
fontSize: 4 * widget._deviceBlockSize,
|
||||
fontSize: 3 * widget._deviceBlockSize,
|
||||
),
|
||||
),
|
||||
// TextFormField(
|
||||
@@ -106,9 +106,11 @@ class _TestResultAddSelectorBoxState extends State<TestResultAddSelectorBox> {
|
||||
child: Container(
|
||||
child: Text(
|
||||
val,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: TextStyle(
|
||||
// color: _mainFontColor,
|
||||
fontSize: 4 * widget._deviceBlockSize,
|
||||
fontWeight: FontWeight.w700,
|
||||
color: Colors.black,
|
||||
fontSize: 3 * widget._deviceBlockSize,
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user