如何在 flutter 中适应 column/row table 图片内容
How to fit column/row table image content in flutter
我有这个小项目可以将 9 张图像放入 column/row table 中,问题是图像显示在屏幕外
我希望它是这样的:
这是代码
return MaterialApp(
home: SafeArea(
child: Scaffold(
backgroundColor: Colors.tealAccent,
body: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.asset('images/1.png', fit: BoxFit.fill),
Image.asset('images/2.png', fit: BoxFit.fill),
Image.asset('images/3.png', fit: BoxFit.fill),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.asset('images/4.png', fit: BoxFit.fill),
Image.asset('images/5.png', fit: BoxFit.fill),
Image.asset('images/6.png', fit: BoxFit.fill),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.asset('images/7.png', fit: BoxFit.fill),
Image.asset('images/8.png', fit: BoxFit.fill),
Image.asset('images/9.png', fit: BoxFit.fill),
],
),
],
)),
),
);
这是结果:
尝试使用扩展小部件-
return MaterialApp(
home: SafeArea(
child: Scaffold(
backgroundColor: Colors.tealAccent,
body: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(child:Image.asset('images/1.png',fit: BoxFit.fill), flex:1),
Expanded(child:Image.asset('images/2.png',fit: BoxFit.fill), flex:1),
Expanded(child:Image.asset('images/3.png',fit: BoxFit.fill), flex:1),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(child:Image.asset('images/4.png',fit: BoxFit.fill), flex:1),
Expanded(child:Image.asset('images/5.png',fit: BoxFit.fill), flex:1),
Expanded(child:Image.asset('images/6.png',fit: BoxFit.fill), flex:1),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(child:Image.asset('images/7.png',fit: BoxFit.fill), flex:1),
Expanded(child:Image.asset('images/8.png',fit: BoxFit.fill), flex:1),
Expanded(child:Image.asset('images/9.png',fit: BoxFit.fill), flex:1),
],
),
],
)),
),
);
让我知道它是否有效...
您可以使用网格视图代替 column/row
return MaterialApp(
home: SafeArea(
child: Scaffold(
body: GridView.count(
crossAxisSpacing: 5,
mainAxisSpacing: 10,
crossAxisCount: 3,
children: <Widget>[
Image.asset('lib/food.jpg'),
Image.asset('lib/food.jpg'),
Image.asset('lib/food.jpg'),
Image.asset('lib/food.jpg'),
Image.asset('lib/food.jpg'),
Image.asset('lib/food.jpg'),
Image.asset('lib/food.jpg'),
Image.asset('lib/food.jpg'),
Image.asset('lib/food.jpg'),
],
),
),
),
);
我有这个小项目可以将 9 张图像放入 column/row table 中,问题是图像显示在屏幕外
我希望它是这样的:
这是代码
return MaterialApp(
home: SafeArea(
child: Scaffold(
backgroundColor: Colors.tealAccent,
body: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.asset('images/1.png', fit: BoxFit.fill),
Image.asset('images/2.png', fit: BoxFit.fill),
Image.asset('images/3.png', fit: BoxFit.fill),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.asset('images/4.png', fit: BoxFit.fill),
Image.asset('images/5.png', fit: BoxFit.fill),
Image.asset('images/6.png', fit: BoxFit.fill),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.asset('images/7.png', fit: BoxFit.fill),
Image.asset('images/8.png', fit: BoxFit.fill),
Image.asset('images/9.png', fit: BoxFit.fill),
],
),
],
)),
),
);
这是结果:
尝试使用扩展小部件-
return MaterialApp(
home: SafeArea(
child: Scaffold(
backgroundColor: Colors.tealAccent,
body: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(child:Image.asset('images/1.png',fit: BoxFit.fill), flex:1),
Expanded(child:Image.asset('images/2.png',fit: BoxFit.fill), flex:1),
Expanded(child:Image.asset('images/3.png',fit: BoxFit.fill), flex:1),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(child:Image.asset('images/4.png',fit: BoxFit.fill), flex:1),
Expanded(child:Image.asset('images/5.png',fit: BoxFit.fill), flex:1),
Expanded(child:Image.asset('images/6.png',fit: BoxFit.fill), flex:1),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(child:Image.asset('images/7.png',fit: BoxFit.fill), flex:1),
Expanded(child:Image.asset('images/8.png',fit: BoxFit.fill), flex:1),
Expanded(child:Image.asset('images/9.png',fit: BoxFit.fill), flex:1),
],
),
],
)),
),
);
让我知道它是否有效...
您可以使用网格视图代替 column/row
return MaterialApp(
home: SafeArea(
child: Scaffold(
body: GridView.count(
crossAxisSpacing: 5,
mainAxisSpacing: 10,
crossAxisCount: 3,
children: <Widget>[
Image.asset('lib/food.jpg'),
Image.asset('lib/food.jpg'),
Image.asset('lib/food.jpg'),
Image.asset('lib/food.jpg'),
Image.asset('lib/food.jpg'),
Image.asset('lib/food.jpg'),
Image.asset('lib/food.jpg'),
Image.asset('lib/food.jpg'),
Image.asset('lib/food.jpg'),
],
),
),
),
);