From 162fe15141ee84c31909b199f7b43cbd8ed7cdfb Mon Sep 17 00:00:00 2001 From: Schummi C Date: Thu, 23 Feb 2023 16:57:52 +0100 Subject: [PATCH] Image_picker addet --- lib/AddPage.dart | 158 ++++++++++++++++++++++++++++++++++++++++++++++- pubspec.lock | 90 +++++++++++++++++++++++++++ pubspec.yaml | 1 + 3 files changed, 248 insertions(+), 1 deletion(-) diff --git a/lib/AddPage.dart b/lib/AddPage.dart index 65045c6..761a1b9 100644 --- a/lib/AddPage.dart +++ b/lib/AddPage.dart @@ -1,4 +1,4 @@ -import 'package:flutter/material.dart'; +/*import 'package:flutter/material.dart'; class AddScreen extends StatefulWidget { @override @@ -107,8 +107,164 @@ class _AddScreenState extends State { ), ); } +}*/ + +import 'dart:io'; + +import 'package:flutter/material.dart'; +import 'package:image_picker/image_picker.dart'; + +class AddScreen extends StatefulWidget { + @override + _AddScreenState createState() => _AddScreenState(); } +class _AddScreenState extends State { + final _formKey = GlobalKey(); + final _titleController = TextEditingController(); + final _descriptionController = TextEditingController(); + File? _imageFile; + + Future _getImage(ImageSource source) async { + final pickedFile = await ImagePicker().getImage(source: source); + + if (pickedFile != null) { + setState(() { + _imageFile = File(pickedFile.path); + }); + } + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: Text('Neues Element hinzufügen'), + actions: [ + Padding( + padding: const EdgeInsets.only(right: 8.0), + child: IconButton( + icon: Icon(Icons.refresh), + onPressed: () { + _titleController.clear(); + _descriptionController.clear(); + setState(() { + _imageFile = null; + }); + }, + ), + ), + Padding( + padding: const EdgeInsets.only(right: 8.0), + child: IconButton( + icon: Icon(Icons.check), + onPressed: () { + if (_formKey.currentState!.validate()) { + // Hier wird die Logik zum Hinzufügen des neuen Elements ausgeführt + Navigator.pop(context); + } + }, + ), + ), + ], + ), + body: Padding( + padding: const EdgeInsets.all(16.0), + child: Form( + key: _formKey, + child: Column( + children: [ + TextFormField( + decoration: InputDecoration( + labelText: 'Titel', + filled: true, + fillColor: Colors.grey[200], + border: OutlineInputBorder( + borderRadius: BorderRadius.circular(16.0), + ), + ), + controller: _titleController, + validator: (value) { + if (value!.isEmpty) { + return 'Bitte geben Sie einen Titel ein.'; + } + return null; + }, + ), + SizedBox(height: 16.0), + TextFormField( + decoration: InputDecoration( + labelText: 'Beschreibung', + filled: true, + fillColor: Colors.grey[200], + border: OutlineInputBorder( + borderRadius: BorderRadius.circular(16.0), + ), + ), + controller: _descriptionController, + validator: (value) { + if (value!.isEmpty) { + return 'Bitte geben Sie eine Beschreibung ein.'; + } + return null; + }, + ), + SizedBox(height: 16.0), + _imageFile != null + ? Image.file(_imageFile!) + : Container( + height: 150.0, + decoration: BoxDecoration( + color: Colors.grey[200], + borderRadius: BorderRadius.circular(16.0), + ), + child: Center( + child: TextButton( + onPressed: () { + showModalBottomSheet( + context: context, + builder: (context) => Column( + mainAxisSize: MainAxisSize.min, + children: [ + ListTile( + leading: Icon(Icons.photo_library), + title: Text ('Galerie auswählen'), + onTap: () { + Navigator.pop(context); + _getImage(ImageSource.gallery); + }, + ), + ListTile( + leading: Icon(Icons.camera_alt), + title: Text('Kamera verwenden'), + onTap: () { + Navigator.pop(context); + _getImage(ImageSource.camera); + }, + ), + ], + ), + ); + }, + child: Text( + 'Bild hinzufügen', + style: TextStyle( + color: Theme.of(context).primaryColor, + fontWeight: FontWeight.bold, + ), + ), + ), + ), + ), + ], + ), + ), + ), + ); + } +} + + /*import 'package:flutter/material.dart'; class AddScreen extends StatelessWidget { diff --git a/pubspec.lock b/pubspec.lock index e9b9197..eb98ba2 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -36,6 +36,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.16.0" + cross_file: + dependency: transitive + description: + name: cross_file + url: "https://pub.dartlang.org" + source: hosted + version: "0.3.3+4" cupertino_icons: dependency: "direct main" description: @@ -62,11 +69,79 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.0.1" + flutter_plugin_android_lifecycle: + dependency: transitive + description: + name: flutter_plugin_android_lifecycle + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.8" flutter_test: dependency: "direct dev" description: flutter source: sdk version: "0.0.0" + flutter_web_plugins: + dependency: transitive + description: flutter + source: sdk + version: "0.0.0" + http: + dependency: transitive + description: + name: http + url: "https://pub.dartlang.org" + source: hosted + version: "0.13.5" + http_parser: + dependency: transitive + description: + name: http_parser + url: "https://pub.dartlang.org" + source: hosted + version: "4.0.2" + image_picker: + dependency: "direct main" + description: + name: image_picker + url: "https://pub.dartlang.org" + source: hosted + version: "0.8.6+3" + image_picker_android: + dependency: transitive + description: + name: image_picker_android + url: "https://pub.dartlang.org" + source: hosted + version: "0.8.5+7" + image_picker_for_web: + dependency: transitive + description: + name: image_picker_for_web + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.11" + image_picker_ios: + dependency: transitive + description: + name: image_picker_ios + url: "https://pub.dartlang.org" + source: hosted + version: "0.8.6+9" + image_picker_platform_interface: + dependency: transitive + description: + name: image_picker_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "2.6.3" + js: + dependency: transitive + description: + name: js + url: "https://pub.dartlang.org" + source: hosted + version: "0.6.4" lints: dependency: transitive description: @@ -102,6 +177,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.8.2" + plugin_platform_interface: + dependency: transitive + description: + name: plugin_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.4" sky_engine: dependency: transitive description: flutter @@ -149,6 +231,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.4.12" + typed_data: + dependency: transitive + description: + name: typed_data + url: "https://pub.dartlang.org" + source: hosted + version: "1.3.1" vector_math: dependency: transitive description: @@ -158,3 +247,4 @@ packages: version: "2.1.2" sdks: dart: ">=2.18.2 <3.0.0" + flutter: ">=3.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index 96b0a42..a833461 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -36,6 +36,7 @@ dependencies: # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^1.0.2 + image_picker: ^0.8.6+3 dev_dependencies: flutter_test: