如何在 PageView 中的页面之间留出一些空间(边距/填充)?

Ama*_*ria 2 dart flutter

我正在使用PageView.builder创建页面。

PageView.builder(
          itemCount: _pagesList.length,
          itemBuilder: (BuildContext context, int index) {
            return Container( 
                    color: _pagesList[index],
                           );
                          }
                         )
Run Code Online (Sandbox Code Playgroud)

我目前拥有的:

我想要的是:

即我想Padding在页面之间提供一些(当它们被滚动时)

原因:我将在这些页面中显示图像,并且由于图像将覆盖每个页面的整个宽度,因此滚动页面时看起来不太好,因为它们被编织在一起,如下所示:

滚动页面时图像之间没有空格

我该如何解决这个问题?

小智 9

这个答案来自 Amon Kataria Github提出的问题

final pageController = PageController(viewportFraction: 1.1);

PageView.builder(
  controller: pageController,
  itemCount: _pagesList.length,
  itemBuilder: (BuildContext context, int index) {
    return FractionallySizedBox(
      widthFactor: 1 / pageController.viewportFraction,
      child: Container(
       color: _pagesList[index],
      ),
    );
  },
);
Run Code Online (Sandbox Code Playgroud)

谢谢@mono0926


小智 8

PageController imagesController =
        PageController(initialPage: 0, viewportFraction: 1.1);

PageView(
    itemBuilder: (BuildContext context, int index) {
     return Padding(
      padding: EdgeInsets.only(left: 10, right: 10),
        child: Container( 
            color: _pagesList[index],
        ),
     );
  }
),
Run Code Online (Sandbox Code Playgroud)

  • 请格式化您提供的代码以便于更好地阅读。另外,以一些介绍性词语的形式进行一些解释会很好。 (3认同)

Wil*_*ndi 0

最大努力:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: Scaffold(
        body: MyPageView()
      )
    );
  }
}


class MyPageView extends StatefulWidget {
  MyPageView({Key key}) : super(key: key);

  _MyPageViewState createState() => _MyPageViewState();
}

class _MyPageViewState extends State<MyPageView> {
  @override
  Widget build(BuildContext context) {
    return PageView(
     children: <Widget>[
    Container(
      color: Colors.black,
      child: Card(
        color: Colors.red,
        )
      ),
    Container(
      color: Colors.black,
      child: Card(
        color: Colors.blue,
      ),
    ),
    Container(
      color: Colors.black,
      child: Card(
        color: Colors.green,
      ),
    ),

  ],
  );
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 不过实际上并没有多大区别:( (2认同)