一尘不染

如何使用image.file加载图像

flutter

我似乎无法简单地将图像从硬盘驱动器加载到屏幕上。Image.network似乎很简单。但是我不知道如何使用Image或Image.file。图像似乎需要流,所以我认为这不是我想要的。

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

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
    File file = new File("Someimage.jpeg");
    @override
    Widget build(BuildContext context) {
        return new MaterialApp(
            home: new Image.file(file),  //doesn't work, but no errors
        );
    }
}

我将Someimage添加到pubspec.yaml文件中,但这也不起作用:

assets:
    - Someimage.jpeg

有人可以给我举个例子吗?谢谢。


阅读 1160

收藏
2020-08-13

共1个答案

一尘不染

这是另一个使用jpg作为背景图像的示例。它还将不透明度应用于图像。

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new Scaffold(
        resizeToAvoidBottomPadding: false,
        appBar: new AppBar(
          title: new Text("test"),
        ),
        body: new Container(
          decoration: new BoxDecoration(
            image: new DecorationImage(
              colorFilter: new ColorFilter.mode(Colors.black.withOpacity(0.6), BlendMode.dstATop),
              image: new AssetImage("assets/images/keyboard.jpg"),
              fit: BoxFit.cover,
            ),
          ),
        ),
      ),
    );
  }
}
2020-08-13