flutter image asset

151

// pubspec.yaml
flutter:
  assets:
    - graphics/

// Inside your widget
Image(image: AssetImage('graphics/background.png'))
Widget build(BuildContext context) {
  return Image(image: AssetImage('graphics/background.png'));
}
import 'dart:async';
import 'dart:io';

import 'package:flutter/services.dart' show rootBundle;
import 'package:path_provider/path_provider.dart';

Future<File> getImageFileFromAssets(String path) async {
  final byteData = await rootBundle.load('assets/$path');

  final file = File('${(await getTemporaryDirectory()).path}/$path');
  await file.writeAsBytes(byteData.buffer.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes));

  return file;
}

File f = await getImageFileFromAssets('images/myImage.jpg');

Comments

Submit
0 Comments