将图像插入容器Flutter应用中

时间:2018-09-06 19:53:10

标签: flutter

我正在看我在startflutter.com上找到的模板,完整代码可以在下面看到

我尝试将自己的图片插入圆圈,似乎没有办法使图片完全插入框中(总是被裁剪)

@override
      Widget build(BuildContext context) {
        final alucard = Hero(
          tag: 'hero',
          child: Padding(
            padding: EdgeInsets.all(16.0),
            child: CircleAvatar(
              radius: 72.0,
              backgroundColor: Colors.transparent,
              backgroundImage: AssetImage('assets/alucard.jpg'),
            ),
          ),
        );

我想像这样在容器中插入图片

     @override
  Widget build(BuildContext context) {
    final alucard = Container(
        decoration: new BoxDecoration(
          image: new DecorationImage(
              image: new AssetImage("images/logo.png"),
              fit: BoxFit.fill,
          )
        )
    );

但是这不起作用并且不会显示出来,这怎么了?

这是整个代码页面...

import 'package:flutter/material.dart';

class HomePage extends StatelessWidget {
  static String tag = 'home-page';

  @override
  Widget build(BuildContext context) {
    final alucard = Hero(
      tag: 'hero',
      child: Padding(
        padding: EdgeInsets.all(16.0),
        child: CircleAvatar(
          radius: 72.0,
          backgroundColor: Colors.transparent,
          backgroundImage: AssetImage('assets/alucard.jpg'),
        ),
      ),
    );

    final welcome = Padding(
      padding: EdgeInsets.all(8.0),
      child: Text(
        'Welcome Alucard',
        style: TextStyle(fontSize: 28.0, color: Colors.white),
      ),
    );

    final lorem = Padding(
      padding: EdgeInsets.all(8.0),
      child: Text(
        'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec hendrerit condimentum mauris id ',
        style: TextStyle(fontSize: 16.0, color: Colors.white),
      ),
    );

    final body = Container(
      width: MediaQuery.of(context).size.width,
      padding: EdgeInsets.all(28.0),
      decoration: BoxDecoration(
        gradient: LinearGradient(colors: [
          Colors.blue,
          Colors.lightBlueAccent,
        ]),
      ),
      child: Column(
        children: <Widget>[alucard, welcome, lorem],
      ),
    );

    return Scaffold(
      body: body,
    );
  }
}

5 个答案:

答案 0 :(得分:2)

以此更改您的容器会很好

new Container(
      height: 120.0,
      width: 120.0,
      decoration: new BoxDecoration(
        image: DecorationImage(
          image: new AssetImage(
              'assets/assets/alucard.jpg'),
          fit: BoxFit.fill,
        ),
        shape: BoxShape.circle,
      ),
    )

答案 1 :(得分:1)

尝试一下

new Container(
  width: 100.00,
  height: 100.00,
  decoration: new BoxDecoration(
  image: new DecorationImage(
      image: ExactAssetImage('assets/example.png'),
      fit: BoxFit.fitHeight,
      ),
  ));

通过编辑pubspec.yaml文件https://docs.flutter.io/flutter/painting/ExactAssetImage-class.html,确保您告诉flutter资产文件夹在哪里

答案 2 :(得分:0)

BoxFit.fill传递到您的Image.asset即可。

尝试一下;

import 'package:flutter/material.dart';

class HomePage extends StatelessWidget {
  static String tag = 'home-page';

  @override
  Widget build(BuildContext context) {
    final alucard = Hero(
      tag: 'hero',
      child: Padding(
        padding: EdgeInsets.all(16.0),
        child: new Container(
          height: 80.0,
          width: 80.0,
          decoration: new BoxDecoration(
            image: DecorationImage(
              image: new AssetImage(
                  'assets/alucard.jpg'),
              fit: BoxFit.fill,
            ),
            borderRadius:
            BorderRadius.circular(80.0),
          ),
        ),
      ),
    );

    final welcome = Padding(
      padding: EdgeInsets.all(8.0),
      child: Text(
        'Welcome Alucard',
        style: TextStyle(fontSize: 28.0, color: Colors.white),
      ),
    );

    final lorem = Padding(
      padding: EdgeInsets.all(8.0),
      child: Text(
        'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec hendrerit condimentum mauris id ',
        style: TextStyle(fontSize: 16.0, color: Colors.white),
      ),
    );

    final body = Container(
      width: MediaQuery.of(context).size.width,
      padding: EdgeInsets.all(28.0),
      decoration: BoxDecoration(
        gradient: LinearGradient(colors: [
          Colors.blue,
          Colors.lightBlueAccent,
        ]),
      ),
      child: Column(
        children: <Widget>[alucard, welcome, lorem],
      ),
    );

    return Scaffold(
      body: body,
    );
  }
}

答案 3 :(得分:0)

可以按以下方式将图像添加到容器中: [your_image_folder_path / image_name]

Container(
    child:  Image(image: AssetImage("images/logo.png")))

答案 4 :(得分:-1)

请尝试以下操作:

Container(
    height: _height*0.35,
    width: _width,
    decoration: BoxDecoration(
        image: DecorationImage(
            image: ExactAssetImage('assets/splash_img.jpeg'),
            fit: BoxFit.fitHeight,
        ),
    ),
),