在以水平PageView.builder查看每个页面后,我想调用一个垂直的PageView.builder来为每个页面提供其自己的垂直回复列表。最好的方法是什么?

  class StageBuilder extends StatelessWidget {
  final List<SpeakContent> speakcrafts;
  StageBuilder(this.speakcrafts);

  final PageController controller = PageController(initialPage: 0);

  @override
  Widget build(context) {
    return PageView.builder(
      controller: controller,
      itemCount: speakcrafts.length,
      itemBuilder: (context, int currentIndex) {
        return createViewItem(speakcrafts[currentIndex], context, currentIndex);
      },
    );
  }

  Widget createViewItem(SpeakContent speakcraft, BuildContext context, int currentIndex) {

    return Scaffold(
        body: (some stuff),
     );
  }
}

最佳答案

问题在于您应该将支架放置在PageView中,否则应该放置。请参阅以下示例:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Padding(
          padding: const EdgeInsets.symmetric(vertical: 50, horizontal: 20),
          child: PageViewDemo(),
        ),
      ),
    );
  }
}

class PageViewDemo extends StatefulWidget {
  @override
  _PageViewDemoState createState() => _PageViewDemoState();
}

class _PageViewDemoState extends State<PageViewDemo> {

  PageController _controller = PageController(
    initialPage: 0,
  );

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return PageView(
      controller: _controller,
      children: [
        MyPage1Widget(),
        MyPage2Widget(),
        MyPage3Widget(),
      ],
    );
  }
}

关于flutter - Flutter/Dart-嵌套的水平和垂直PageView构建器?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/63529404/

10-13 01:15