Skip to content
Snippets Groups Projects
Select Git revision
  • 1c32b2797fb266616499c9352d188876d4ab2637
  • master default protected
  • 12-improve-ai
  • 14-improve-app-metadata
  • Release_0.8.1_26 protected
  • Release_0.8.0_25 protected
  • Release_0.7.2_24 protected
  • Release_0.7.1_23 protected
  • Release_0.7.0_22 protected
  • Release_0.6.0_21 protected
  • Release_0.5.0_20 protected
  • Release_0.4.0_19 protected
  • Release_0.3.2_18 protected
  • Release_0.3.1_17 protected
  • Release_0.3.0_16 protected
  • Release_0.2.1_15 protected
  • Release_0.2.0_14 protected
  • Release_0.1.1_13 protected
  • Release_0.1.0_12 protected
  • Release_0.0.11_11 protected
  • Release_0.0.10_10 protected
  • Release_0.0.9_9 protected
  • Release_0.0.8_8 protected
  • Release_0.0.7_7 protected
24 results

outlined_text_widget.dart

Blame
  • take_picture_widget.dart 3.25 KiB
    import 'dart:io';
    
    import 'package:camera/camera.dart';
    import 'package:flutter/material.dart';
    import 'package:flutter_custom_toolbox/flutter_toolbox.dart';
    
    import 'package:random/utils/picture_storage.dart';
    
    class TakePictureWidget extends StatefulWidget {
      const TakePictureWidget({super.key});
    
      @override
      TakePictureWidgetState createState() => TakePictureWidgetState();
    }
    
    class TakePictureWidgetState extends State<TakePictureWidget> {
      CameraController? controller;
      PictureStorage? storage;
    
      List<String> previousImages = [];
      List<String> debug = [];
    
      @override
      void initState() {
        loadCamera();
        storage = PictureStorage();
        super.initState();
      }
    
      loadCamera() async {
        final List<CameraDescription> cameras = await availableCameras();
        controller = CameraController(
          cameras.first,
          ResolutionPreset.max,
          enableAudio: false,
        );
    
        controller!.initialize().then((_) {
          if (!mounted) {
            return;
          }
          setState(() {});
        });
      }
    
      @override
      void dispose() {
        controller!.dispose();
        super.dispose();
      }
    
      @override
      Widget build(BuildContext context) {
        return Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              SizedBox(
                height: 400,
                child: controller == null
                    ? const Center(child: Text("Loading camera..."))
                    : !controller!.value.isInitialized
                        ? const Center(child: CircularProgressIndicator())
                        : CameraPreview(controller!),
              ),
              ElevatedButton.icon(
                label: const Text("Take picture"),
                icon: const Icon(UniconsLine.camera),
                onPressed: () async {
                  try {
                    if ((controller != null) && (controller!.value.isInitialized)) {
                      final XFile image = await controller!.takePicture();
                      printlog('image.path: ${image.path}');
                      debug.add('image.path: ${image.path}');
    
                      File savedFile = await storage!.writeCounter(File(image.path));
                      debug.add('image.path: ${image.path}');
    
                      String imagePath = savedFile.path;
                      printlog('imagePath: $imagePath');
                      debug.add('imagePath: $imagePath');
    
                      previousImages.add(imagePath);
                      setState(() {});
                    }
                  } catch (e) {
                    debug.add('error: $e');
                    setState(() {});
    
                    printlog(e.toString());
                  }
                },
              ),
              const Text('debug: '),
              Column(
                children: debug.map((String line) {
                  return Text(line);
                }).toList(),
              ),
              previousImages.isEmpty
                  ? const Text('no previous images')
                  : Column(
                      children: previousImages.map((String imagePath) {
                        return Row(
                          children: [
                            // Image.file(File(imagePath)),
                            Text(imagePath),
                          ],
                        );
                      }).toList(),
                    ),
            ],
          ),
        );
      }
    }