簡體   English   中英

我可以用腳手架 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下添加type: BottomNavigationBarType.fixed bottomNavigationBar: BottomNavigationBar()

一個例子:-

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

欲了解更多信息,請訪問以下鏈接。

堆棧溢出時的底部導航欄問題

使用 BottomAppBar:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM