【问题标题】:How to add Floating action Button in Bottom Navigation Bar Center with notch?如何在底部导航栏中心添加带有缺口的浮动操作按钮?
【发布时间】:2019-04-17 12:12:49
【问题描述】:

我正在尝试在底部导航栏的中间添加一个浮动操作按钮。问题是没有出现缺口。这是问题的图片。

我的is码是这样的

import 'package:flutter/material.dart';

class MyHome extends StatefulWidget {
  @override
  _MyHomeState createState() => _MyHomeState();
}

class _MyHomeState extends State<MyHome> {
  int _selectedTab = 0;
  final _pageOptions = [
    Text('Item 1'),
    Text('Item 2'),
    Text('Item 3'),
    Text('Item 4'),
  ];

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primaryColor: Color(0xffFF5555),
      ),
      home: Scaffold(
        body: _pageOptions[_selectedTab],
        floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
        floatingActionButton: FloatingActionButton(
          onPressed: () {},
          child: Icon(Icons.add),
          backgroundColor: Colors.red,
          foregroundColor: Colors.white,
          elevation: 2.0,
        ),
        bottomNavigationBar: BottomAppBar(
          notchMargin: 2.0,
          shape: CircularNotchedRectangle(),
          child: SizedBox(
            height: 80,
            child: Theme(
              data: Theme.of(context).copyWith(
                  // sets the background color of the `BottomNavigationBar`
                  canvasColor: Color(0xff1B213B),

                  // sets the active color of the `BottomNavigationBar` if `Brightness` is light
                  primaryColor: Color(0xffFF5555),
                  textTheme: Theme.of(context)
                      .textTheme
                      .copyWith(caption: new TextStyle(color: Colors.white))),
              child: BottomNavigationBar(
                type: BottomNavigationBarType.fixed,
                currentIndex: _selectedTab,
                onTap: (int index) {
                  setState(() {
                    _selectedTab = index;
                  });
                },
                fixedColor: Color(0xffFF5555),
                items: [
                  BottomNavigationBarItem(
                      icon: Icon(Icons.tv), title: Text('')),
                  BottomNavigationBarItem(
                      icon: Icon(Icons.card_membership), title: Text('')),
                  BottomNavigationBarItem(
                      icon: Icon(Icons.share), title: Text('')),
                  BottomNavigationBarItem(
                      icon: Icon(Icons.home), title: Text('')),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}

我想在中间那个红色的 fab 图标周围添加一个缺口。我尝试了两种形状:CircularNotchedRectangle() 和 AutomaticNotchedShape 方法。但没有任何效果。大多数示例显示在“BottomAppBar”中使用“Row”。但我想使用 BottomNavigationBar。请帮助我找到解决方案

【问题讨论】:

标签: android dart flutter flutter-layout


【解决方案1】:

在BottomAppBar(),小部件中使用这个属性

clipBehavior: Clip.antiAlias,

【讨论】:

    【解决方案2】:
    Scaffold(
      floatingActionButtonLocation: FloatingActionButtonLocation.endDocked,
      floatingActionButton: FloatingActionButton(...),
      bottomNavigationBar: BottomAppBar(
        shape: CircularNotchedRectangle(),
        clipBehavior: Clip.antiAliasWithSaveLayer,
        ...
      ),
    );
    

    【讨论】:

    • 请不要发布仅代码的答案。相反,请解释它为什么有帮助并提供指向上下文来源的链接。我们正在努力进行教育,而不仅仅是解决眼前的问题。
    【解决方案3】:
    Scaffold(
          appBar: AppBar(
            title: Text('Demo'),
          ),
          backgroundColor: Theme.of(context).backgroundColor,
          floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
          floatingActionButton:
          SizedBox(
            width: 80.0,
            height: 80.0,
            child: FloatingActionButton(
              backgroundColor: Theme.of(context).primaryColor,
                child: Icon(Icons.add), onPressed: () {}),
          ),
          bottomNavigationBar: BottomAppBar(
              color: Theme.of(context).secondaryHeaderColor,
              child: Container(
                height: 60,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    IconButton(icon: Icon(Icons.settings), onPressed: () {}),
                    IconButton(icon: Icon(Icons.speaker_notes_outlined), onPressed: () {}),
                    SizedBox(width: 40), // The dummy child
                    IconButton(icon: Icon(Icons.person), onPressed: () {}),
                    IconButton(icon: Icon(Icons.email), onPressed: () {}),
                  ],
                ),
              )),
        );
    

    【讨论】:

    • 虽然此代码可能会解决问题,including an explanation 关于如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的回答添加解释并说明适用的限制和假设。
    猜你喜欢
    • 2020-01-14
    • 1970-01-01
    • 2022-10-05
    • 1970-01-01
    • 2019-11-08
    • 2022-08-11
    • 1970-01-01
    • 1970-01-01
    • 2020-11-19
    相关资源
    最近更新 更多