【问题标题】:How to prevent widget from beeing scrolled?如何防止小部件被滚动?
【发布时间】:2020-10-22 17:01:39
【问题描述】:

如何防止第一个小部件(ListView)被滚动?

这个想法是滚动SomeList,但最顶部的ListView 小部件应该保持不可滚动。

body: ListView(
        children: <Widget>[
          ListView(
            shrinkWrap: true,
            children: <Widget>[
              ListTile( // how to prevet this widget from beeing scrolled?
                title: Container(
                  height: 30,
                  child: Row(...),
                ),
              ),
            ],
          ),
          SomeList(), // builds ListView.separated( ...
        ],
      ),

更新:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(title: 'Flutter Demo', home: MyListView());
  }
}

class MyListView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('AppBar'),
      ),
      body: ListView(
        children: <Widget>[
          ListView(
            physics: const NeverScrollableScrollPhysics(),
            shrinkWrap: true,
            children: <Widget>[
              ListTile(
                title: Container(
                  height: 30,
                  color: Colors.black45,
                  child: Row(
                    children: const <Widget>[
                      Expanded(child: Text('Some header')),
                    ],
                  ),
                ),
              ),
            ],
          ),
          RapportList(),
        ],
      ),
    );
  }
}

class RapportList extends StatefulWidget {
  @override
  _RapportListState createState() => _RapportListState();
}

class _RapportListState extends State<RapportList> {
  @override
  Widget build(BuildContext context) {
    return ListView.separated(
      physics: const ScrollPhysics(),
      shrinkWrap: true,
      itemCount: 100,
      itemBuilder: (context, index) {
        return ListTile(
          title: Row(
            children: <Widget>[
              Expanded(child: Text('$index')),
            ],
          ),
        );
      },
      separatorBuilder: (context, index) {
        return const Divider();
      },
    );
  }
}

【问题讨论】:

    标签: flutter


    【解决方案1】:

    解决方案是使用Expended Widget, 也使用physics: NeverScrollableScrollPhysics(),。这是完整的代码:

    class MyListView extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: const Text('AppBar'),
          ),
          body: Container(
            height: MediaQuery.of(context).size.height,
            child: Column(
              children: <Widget>[
                Expanded(
                  flex:1,
                  child: ListView(
                    physics: const NeverScrollableScrollPhysics(),
                    shrinkWrap: true,
                    children: <Widget>[
                      ListTile(
                        title: Container(
                          height: 30,
                          color: Colors.black45,
                          child: Row(
                            children: const <Widget>[
                              Expanded(child: Text('Some header')),
                            ],
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
                Expanded(flex:9,child: RapportList()),
              ],
            ),
          ),
        );
      }
    }
    

    【讨论】:

    • 一旦我在最顶部的 ListView 中使用 NeverScrollableScrollPhysics,整个屏幕就不再滚动了
    • 在第二个listview中使用,你不想滚动的listview
    • @meditat 在这种情况下,所有屏幕都可以再次滚动
    • @rozerro 没有 Listview 作为父小部件使用 Column 代替
    • 我认为您要实现的目标如下:滚动 SomeList 不要滚动 firstList 并且两个屏幕的容器都不应滚动。
    【解决方案2】:

    在 Listview 中使用物理:const NeverScrollableScrollPhysics() 然后你可以防止你的小部件被滚动。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-25
      • 1970-01-01
      • 1970-01-01
      • 2019-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多