搜索插件、代码仓与 APP 分类

GitHub 代码仓mt-akar/bottom_nav_layout

bottom_nav_layout

一个快速且功能强大的 Flutter 布局,带有底部导航栏。

bottom_nav_layout 项目封面
星标
35
复刻
11
最近推送(UTC)
2023年7月4日
项目状态
未归档

原始 README

以下为英文项目原文快照,最新内容请访问 GitHub。

bottom_nav_layout

License: MIT GitHub Repo stars GitHub followers

Demo

stack

What is bottom_nav_layout?

It is a quick flutter app layout for building an app with a bottom nav bar. You can get an app with fluent behavior running in 15 lines of code.

Why bottom_nav_layout?
  • Eliminates all boilerplate code for bottom nav bar coordination.
  • Offers additional common features.
    • Page state preservation
    • Lazy page loading
    • Page transition animations
    • In-page navigation
    • Back button navigation for Android
  • Works with any bottom bar you want. Use the material or cupertino bottom bar, grab one from pub.dev or use your own.
  • You can customize or turn any feature off.

Content

Usage

Installation

Add the following to your pubspec.yaml file.

dependencies:
  bottom_nav_layout: ^3.0.10

Quick Start Example

import 'package:bottom_nav_layout/bottom_nav_layout.dart';

void main() => runApp(MaterialApp(
      home: BottomNavLayout(
        // The app's destinations
        pages: [
          (_) => Center(child: Text("Welcome to bottom_nav_layout")),
          (_) => SliderPage(),
          (_) => Center(child: TextField(decoration: InputDecoration(hintText: 'Go..'))),
        ],
        bottomNavigationBar: (currentIndex, onTap) => BottomNavigationBar(
          currentIndex: currentIndex,
          onTap: (index) => onTap(index),
          items: [
            BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),
            BottomNavigationBarItem(icon: Icon(Icons.linear_scale), label: 'Slider'),
            BottomNavigationBarItem(icon: Icon(Icons.search), label: 'Search'),
          ],
        ),
      ),
    ));

Done. You have a complete, working application.

SliderPage code

Parameters

Name Description Default
pages The app's destinations. N/A
bottomNavigationBar The bottom navbar of the layout. N/A
savePageState When false, the pages are reinitialized every time they are navigated. (Material behavior). When true, the pages are initialized once and hidden/shown on navigation. (Cupertino behavior) false
lazyLoadPages When false, pages are created in the beginning. When true, pages are created when they are navigated for the first time. false
pageStack Navigation stack that remembers pages visited. Enhances back button management on Android. ReorderToFrontPageStack for Android, NoPageStack for iOS
extendBody Passed to Scaffold.extendBody. false
resizeToAvoidBottomInset Passed to Scaffold.resizeToAvoidBottomInset. true
pageTransitionData Animation configuration for page transitions. null

Inner Widget Tree

inner_widget_tree


image

Page State Preservation

The state changes you made in a page such as scroll amount, sub-navigation, form inputs etc. are preserved. You can enable it as per Cupertino Design Guidelines or disable it as per Material Design Guidelines

savePageState: true, // Default is false

Lazy Page Loading

The layout offers the option to lazily create the pages using the passed in page builders. When lazyLoadPages is set to true, the pages are not created until they are navigated to for the first time. This is useful when a non-initial page;

  • Has a launch animation.
  • Runs a heavy process that is not needed until the page is opened.
lazyLoadPages: true, // Default is false

Page Back Stack

Documentation Example
documentation back stack example, programmatic navigation example

The layout remembers the order of pages navigated and when back button is pressed, navigates back to the previously navigated page.

// Default is ReorderToFrontPageStack for Android and NoPageStack for iOS.
pageStack: ReorderToFrontPageStack(initialPage: 0),

There are many useful page back stack behaviors implemented such as reorder-to-front and replace-except-first. You can also implement your own.

You also specify the initialPage inside PageStack.

To change the current page programmatically, you can use

// Navigate to a page. 
myPageStack.push(2);

// Navigate back to the previous page on the stack.
myPageStack.pop();

// PageStacks inherit ListQueue.
var top = myPageStack.peek();
var length = myPageStack.length;

Page Transition Animation

Documentation Example
- example

You can set an transition animation between pages. Create your own AnimationBuilder or use one of the built in ones.

These animation work with both bottom navbar and Android back button.

// Default is null.
pageTransitionData: PageTransitionData(
  builder: PrebuiltAnimationBuilderBuilders.zoomInAndFadeOut,
  duration: 150,
  direction: AnimationDirection.inAndOut,
),

In-Page Navigation

Documentation Example
documentation example

The layout maintains a flat navigation pattern.


Figure: Flat Navigation

Benefits

  1. Navigator per page is trivial to set up.
  2. You only need to push pages you need. Pops are handled by the layout.
  3. Android back button navigates both in-page and among pages.
  4. Bottom bar pops all in-page stack when the current bar item is reselected.
  5. If you put an app bar to your page, it will show the up button correctly.

To do this, the page should have a Navigator widget that use the passed in GlobalKey as its key.

pages: [
  (navKey) => Navigator(
        key: navKey,
        initialRoute: "/",
        onGenerateRoute: (routeSettings) => MaterialPageRoute(
          builder: (context) {
            if (routeSettings.name == "/")
              return OverviewPage();
            else if (routeSettings.name == "/details")
              return DetailsPage();
            else
              return Center(child: Text("Unknown route."));
          },
        ),
      ),
  (_) => SliderPage(),
  (_) => SliderPage(),
],

Different Bottom Bars

Documentation Example
documentation example

So far, we only worked on Material bottom nav bar. The layout supports any bottom bar.

Example usage of flutter_snake_navigationbar:

bottomNavigationBar: (currentIndex, onTap) => SnakeNavigationBar.color(
  currentIndex: currentIndex,
  onTap: (index) => onTap(index),
  items: [
    BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),
    BottomNavigationBarItem(icon: Icon(Icons.linear_scale), label: 'Slider'),
    BottomNavigationBarItem(icon: Icon(Icons.search), label: 'Search'),
  ],
),

Improvements

  • Tell me if you want to see a feature your app has/needs in this package. I will do my best to integrate it.
  • I am also considering to make a drawer_nav_layout package. If you are interested, let me know!

Community

Any feedback is appreciated. 🚀🚀

I love talking about code. Find me on discord at ViraL#2868

If you like this work, please consider 👍 the package and ⭐ the repo. It is appreciated.

If you have queries, feel free to create an issue.

If you would like to contribute, feel free to create a PR.

Mustafa Akar GitHub avatar
GitHub 作者

Mustafa Akar ↗

Ankara
语言DartHTMLSwiftKotlinObjective-C

分类

使用的依赖

依赖清单 3 项

  • universal_io^2.0.4
  • flutter{"sdk":"flutter"}
  • flutter_test开发依赖{"sdk":"flutter"}

如何选择适合项目的代码仓?

先按使用场景筛选,再比较项目活跃情况,最后到原始仓库核对使用要求。

  1. 通过仓库名、作者或描述搜索,再按技术分类缩小范围。
  2. 结合星标、复刻与最近推送比较项目;星标反映社区关注,不代表质量保证。
  3. 使用前在 GitHub 核对 README、许可证与 Issues。

来源与维护

“A repository is the most basic element of GitHub.”

GitHub Docs — About repositories ↗