英文:
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,
),
],
),
),
);
}
}
这里是代码截图,其中代码部分被红色下划线标记:
英文:
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
答案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?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论