Flutter:可以为参数类型“图像功能(动态)”分配哪种参数类型?

时间:2020-07-03 05:33:32

标签: image flutter dart

我只是想加载图像。我正在通过网络支持使用Beta频道。 Flutter 1.19.0-4.3.pre。 Dart 2.9.0

这是我的发布规范:

flutter:
  uses-material-design: true
  assets:
    - assets/

这是我的main.dart;

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Image(image: AssetImage('assets/logo.png')),
          ] < Widget > [
            Text('Find happy, temporary, homes for your pets'),
          ],
        ),
      ),
    );
  }
}
      

我只是从Flutter网站上的文档复制/粘贴了。我要去哪里错了?

2 个答案:

答案 0 :(得分:1)

请确保您的pubspec.yaml文件的缩进正确:

flutter:
  assets:
    - assets/

然后,对于您的main.dart,我认为您不需要窗口小部件生成器。因此,您的main.dart应该如下所示:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Image(image: AssetImage('assets/logo.png'),
            ],
          ),
        ),
      ),
    );
  }
}

答案 1 :(得分:1)

您不必具有two arrays in a single <Widget>[]列类。我可以看到您已经这样做:

<Widget>[
    Image(image: AssetImage('assets/logo.png')),
] < Widget > [
    Text('Find happy, temporary, homes for your pets'),
],

这确实是错误的。普通Column()接受的是这样的。 [要了解有关列类的更多信息,请单击here]

Column(
  children: <Widget>[
    // You multiple widget goes here not in multiple arrays
    Widget1
    Widget2
    ...
  ]
)

因此,要更正您的代码,您应该执行类似的操作,否则,您已经对assets做过正确的操作

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          // One single array keeps your multiple widgets
          children: <Widget>[
            Image(image: AssetImage('assets/logo.png')),
            Text('Find happy, temporary, homes for your pets'),
          ]
        )
      )
    );
  }
}

如果适合您,请告诉我。学习愉快:)