如何在颤动中创建浮动按钮栏?
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'),
],
),
),
);
}
}
我想在 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'),
],
),
),
);
}
}