1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

FlutterのUserAccountsDrawerHeaderのcurrentAccountPictureの画像をギャラリーから選択する

Last updated at Posted at 2021-07-13

環境

flutter2.2.3

手順

pubspec.yamlに下記を追加します。
image_picker: ^0.8.1+3

実装ファイルで下記をインポートします。
import 'dart:io';
import 'package:image_picker/image_picker.dart';

実装dartファイルのState内で下記を実装します。
  File? _image;
  final picker = ImagePicker();

  Future getImageFromGallery() async {
    final pickedFile = await picker.getImage(source: ImageSource.gallery);

    setState(() {
      _image = File(pickedFile!.path);
    });
  }


実装dartファイルのUserAccountsDrawerHeader内で下記プロパティを実装します。
currentAccountPicture: GestureDetector(
 onTap: () => getImageFromGallery(),
 child: CircleAvatar(
  backgroundColor: Colors.black54,
  child: _image == null
  ? Icon(Icons.photo_camera, color: Colors.white)
  : Image.file(_image!),
 ),
)

エミュレータでギャラリーに画像を準備する手順
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/630191/4ad0dae3-0243-5bad-fd88-82f3b66e3129.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/630191/92c39c90-e988-a583-a187-b3da0ad0acc5.png)
ここでpngファイルなどをドラッグ&ドロップ ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/630191/9ebcd4f8-ce53-03bf-5bdc-964dff1a7b44.png)

デモ

![0713.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/630191/64b4f57c-2a43-34ec-1693-33e919010f2e.gif)
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?