【问题标题】:Flutter ListView scrollPhysics does not work when it's in SingleChildScrollViewFlutter ListView scrollPhysics 在 SingleChildScrollView 中不起作用
【发布时间】:2021-08-31 21:34:00
【问题描述】:

我需要 SingleChildScrollView 中的 ListView 部分来拥有 PageScrollPhysics,但 ListView 不会相应滚动,除非我也更改 SingleChildScrollView 的 scrollPhysics(在第 35 行和第 36 行之间),这不是我想要的行为,因为我还有其他需要的部分普通滚动物理。

有没有一种方法可以让 ListView 采用 PageScrollPhysics 而其余的小部件以 SingleChildScrollView 应该的方式滚动?

这是我的代码的最小工作示例

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')),)
          ],
        ),
      ),
    );
  }
}

【问题讨论】:

  • 使用 NestedScrollView 代替 SingleChild

标签: flutter


【解决方案1】:
SingleChildScrollView(
  physics: AlwaysScrollableScrollPhysics(),
....
....
  ListView(
    shrinkWrap: true,
    physics: NeverScrollableScrollPhysics(),
    ....
    ....
    ....
  ),
),

【讨论】:

  • 但我的 ListView 中仍然需要 PageScrollPhysics 效果
  • 尝试在Scaffold中使用PageScrollPhysics
  • 为什么要把ListView放到SingleChildScrollView? ListView 已经可滚动。你不需要这个。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-15
  • 2020-09-04
  • 2022-08-05
  • 2021-11-08
  • 2023-01-25
  • 1970-01-01
  • 2020-09-20
相关资源
最近更新 更多