如何在Flutter中获得渐变底部导航选项卡?

时间:2020-05-10 19:14:00

标签: flutter flutter-layout flutter-bottomnavigation

发布上有一个软件包 https://pub.dev/packages/gradient_bottom_navigation_bar

但是很长一段时间没有更新。 那么,有没有一种方法可以创建具有渐变效果的自定义导航栏?

类似的东西... enter image description here

1 个答案:

答案 0 :(得分:1)

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