我可以用脚手架 bottomNavigationBar 包裹 ListView 吗?

Can I Scaffold bottomNavigationBar wrap ListView?

我在考虑 — 如果底部需要更多菜单元素 — 我可以将 Scaffold bottomNavigationBar 包装到 ListView 并水平滚动吗?

// below code is wrong, but it shows my idea clearly (I suppose :) )
// piece of Scaffold
bottomNavigationBar: BottomNavigationBar(
        items: [
          ListView(
            children: [
              BottomNavigationBarItem(icon: Icon(Icons.home), label: "Q"),
              BottomNavigationBarItem(
                  icon: Icon(Icons.contact_mail), label: "W"),
              BottomNavigationBarItem(
                  icon: Icon(Icons.contact_page), label: "E"),
              BottomNavigationBarItem(icon: Icon(Icons.search), label: "R"),
            ],
          )
        ],
      ),

当您的 bottomNavigationBar 有 3 件或更多物品时,您不需要 ListView 。您只需要在 bottomNavigationBar: BottomNavigationBar()

下添加 type: BottomNavigationBarType.fixed

一个例子:-

bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed, // This is all you need!
items: // ...,
)

更多信息请访问下面link。

使用底部应用栏:

bottomNavigationBar: BottomAppBar(
  child: ListView(

另一种方法是使用 Tabbar:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: _title,
      home: MyStatelessWidget(),
    );
  }
}

class MyStatelessWidget extends StatelessWidget {
  const MyStatelessWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      initialIndex: 1,
      length: 9,
      child: Scaffold(
        appBar: AppBar(
          title: const Text('TabBar Widget'),
        ),
        body: Column(
          children: [
            Expanded(
              child: const TabBarView(
                children: <Widget>[
                  Center(
                    child: Text("It's cloudy here"),
                  ),
                  Center(
                    child: Text("It's rainy here"),
                  ),
                  Center(
                    child: Text("It's sunny here"),
                  ),
                  Center(
                    child: Text("It's cloudy here"),
                  ),
                  Center(
                    child: Text("It's rainy here"),
                  ),
                  Center(
                    child: Text("It's sunny here"),
                  ),
                  Center(
                    child: Text("It's cloudy here"),
                  ),
                  Center(
                    child: Text("It's rainy here"),
                  ),
                  Center(
                    child: Text("It's sunny here"),
                  ),
                ],
              ),
            ),
            Container(
              color: Colors.blue,
              child: const TabBar(
                isScrollable: true,
                tabs: <Widget>[
                  Tab(
                    icon: Icon(Icons.cloud_outlined),
                  ),
                  Tab(
                    icon: Icon(Icons.beach_access_sharp),
                  ),
                  Tab(
                    icon: Icon(Icons.brightness_5_sharp),
                  ),
                  Tab(
                    icon: Icon(Icons.cloud_outlined),
                  ),
                  Tab(
                    icon: Icon(Icons.beach_access_sharp),
                  ),
                  Tab(
                    icon: Icon(Icons.brightness_5_sharp),
                  ),
                  Tab(
                    icon: Icon(Icons.cloud_outlined),
                  ),
                  Tab(
                    icon: Icon(Icons.beach_access_sharp),
                  ),
                  Tab(
                    icon: Icon(Icons.brightness_5_sharp),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}