flutter 尺寸框和图像错误,资产,你能帮助我吗?

2eafrhcq  于 2023-01-09  发布在  Flutter
关注(0)|答案(2)|浏览(139)

我对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,
            ),
          ],
        ),
      ),
    );
  }
}

这是代码下有红色下划线的照片

pdtvr36n

pdtvr36n1#

使用这种方法,

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,
             ),
          ],
        ),
      ),
    );
  }
}
rkue9o1l

rkue9o1l2#

出现此问题是因为您在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,
            ),
          ],
        ),
      ),
    );
  }
}

您可以选中"const" and "final" keywords in Dart?

相关问题