Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get or pick local GIF file from device

I have done lots of R&D about the pick-only GIF file and all give results only display or showing GIF files but I want to only pick GIF files from a local device like images or videos.

like image 516
Alpit Panchal Avatar asked Oct 27 '25 14:10

Alpit Panchal


1 Answers

you can use file_picker package and set the allowedExtensions to gif.

allowedExtensions: ['gif']

here is an example:

import 'dart:io';

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

void main() {
  runApp(MaterialApp(
    home: MyApp(),
  ));
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  var _selectedFile;
  String customAppLogoName;
  _openFileManager() async {
    FilePickerResult result = await FilePicker.platform.pickFiles(
      type: FileType.custom,
      allowedExtensions: ['gif'],
    );
    if (result != null) {
      PlatformFile selectedFile = result.files.first;
      setState(() {
        _selectedFile = File(selectedFile.path);
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        title: Text("File Picker"),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          _openFileManager();
        },
        child: Icon(Icons.image),
      ),
      body: Center(
        child: Container(
          child: _selectedFile == null
              ? Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Icon(
                      Icons.broken_image,
                      size: 100,
                    ),
                    Text('no image'),
                  ],
                )
              : Image(
                  image: FileImage(_selectedFile),
                ),
        ),
      ),
    );
  }
}

result:

enter image description here

like image 113
Nazarudin Avatar answered Oct 30 '25 04:10

Nazarudin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!