简体   繁体   English

如何在 flutter 中获取渐变底部导航选项卡?

[英]How to get gradient bottom navigation tab in flutter?

There is a package on pub https://pub.dev/packages/gradient_bottom_navigation_barpub https://pub.dev/packages/gradient_bottom_navigation_bar 上有一个 package

but this is not updated for a very long time.但是这个已经很久没有更新了。 So, is there a way to create own custom navigation bar with a gradient effect?那么,有没有办法创建自己的具有渐变效果的自定义导航栏?

something like this...像这样的…… 在此处输入图像描述

All it's possible with Flutter, one option could be use a transparent background in your BottomNavigationBar and put it inside a container with a BoxDecoration, try the next: Flutter 的所有可能,一个选项可以是在您的 BottomNavigationBar 中使用透明背景并将其放入带有 BoxDecoration 的容器中,尝试下一个:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: Text("Hello"),
        ),
        bottomNavigationBar: _createBottomNavigationBar(),
      ),
    );
  }

  Widget _createBottomNavigationBar() {
    return Container(
      decoration: BoxDecoration(
        gradient: LinearGradient(
          colors: [Color(0xFF00D0E1), Color(0xFF00B3FA)],
          begin: Alignment.topLeft,
          end: Alignment.topRight,
          stops: [0.0, 0.8],
          tileMode: TileMode.clamp,
        ),
      ),
      child: BottomNavigationBar(
        currentIndex: 0,
        onTap: (index) {},
        showUnselectedLabels: false,
        backgroundColor: Colors.transparent,
        type: BottomNavigationBarType.fixed,
        elevation: 0,
        unselectedItemColor: Colors.white,
        selectedIconTheme: IconThemeData(color: Colors.white),
        items: [
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            title: Text(
              "Home",
              style: TextStyle(color: Colors.white),
            ),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.business),
            title: Text(
              "Business",
              style: TextStyle(color: Colors.white),
            ),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.school),
            title: Text(
              "School",
              style: TextStyle(color: Colors.white),
            ),
          ),
        ],
      ),
    );
  }
}

I've got a more basic solution.我有一个更基本的解决方案。 You can check this DartPad .你可以检查这个 DartPad

The end result:最终结果:

颤振中的渐变底部导航栏

The trick is this:诀窍是这样的:

  • Set the background color of BottomNavigationBar as transparent.BottomNavigationBar的背景颜色设置为透明。
  • Wrap it with Stack .Stack包裹它。
  • Add a Container as first child to the Stack .添加一个Container作为第一个孩子到Stack
  • Set Container 's decoration to gradient.Container的装饰设置为渐变。
  • Set its height to 60.将其高度设置为 60。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM