ecp_file_view 3.0.2 copy "ecp_file_view: ^3.0.2" to clipboard
ecp_file_view: ^3.0.2 copied to clipboard

A file viewer plugin for Flutter, support local file and network link of Android, iOS.

example/lib/main.dart

import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:ecp_file_view/ecp_file_view.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:path_provider/path_provider.dart';

import 'page_local_file_viewer.dart';

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

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      localizationsDelegates: <LocalizationsDelegate<dynamic>>[
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
        ViewerLocalizationsDelegate.delegate,
      ],
      supportedLocales: [Locale('en', 'US'), Locale('zh', 'CN')],
      debugShowCheckedModeBanner: false,
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  List<String> files = [
    'docx.docx',
    'doc.doc',
    'xlsx.xlsx',
    'xls.xls',
    'pptx.pptx',
    'ppt.ppt',
    'pdf.pdf',
    'txt.txt',
    "http://www.xqb5.cc/api/txt_down.php?articleid=75255&articlename=%E4%BF%AE%E4%BB%99%EF%BC%8C%E4%BB%8E%E5%87%A1%E4%BA%BA%E4%BF%AE%E4%BB%99%E5%BC%80%E5%A7%8B"
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('File View')),
      body: _buildBodyWidget(),
    );
  }

  Widget _buildBodyWidget() {
    return ListView.builder(
      itemCount: files.length,
      itemBuilder: (context, index) {
        String filePath = files[index];
        String fileShowText = '';

        int i = filePath.lastIndexOf('/');
        if (i <= -1) {
          fileShowText = filePath;
        } else {
          fileShowText = filePath.substring(i + 1);
        }

        int j = fileShowText.lastIndexOf('.');

        String title = '';
        String type = '';

        if (j > -1) {
          title = fileShowText.substring(0, j);
          type = fileShowText.substring(j + 1).toLowerCase();
        }

        final child = ElevatedButton(
          onPressed: () {
            if (filePath.contains('http://') || filePath.contains('https://')) {
              onNetworkTap(title, type, filePath);
            } else {
              onLocalTap(type, 'assets/files/$filePath');
            }
          },
          child: Text(fileShowText),
        );

        return Container(
          margin: const EdgeInsets.only(top: 10.0),
          padding: const EdgeInsets.symmetric(horizontal: 15.0),
          child: child,
        );
      },
    );
  }

  Future onLocalTap(String type, String assetPath) async {
    String filePath = await setFilePath(type, assetPath);
    if (!await asset2Local(type, assetPath)) {
      return;
    }

    Navigator.of(context).push(MaterialPageRoute(builder: (ctx) {
      return LocalFileViewerPage(filePath: filePath);
    }));
  }

  Future onNetworkTap(String title, String type, String downloadUrl) async {
    var name = "test.txt";
    Navigator.of(context).push(MaterialPageRoute(builder: (ctx) {
      return NetworkFileViewer(url: downloadUrl, name: name);
    }));
  }

  Future asset2Local(String type, String assetPath) async {
    String filePath = await setFilePath(type, assetPath);

    File file = File(filePath);
    if (fileExists(filePath)) {
      await file.delete();
    }

    await file.create(recursive: true);
    debugPrint("文件路径 -> ${file.path}");
    ByteData bd = await rootBundle.load(assetPath);
    await file.writeAsBytes(bd.buffer.asUint8List(), flush: true);
    return true;
  }

  Future setFilePath(String type, String assetPath) async {
    final directory = await getTemporaryDirectory();
    return "${directory.path}/fileview/${base64.encode(utf8.encode(assetPath))}.$type";
  }

  bool fileExists(String filePath) => File(filePath).existsSync();
}
0
likes
130
points
141
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

A file viewer plugin for Flutter, support local file and network link of Android, iOS.

Homepage

License

Apache-2.0 (license)

Dependencies

better_open_file, dict_flutter_extension, dio, ecp_net, ecp_theme, flutter, flutter_smart_dialog, path_provider, permission_handler

More

Packages that depend on ecp_file_view

Packages that implement ecp_file_view