Home > Net >  How do I put a flutter PageView inside a Listview?
How do I put a flutter PageView inside a Listview?

Time:10-27

My goal is to have a Pageview horizontal scroll only for some widgets in my Listview. My expectation is, I can just put Pageview inside a Listview, but I got some kind of error unbound height. Which led me to this question, it solves the problem by containing Pageview inside a Container or Sizedbox. The solution did work for Column but doesn't work for Listview. Still got the same error unbound height.

Here is the code that I write


class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: ListView(
          padding: const EdgeInsets.all(24),
          children: [
            SomeWidget(),

            // Dropdown status
            SizedBox(height: 16),
            SomeWidget(),

            // The Pageview
            SizedBox(height: 16),
            Expanded(
              child: PageView(
                children: <Widget>[
                  SizedBox(height: 100, child: Container(color: Colors.red)),
                  Container(color: Colors.blue),
                  Container(color: Colors.yellow),
                  Container(color: Colors.amber),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

CodePudding user response:

PageView Requires size Expanded while list view must know the size of its children you can use this library https://pub.dev/packages/expandable_page_view which is know the size of screen then set it to the PageView

  • Related