Untitled

mail@pastecode.io avatar
unknown
dart
2 months ago
1.9 kB
0
Indexable
Never
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Изображение из галереи',
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  File? image;
  TextEditingController nameController = TextEditingController();
  TextEditingController surnameController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Изображение из галереи'),
      ),
      body: Center(
        child: Column(
          children: [
            image != null
                ? Image.file(image!)
                : Text('Изображение не выбрано'),
            TextField(
              controller: nameController,
              decoration: InputDecoration(
                labelText: 'Имя',
              ),
            ),
            TextField(
              controller: surnameController,
              decoration: InputDecoration(
                labelText: 'Фамилия',
              ),
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () async {
          final pickedFile = await ImagePicker().pickImage(source: ImageSource.gallery);
          if (pickedFile != null) {
            image = File(pickedFile.path);
            setState(() {});
          }
        },
        child: Icon(Icons.photo),
      ),
    );
  }
}
Leave a Comment