"位置"小部件未显示图像



当我用定位的Widget包装容器并在移除时给top任何值时,图像不会显示它显示的值。为什么。

========================================================================================================================================================================================================================================================================================================================================================================================================这是代码

import 'package:flutter/material.dart';
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
child: Column(
children: [
SizedBox(
height: MediaQuery.of(context).size.height * 2,
child: Stack(
children: [
Positioned(
top: 1,
child: Container(
// height: 30,
// color: Colors.red,
height: MediaQuery.of(context).size.height * 1 - 17,
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage('home/h_intro-s1-l1.jpg'),
fit: BoxFit.fill,
),
),
),
),
],
),
),
],
),
),
);
}
}
// here you only give vertical dimensi
// but you didnt give any horizontal dimension. that caused
// the width of container will force into Positioned width, which is 0.
height: MediaQuery.of(context).size.height * 1 - 17,
width: any value.
decoration: const BoxDecoration(
image: DecorationImage(
image:AssetImage('home/h_intro-s1-l1.jpg')this,
fit: BoxFit.fill,
)
),

文档定位:…

或者,宽度和高度属性可以用于维度,具有一个相应的位置属性

您也需要设置leftrightboth锚点才能使用Positioned,请尝试以下操作:

Positioned(
top: 1,
left:0,
right:0,
child: Container(
// height: 30,
// color: Colors.red,
height: MediaQuery.of(context).size.height * 1 - 17,
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage('home/h_intro-s1-l1.jpg'),
fit: BoxFit.fill,
),
),
),
),

最新更新