繁体   English   中英

如何在 flutter 中创建浮动按钮栏?

[英]how to create floating button bar in flutter?

我想在 flutter 中创建一个浮动按钮栏,类似于共享的图像中显示的floating bottom bar 在此处输入图像描述

上图来自依赖https://pub.dev/packages/floating_bottom_navigation_bar ,这是用于页面导航,但我不希望导航只希望在按下按钮时执行操作,并且视图应该是相同的。

您可以在下面复制粘贴运行完整代码
您不需要将页面导航逻辑放入其中,视图将相同
可以直接查看onTap知道用户点击了哪个按钮

bottomNavigationBar: FloatingNavbar(
          onTap: (int val) {
            switch (val) {
              case 0:
                {
                  action = "Home";
                }
                break;

工作演示

在此处输入图像描述

完整代码

import 'package:floating_bottom_navigation_bar/floating_bottom_navigation_bar.dart';
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  // This widget is the root of your application.
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  int _index = 0;
  String action = "Home";

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Floating NavBar Example',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Floating NavBar Example'),
          centerTitle: true,
        ),
        //If you want to show body behind the navbar, it should be true
        extendBody: true,
        body: Center(
          child: Column(
            children: [
              Text(
                "index: $_index",
                style: TextStyle(
                  fontSize: 52,
                ),
              ),
              Text("$action"),
            ],
          ),
        ),
        bottomNavigationBar: FloatingNavbar(
          onTap: (int val) {
            switch (val) {
              case 0:
                {
                  action = "Home";
                }
                break;

              case 1:
                {
                  action = "Explore"; //statements;
                }
                break;
              case 2:
                {
                  action = "Chats"; //statements;
                }
                break;
              case 3:
                {
                  action = "Settings"; //statements;
                }
                break;
              default:
                {
                  //statements;
                }
                break;
            }

            setState(() => _index = val);
          },
          currentIndex: _index,
          items: [
            FloatingNavbarItem(icon: Icons.home, title: 'Home'),
            FloatingNavbarItem(icon: Icons.explore, title: 'Explore'),
            FloatingNavbarItem(icon: Icons.chat_bubble_outline, title: 'Chats'),
            FloatingNavbarItem(icon: Icons.settings, title: 'Settings'),
          ],
        ),
      ),
    );
  }
}

暂无
暂无

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

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