要将PageView居中,可以在父容器中使用Align组件。以下是一个示例代码:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('PageView'),
),
body: Center(
child: Container(
width: MediaQuery.of(context).size.width,
height: 200,
child: Align(
alignment: Alignment.center,
child: PageView(
children: [
Container(
color: Colors.red,
child: Center(
child: Text('Page 1'),
),
),
Container(
color: Colors.blue,
child: Center(
child: Text('Page 2'),
),
),
Container(
color: Colors.green,
child: Center(
child: Text('Page 3'),
),
),
],
),
),
),
),
),
);
}
}
在这个示例中,我们使用一个Container来限制PageView的宽度和高度。然后,我们使用Align组件将PageView居中。通过设置alignment属性为Alignment.center,我们可以将PageView在父容器中居中显示。