对于 FittedBox 小部件, fit: BoxFit.fill属性不适用于SOMETHING。BoxFit.fill 应该将子小部件拉伸到父小部件的完整宽度和高度。这就是它在文档中所说的
通过扭曲源的纵横比来填充目标框。
最有趣的是,在没有Null-safety的 Dart 版本中,完全相同的代码可以正常工作。
但这不会发生。为简单起见,我将图像替换为填充容器。下面是完整的代码和截图。
主要.dart
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Name App',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
title: const Text('Name Page'),
),
body: const SafeArea(
child: MyHomePage(),
),
),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
return Align(
alignment: Alignment.center,
child: Container(
width: 200.0,
height: 200.0,
color: Colors.amber,
alignment: Alignment.center,
child: FittedBox(
fit: BoxFit.fill,
/*child: Image.asset(
//width = 240, height = 400
'assets/images/a.jpg',
),*/
child: Container(
width: 240.0,
height: 400.0,
color: Colors.green,
),
),
),
);
}
}

容器中的属性
alignment: Alignment.center显然是多余的,因为 您已经将其居中,并且看起来与以下内容冲突FittedBox: