flutter SingleChildScrollView不可滚动

v64noz0r  于 2023-05-08  发布在  Flutter
关注(0)|答案(3)|浏览(270)

除了不可滚动外,此小部件呈现没有错误

SingleChildScrollView(
    child: Column(
      children: [
        ListView(
          shrinkWrap: true,
          children: [
            ListTile(
              title: Row(
                children: const [
                    Expanded(child: Text('text'),),
                    Expanded(child: Text('text'),),
                ],
              ),
            ),
          ],
        ),
        RapportList(), // this is not scrollable
      ],
    ),
),

其中RapportList()是一个statefull小部件,它构建一个

ListView.builder(
  shrinkWrap: true,
  itemCount: _rapports.length,
  itemBuilder: (context, index) {
    return ListTile(
      title: Row(
        children: <Widget>[
        ...

我尝试用SingleChildScrollView Package ListView.builder,但没有结果。它仍然不可滚动。

euoag5mw

euoag5mw1#

我想你只需要补充:

physics: const NeverScrollableScrollPhysics(),

你的RapportList()
下面是我测试的代码:

import 'package:flutter/material.dart';

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

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

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, 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: <Widget>[
            ListView(
              shrinkWrap: true,
              children: <Widget>[
                ListTile(
                  title: Row(
                    children: const <Widget>[
                      Expanded(child: Text('text'),),
                      Expanded(child: Text('text'),),
                    ],
                  ),
                ),
              ],
            ),
            ListView.builder( //<--RapportList().
              physics: const NeverScrollableScrollPhysics(), //<--here
              shrinkWrap: true,
              itemCount: 100,
              itemBuilder: (context, index){
                return ListTile(
                  title: Row(
                    children: <Widget>[
                      Text("ListTile with index ${index}")
                    ],
                  ),
                );
              },
            ),
          ],
        ),
      ),
    );
  }
}

这样,RapportList()将不可滚动,当您尝试“滚动”其中一个元素时,您将滚动整个SingleChildScrollView();

szqfcxe2

szqfcxe22#

使用Expanded小部件 Package 它:有时,如果SingleChildScrollView小部件没有足够的空间进行扩展,它可能无法滚动。您可以尝试使用Expanded小部件 Package 它,以给予更多的垂直扩展空间。例如:

Expanded(
  child: SingleChildScrollView(
    child: // your code 
  ),
)
pgvzfuti

pgvzfuti3#

@override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(body:  SingleChildScrollView(
    child: Column(
      children: <Widget>[
        ListView(
          shrinkWrap: true,
          children: <Widget>[
            ListTile(
              title: Row(
                children: const <Widget>[
                    Expanded(child: Text('text'),),
                    Expanded(child: Text('text'),),
                ],
              ),
            ),
          ],
        ),
        SizedBox(height: 6000,),
        Text("...."),
        Container(
        ),
      ],
    ),
)
    );  
  }
}

相关问题