尺寸框和Image.asset出现错误,你能帮我吗?

huangapple go评论70阅读模式
英文:

Error with Sizebox and Image.asset, can you help me?

问题

我在Flutter中遇到问题...我不明白为什么元素被红色下划线标记...你能帮助我吗?

import 'package:flutter/material.dart';

class LandingScreen extends StatelessWidget {
  const LandingScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    return Scaffold(
      body: SafeArea(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: const [
            SizedBox(height: 50),
            Text(
              'Welcome to app',
              style: TextStyle(
                fontSize: 33,
                fontWeight: FontWeight.w600,
              ),
            ),
            SizedBox(height: size.height / 9),
            Image.asset(
              'assets/bg.png',
              height: 340,
              width: 340,
            ),
          ],
        ),
      ),
    );
  }
}

这里是代码截图,其中代码部分被红色下划线标记:

尺寸框和Image.asset出现错误,你能帮我吗?

英文:

I have a problem with Flutter.... I don't understand why the elements are underlined in red... Can you help me?

import 'package:flutter/material.dart';

class LandingScreen extends StatelessWidget {
  const LandingScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    return Scaffold(
      body: SafeArea(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: const [
            SizedBox(height: 50),
            Text(
              'Welcome to app',
              style: TextStyle(
                fontSize: 33,
                fontWeight: FontWeight.w600,
              ),
            ),
            SizedBox(height: size.height / 9),
            Image.asset(
              'assets/bg.png',
              height: 340,
              width: 340,
            ),
          ],
        ),
      ),
    );
  }
}

here is the photo with the code underlined in red

尺寸框和Image.asset出现错误,你能帮我吗?

答案1

得分: 0

使用这个方法,

Image(
  image: const AssetImage("assets/bg.png"),
  height: 340,
  width: 340,
),

完整代码:

import 'package:flutter/material.dart';

class LandingScreen extends StatelessWidget {
  const LandingScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    return Scaffold(
      body: SafeArea(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: const [
            SizedBox(height: 50),
            Text(
              'Welcome to app',
              style: TextStyle(
                fontSize: 33,
                fontWeight: FontWeight.w600,
              ),
            ),
            SizedBox(height: size.height / 9),
            Image(
              image: const AssetImage("assets/bg.png"),
              height: 340,
              width: 340,
            ),
          ],
        ),
      ),
    );
  }
}
英文:

use this method,

          Image(
                image: const AssetImage("assets/bg.png"),
                height: 340,
                width: 340,
                  ),

Full code:

import 'package:flutter/material.dart';

class LandingScreen extends StatelessWidget {
  const LandingScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    return Scaffold(
      body: SafeArea(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: const [
            SizedBox(height: 50),
            Text(
              'Welcome to app',
              style: TextStyle(
                fontSize: 33,
                fontWeight: FontWeight.w600,
              ),
            ),
            SizedBox(height: size.height / 9),
            Image(
                image: const AssetImage("assets/bg.png"),
                height: 340,
                width: 340,
             ),
          ],
        ),
      ),
    );
  }
}

答案2

得分: 0

问题出在你在 children: const [上使用了const

但是你在运行时读取了size.height,因此它不能是const

你可以这样做:

class LandingScreen extends StatelessWidget {
  const LandingScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    return Scaffold(
      body: SafeArea(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            const SizedBox(height: 50),
            const Text(
              'Welcome to app',
              style: TextStyle(
                fontSize: 33,
                fontWeight: FontWeight.w600,
              ),
            ),
            SizedBox(height: size.height / 9),
            Image.asset(
              'assets/bg.png',
              height: 340,
              width: 340,
            ),
          ],
        ),
      ),
    );
  }
}

你可以查看Dart中的"const"和"final"关键字?

英文:

The issue is coming because you are using const on children: const [.

But you are reading size.height on runtime. Therefore it can't be const.

You can do

class LandingScreen extends StatelessWidget {
  const LandingScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    return Scaffold(
      body: SafeArea(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            const SizedBox(height: 50),
            const Text(
              'Welcome to app',
              style: TextStyle(
                fontSize: 33,
                fontWeight: FontWeight.w600,
              ),
            ),
            SizedBox(height: size.height / 9),
            Image.asset(
              'assets/bg.png',
              height: 340,
              width: 340,
            ),
          ],
        ),
      ),
    );
  }
}

You can check "const" and "final" keywords in Dart?

huangapple
  • 本文由 发表于 2023年1月9日 02:15:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75050264.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定