0

Upon viewing each page in a horizontal PageView.builder I'd like to call a vertical PageView.builder to supply each page with its own vertical list of replies. What's the best way to do this?

  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),
     );
  }
}
Meggy
  • 1,491
  • 3
  • 28
  • 63

1 Answers1

1

The problem is that you are putting a scaffold inside your PageView when it should be otherwise.
See the example below:

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(),
      ],
    );
  }
}
Rod
  • 1,601
  • 12
  • 18
  • I'm using pageview.builder. Should I user pageview instead? How can I access my current itemCount: speakcrafts.length, itemBuilder: (context, int currentIndex) { – Meggy Aug 21 '20 at 20:38
  • The problem is not the pageview.builder, it is returning a scaffold inside it (from the createViewItem function). Your StageBuilder widget should be the body of a scaffold and your createViewItem should return something to be shown inside the body of your scaffold, maybe a Container, a Column or any other common widget. – Rod Aug 21 '20 at 21:15