日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

在 Flutter 中使用 NavigationRail 和 BottomNavigationBar

發布時間:2025/3/19 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在 Flutter 中使用 NavigationRail 和 BottomNavigationBar 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在 Flutter 中使用 NavigationRail 和 BottomNavigationBar

作者:堅果

公眾號:“大前端之旅”

華為云享專家,InfoQ簽約作者,阿里云專家博主,51CTO博客首席體驗官,開源項目GVA成員之一,專注于大前端技術的分享,包括Flutter,鴻蒙,小程序,安卓,VUE,JavaScript等。

本文將向您展示如何使用NavigationRailBottomNavigationBar在 Flutter 中創建****自適應布局。我們將瀏覽一下這個概念,然后通過一個完整的例子來在實踐中應用這個概念。

NavigationRail小部件用于創建位于應用左側或右側的“垂直標簽欄”。它非常適合平板電腦、筆記本電腦、電視等寬屏設備。它通常包含多個視圖,讓用戶可以輕松地在不同視圖之間切換。

BottomNavigationBar部件用于創建非常適合智能手機的底部標簽欄。它由多個選項卡組成,讓用戶可以輕松地在視圖之間導航。

我們可以使用NavigationRailBottomNavigationBar來構建現代自適應布局。當屏幕很大時,我們顯示NavigationRail,當屏幕較小時,我們顯示BottomNavigationBar。一次只出現其中一個。要檢測屏幕寬度,我們可以使用:

MediaQuery.of(context).size.width

這個例子

應用預覽

我們要構建的應用程序有一個導航欄、一個底部標簽欄和 4 個不同的視圖:主頁、Feed、收藏夾和設置。每個視圖都與底部標簽欄的一個標簽和導航欄的一個項目相連。

  • 如果屏幕寬度小于 640 像素,則將呈現底部標簽欄,而不會顯示左側導航欄。
  • 如果屏幕寬度等于或大于 640 像素,則不會呈現底部標簽欄,而會顯示左側導航欄。

以下是它的工作原理:

截圖

代碼

這是生成上述應用程序的完整代碼(帶有解釋):

// main.dart import 'package:flutter/material.dart';void main() {runApp(const MyApp()); }class MyApp extends StatelessWidget {const MyApp({Key? key}) : super(key: key);@overrideWidget build(BuildContext context) {return MaterialApp(// Remove the debug bannerdebugShowCheckedModeBanner: false,title: '大前端之旅',theme: ThemeData(primarySwatch: Colors.indigo),home: const HomeScreen());} }class HomeScreen extends StatefulWidget {const HomeScreen({Key? key}) : super(key: key);@override_HomeScreenState createState() => _HomeScreenState(); }class _HomeScreenState extends State<HomeScreen> {// The contents of views// Only the content associated with the selected tab is displayed on the screenfinal List<Widget> _mainContents = [// Content for Home tabContainer(color: Colors.yellow.shade100,alignment: Alignment.center,child: const Text('Home',style: TextStyle(fontSize: 40),),),// Content for Feed tabContainer(color: Colors.purple.shade100,alignment: Alignment.center,child: const Text('Feed',style: TextStyle(fontSize: 40),),),// Content for Favorites tabContainer(color: Colors.red.shade100,alignment: Alignment.center,child: const Text('Favorites',style: TextStyle(fontSize: 40),),),// Content for Settings tabContainer(color: Colors.pink.shade300,alignment: Alignment.center,child: const Text('Settings',style: TextStyle(fontSize: 40),),)];// The index of the selected tab// In the beginning, the Home tab is selectedint _selectedIndex = 0;@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: const Text('大前端之旅'),),// Show the bottom tab bar if screen width < 640bottomNavigationBar: MediaQuery.of(context).size.width < 640? BottomNavigationBar(currentIndex: _selectedIndex,unselectedItemColor: Colors.grey,selectedItemColor: Colors.indigoAccent,// called when one tab is selectedonTap: (int index) {setState(() {_selectedIndex = index;});},// bottom tab itemsitems: const [BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),BottomNavigationBarItem(icon: Icon(Icons.feed), label: 'Feed'),BottomNavigationBarItem(icon: Icon(Icons.favorite), label: 'Favorites'),BottomNavigationBarItem(icon: Icon(Icons.settings), label: 'Settings')]): null,body: Row(mainAxisSize: MainAxisSize.max,children: [// Show the navigaiton rail if screen width >= 640if (MediaQuery.of(context).size.width >= 640)NavigationRail(minWidth: 55.0,selectedIndex: _selectedIndex,// Called when one tab is selectedonDestinationSelected: (int index) {setState(() {_selectedIndex = index;});},labelType: NavigationRailLabelType.all,selectedLabelTextStyle: const TextStyle(color: Colors.amber,),leading: Column(children: const [SizedBox(height: 8,),CircleAvatar(radius: 20,child: Icon(Icons.person),),],),unselectedLabelTextStyle: const TextStyle(),// navigation rail itemsdestinations: const [NavigationRailDestination(icon: Icon(Icons.home), label: Text('Home')),NavigationRailDestination(icon: Icon(Icons.feed), label: Text('Feed')),NavigationRailDestination(icon: Icon(Icons.favorite), label: Text('Favorites')),NavigationRailDestination(icon: Icon(Icons.settings), label: Text('Settings')),],),// Main content// This part is always shown// You will see it on both small and wide screenExpanded(child: _mainContents[_selectedIndex]),],),);} }

