emily
emily

Reputation: 382

Flutter ListView scrollPhysics does not work when it's in SingleChildScrollView

I need a ListView section in SingleChildScrollView to have PageScrollPhysics, but the ListView does not scroll accordingly unless I change the SingleChildScrollView's scrollPhysics (between line 35 and 36) too, which is not my desired behaviour as I still have other sections which need normal scroll physics.

Is there a way that I could make the ListView adopt PageScrollPhysics while the remaining widgets scrolls the way a SingleChildScrollView should?

Here's a minimal working example of my code

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [
            Text('TITLE'),
            ListView(
              shrinkWrap: true,
              physics: PageScrollPhysics(),
              scrollDirection: Axis.vertical,
              children: <Widget>[
                Container(
                  height: MediaQuery.of(context).size.height/2.5,
                  color: Colors.amber[800],
                  child: const Center(child: Text('Entry B')),
                ),
                Container(
                  height: MediaQuery.of(context).size.height/2.5,
                  color: Colors.amber[700],
                  child: const Center(child: Text('Entry C')),
                ),
                Container(
                  height: MediaQuery.of(context).size.height/2.5,
                  color: Colors.amber[600],
                  child: const Center(child: Text('Entry D')),
                ),
                Container(
                  height: MediaQuery.of(context).size.height/2.5,
                  color: Colors.amber[500],
                  child: const Center(child: Text('Entry E')),
                ),
                Container(
                  height: MediaQuery.of(context).size.height/2.5,
                  color: Colors.amber[400],
                  child: const Center(child: Text('Entry F')),
                ),
                Container(
                  height: MediaQuery.of(context).size.height/2.5,
                  color: Colors.amber[300],
                  child: const Center(child: Text('Entry G')),
                ),
              ],
            ),
            Container( height: MediaQuery.of(context).size.height/2.5,
                  color: Colors.red[300],
                  child: const Center(child: Text('OTHER CONTENTS')),),
             Container( height: MediaQuery.of(context).size.height/2.5,
                  color: Colors.teal[300],
                  child: const Center(child: Text('ANOTHER CONTENT')),)
          ],
        ),
      ),
    );
  }
}

Upvotes: 1

Views: 14113

Answers (1)

Ujjwal Raijada
Ujjwal Raijada

Reputation: 975

SingleChildScrollView(
  physics: AlwaysScrollableScrollPhysics(),
....
....
  ListView(
    shrinkWrap: true,
    physics: NeverScrollableScrollPhysics(),
    ....
    ....
    ....
  ),
),

Upvotes: 3

Related Questions