flutter 如何使列表在抽屉中可滚动

at0kjp5o  于 2022-12-14  发布在  Flutter
关注(0)|答案(3)|浏览(172)

我想使内部列可滚动。我如何才能实现它,尝试替换为列表视图和单个子滚动视图,但不工作。这是小部件结构

>Drawer
  >>Container
    >>>SingleChildScrollView(never Scrollable)
       >>>>Column
          >>>>>Drawer Header
          >>>>>Column
          >>>>>ListView.builder(never scrollble physics)
          >>>>>Row

请帮帮忙

ct3nt3jp

ct3nt3jp1#

在scrollable中使用ListView时,需要限制它的高度。最简单的方法是将ListViewshrinkWrap属性设置为true。
请参阅Vertical viewport was given unbounded height

disho6za

disho6za2#

试一次这个,然后告诉我们。
在这里,SingleChildScrollView小部件用于 Package 内部Column小部件,使其可以滚动。

Scaffold(
  drawer: Drawer(
    child: Container(
      child: SingleChildScrollView(
        child: Column(
          children: [
            DrawerHeader(),
            SingleChildScrollView(
              child: Column(
                children: [
                  // your scrollable content
                ],
              ),
            ),
            ListView.builder(
              physics: AlwaysScrollableScrollPhysics(),
              itemBuilder: (context, index) {
                // return a widget for each item in your list
              },
            ),
            Row(
              children: [
                // other content
              ],
            ),
          ],
        ),
      ),
    ),
  ),
  // other properties of the Scaffold widget
)
rqqzpn5f

rqqzpn5f3#

首先,创建一个List〈Map〈String,dynamic〉〉,在其中存储您的内容及其操作,即

final _actions = [
    {'name': 'My Profile', 'onTap': () {}},
    {'name': 'Setting', 'onTap': () {}},
    {'name': 'Terms and Conditions', 'onTap': () {}},
    {'name': 'Privacy Policy', 'onTap': () {}},
  ];

然后你就可以实现你的抽屉

drawer: Drawer(
  child: Column(
    children: [
      const UserAccountsDrawerHeader(
        accountName: Text('accountName'),
        accountEmail: Text('accountEmail'),
      ),
      Expanded(
        child: ListView.builder(
          itemBuilder: (_, i) {
            final action = _actions[i];
            return ListTile(
              onTap: (action['onTap'] as VoidCallback),
              title: Text((action['name'] as String?) ?? ''),
            );
          },
          itemCount: _actions.length,
        ),
      )
    ],
  ),
),

相关问题