我正在try 添加一个BottomNavigationBar,每当你点击屏幕(在我的实例中点击 map )或向下滑动时,它的大小会变小,其中的一些项目会被移除.

例如:

Concept

我正在try 复制应用程序Citymapper的BNB

推荐答案

要做到这一点,一种方法是在底部导航栏附加的详细信息上方添加一个透明层.在UI的该部分中,您可以使用GestureDetector来处理手势,但这必须使用Stack来设计.以下是一个最小的代码.

import 'package:flutter/material.dart';

void main() {
  runApp(const MaterialApp(
    home: App(),
  ));
}

class App extends StatefulWidget {
  const App({super.key});

  @override
  State<App> createState() => _AppState();
}

class _AppState extends State<App> {
  bool isExpanded = true;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.amber,
        body: Stack(
          children: [
            ///Your map view goes at the bottom of the stack
            const Center(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Icon(Icons.map),
                  Text("Map view"),
                ],
              ),
            ),
            ///Your custom bottom navigation bar
            Align(
              alignment: Alignment.bottomCenter,
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  if (isExpanded)
                    Expanded(
                      child: ExpandedView(onTap: () {
                        setState(() {
                          isExpanded = false;
                        });
                      }),
                    ),
                  BottomNavigationBar(
                    onTap: (_) {
                      setState(() {
                        isExpanded = true;
                      });
                    },
                    elevation: 0,
                    items: const [
                      BottomNavigationBarItem(
                          icon: Icon(Icons.local_taxi_outlined), label: "Taxi"),
                      BottomNavigationBarItem(
                          icon: Icon(Icons.train_outlined), label: "Train")
                    ],
                  ),
                ],
              ),
            )
          ],
        ));
  }
}

class ExpandedView extends StatelessWidget {
  final VoidCallback onTap;
  const ExpandedView({super.key, required this.onTap});

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Expanded(
          child: GestureDetector(
            //handle other gesture callbacks
            onTap: onTap,
            child: Container(
              color: Colors.transparent,
            ),
          ),
        ),
        Container(
          height: 150,
          width: MediaQuery.sizeOf(context).width,
          color: Colors.green,
        ),
      ],
    );
  }
}

Flutter相关问答推荐

Flutter—如何在Riverpod中使用全类状态?

如何等待抖动S定时器.定期取消?

build_runner显示错误没有为类ClassDeclaration定义getter宏关键字'

使用ThemeProvider不在亮模式和暗模式之间切换

Flutter -如何停止块监听程序在后台堆栈中运行

Flutter BLoC et workmanager:如何创建独特的仓库初始化模式?

你能go 掉Flutter 小部件测试中的样板吗?

任务';:app:check DebugDuplicateClasss';的Ffltter Share_plus抛出执行失败

Flutter-Riverpod:如何只从提供者读取一次值

Flutter - Riverpod 2.0 - 如何访问 onDispose 回调?

如何使自动焦点位于已经有一些文本的 TextField 的第一行?

将状态维护到 asyncNotifier

无法使用 bloc 更新 ListView.builder 的特定时间

这是使用导航功能处理从一页到另一页的移动的最佳方式

Flutter RIverpod 奇怪地改变状态

通过点击缩略图加载全屏图像

如何从 Flutter 中的列表中提取 JSON?

如何做到这一点,当我们在 textified 中输入 ab 然后将 applebanana 显示为一个单词?飘飘然

FLUTTER+SQL:在 ListViewBuilder 中显示数据库中的数据

我想将我的模型类对象发送到下一个屏幕之前我这样做首先我在第二个屏幕中声明对象