構造函數和引用

NavigationRail 構造函數:

NavigationRail({Key? key, Color? backgroundColor, bool extended = false, Widget? leading, Widget? trailing, required List<NavigationRailDestination> destinations, required int selectedIndex, ValueChanged<int>? onDestinationSelected, double? elevation, double? groupAlignment, NavigationRailLabelType? labelType, TextStyle? unselectedLabelTextStyle, TextStyle? selectedLabelTextStyle, IconThemeData? unselectedIconTheme, IconThemeData? selectedIconTheme, double? minWidth, double? minExtendedWidth, bool? useIndicator, Color? indicatorColor })

BottomNavigationBar 構造函數:

BottomNavigationBar({Key? key, required List<BottomNavigationBarItem> items, ValueChanged<int>? onTap, int currentIndex = 0, double? elevation, BottomNavigationBarType? type, Color? fixedColor, Color? backgroundColor, double iconSize = 24.0, Color? selectedItemColor, Color? unselectedItemColor, IconThemeData? selectedIconTheme, IconThemeData? unselectedIconTheme, double selectedFontSize = 14.0, double unselectedFontSize = 12.0, TextStyle? selectedLabelStyle, TextStyle? unselectedLabelStyle, bool? showSelectedLabels, bool? showUnselectedLabels, MouseCursor? mouseCursor, bool? enableFeedback, BottomNavigationBarLandscapeLayout? landscapeLayout })

參考:

  • navigation:設計(material.io)
  • NavigationRail 類(flutter.dev)
  • NavigationRailDestination類 (flutter.dev)
  • BottomNavigationBar 類(flutter.dev)

后記

您已經學習了一種使用 NavigationRail 和 BottomNavigationBar 創建現代自適應用戶界面的簡單但有效的技術。考慮到這些知識,您可以為從智能手機到平板電腦和筆記本電腦的各種設備構建更直觀、更有吸引力的應用程序。因此,您的應用程序將獲得越來越多的用戶,并有更大的成功機會。

總結

以上是生活随笔為你收集整理的在 Flutter 中使用 NavigationRail 和 BottomNavigationBar的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 18岁成年人网站 | 9l视频自拍九色9l视频成人 | 18成人免费观看网站下载 | 好吊妞一区二区三区 | 综合人人| 性生活毛片 | 9i免费看片黄 | 国产成人在线一区二区 | 亚洲视频在线播放免费 | 夫妻露脸自拍[30p] | 中文字幕一区二区精品 | 亚洲不卡电影 | 久久96视频 | 久青草视频在线 | 日韩一二区 | 视频国产在线 | 黄色aa网站 | 亚洲精品免费电影 | 国产视频一区二区视频 | 草草视频在线观看 | 黄色av大片| 精品无码久久久久久久久久 | 亚洲精品久 | 久久久夜色 | 欧美日韩免费一区 | 4438x五月天| 麻豆国产精品777777在线 | 亚洲精品国产精品国自产观看浪潮 | 中文字幕综合网 | 日韩一级色| 在线免费观看一区二区三区 | 小宵虎南在线观看 | 好吊色av| 综合伊人av| 图书馆的女友动漫在线观看 | 欧美小视频在线 | 久久久久久久久久久久久久久久久 | 精品久久一区二区三区 | 这里只有精品66 | 国产v亚洲 | 91在线网| 777久久久精品一区二区三区 | 欧美韩国日本在线 | 久久精品黄 | 久久免费的精品国产v∧ | av网站免费看| 亚洲第一区av | 男女在线视频 | 爱爱爱网 | 啪啪导航| 成人久久精品 | 国产欧美一区二区 | 国产欧美一区二区三区另类精品 | 欧美激情亚洲色图 | 亚洲毛片一区二区三区 | 国产一区二区三区四区五区美女 | 美女扒开下面让男人捅 | 成人福利视频在线观看 | 丨国产丨调教丨91丨 | 91精品国产综合久久久蜜臀粉嫩 | 国产精品成人久久久久久久 | 一进一出好爽视频 | 午夜免费视频 | 亚洲精品小视频在线观看 | 午夜激情福利电影 | 久久免费视频精品 | 精品动漫一区二区三区在线观看 | av不卡在线免费观看 | 人妻在线一区二区 | 国产成人精品综合久久久久99 | 成人欧美一区二区三区黑人一 | 男人天堂五月天 | 国产欧美一区二区视频 | 日韩av免费在线播放 | 葵司在线视频 | 欧美偷拍一区二区 | 嫩草研究院在线观看 | 国产乡下妇女做爰毛片 | 成人自拍视频网站 | 欧洲xxxxx | 一区二区免费在线播放 | 超碰在线最新 | 捆绑调教视频网站 | 中字幕一区二区三区乱码 | 又色又爽又黄gif动态图 | www在线播放 | 丁香六月av| 天堂va欧美va亚洲va老司机 | 青草伊人网| 国产黄色影院 | 欧美粗暴jizz性欧美20 | 日本a网| 91久久爽久久爽爽久久片 | 中文字幕日本在线观看 | 精品人伦一区二区三区蜜桃网站 | 欧美a一级 | 免费成人美女女电影 | 蜜臀av免费一区二区三区水牛 | 久久午夜夜伦鲁鲁片无码免费 |