【问题标题】:How to set border radius to bottom app bar in a flutter app?如何在颤动应用程序中将边框半径设置为底部应用程序栏?
【发布时间】:2019-10-27 20:34:19
【问题描述】:

我想将 borderRadius 设置为 底部导航应用栏,如图所示。我曾尝试将底部导航应用栏放置在 ClipRRect borderRadiusContainer 装饰中,但没有成功。那么如何将 topLeft 和 topRight 边框半径应用于我的底部导航栏。请帮助让我知道我该怎么做?

main.dart

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Food Ordering',
      theme: ThemeData(primarySwatch: Colors.blue, primaryColor: Colors.white),
      home: MyStatefulWidget(),
      routes: <String, WidgetBuilder>{
        '/detail-page': (BuildContext context) => MyDetailPage(),
      },
    );
  }
}

class MyStatefulWidget extends StatefulWidget {
  MyStatefulWidget({Key key}) : super(key: key);

  @override
  _MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  int _selectedIndex = 0;
  static const TextStyle optionStyle =
      TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
  static List<Widget> _widgetOptions = <Widget>[
    HomePage(),
    HomePage(),
    HomePage(),
    HomePage(),
  ];

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: _widgetOptions.elementAt(_selectedIndex),
      ),
      bottomNavigationBar: BottomNavigationBar(
          items: <BottomNavigationBarItem>[
            BottomNavigationBarItem(
              icon: Image.asset('assets/icon-home.png'),
              title: Text('Home'),
            ),
            BottomNavigationBarItem(
              icon: Image.asset('assets/icon-mentors.png'),
              title: Text('Mentors'),
            ),
            BottomNavigationBarItem(
              icon: Image.asset('assets/icon-messages.png'),
              title: Text('Messages'),
            ),
            BottomNavigationBarItem(
              icon: Image.asset('assets/icon-settings.png'),
              title: Text('Settings'),
            ),
          ],
          currentIndex: _selectedIndex,
          selectedItemColor: Colors.blue,
          onTap: _onItemTapped),
    );
  }
}

【问题讨论】:

  • 看起来不是一件小事。但可能您可能需要将 BottomNavigationBar 包装到 Material 小部件中。至少Material 的 API 文档说它正在处理裁剪和整形。虽然我不确定它们是否有你需要的现成形状。
  • 它已经封装在一个 MaterialApp 中,看看更新后的代码。

标签: flutter dart flutter-layout


【解决方案1】:

编辑

Scaffold 现在有一个名为extendBody 的属性,可用于在bottomBar 下方扩展主体。从文档中,

如果为 true,并且指定了bottomNavigationBar 或persistentFooterButtons,则body 将延伸到Scaffold 的底部,而不是仅延伸到bottomNavigationBar 或persistentFooterButtons 的顶部。

这意味着你需要做的就是

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Some Text'),
      ),
      body: bodyContent,
      extendBody: true,
      bottomNavigationBar: bottomNavigationBar,
    );
  }

  Widget get bodyContent {
    return Container(color: Colors.red);
  }

  Widget get bottomNavigationBar {
    return ClipRRect(
      borderRadius: const BorderRadius.only(
        topRight: Radius.circular(40),
        topLeft: Radius.circular(40),
      ),
      child: BottomNavigationBar(
        items: const [
          BottomNavigationBarItem(icon: Icon(Icons.home), label: '1'),
          BottomNavigationBarItem(icon: Icon(Icons.usb), label: '2'),
          BottomNavigationBarItem(
              icon: Icon(Icons.assignment_ind), label: '3'),
          BottomNavigationBarItem(
              icon: Icon(Icons.multiline_chart), label: '4'),
        ],
        unselectedItemColor: Colors.grey,
        selectedItemColor: Colors.black,
        showUnselectedLabels: true,
      ),
    );
  }
}

过时

把它放在一个堆栈中。不要直接将底部导航栏添加到脚手架。

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Some Text'),
      ),
      body: Stack(
        children: <Widget>[
          bodyContent,
          Positioned(
            left: 0,
            right: 0,
            bottom: 0,
            child: bottomNavigationBar,
          ),
        ],
      ),
    );
  }

  Widget get bodyContent {
    return Container(color: Colors.red);
  }

  Widget get bottomNavigationBar {
    return ClipRRect(
      borderRadius: BorderRadius.only(
        topRight: Radius.circular(40),
        topLeft: Radius.circular(40),
      ),
      child: BottomNavigationBar(
        items: [
          BottomNavigationBarItem(icon: Icon(Icons.home), title: Text('1')),
          BottomNavigationBarItem(icon: Icon(Icons.usb), title: Text('2')),
          BottomNavigationBarItem(
              icon: Icon(Icons.assignment_ind), title: Text('3')),
          BottomNavigationBarItem(
              icon: Icon(Icons.multiline_chart), title: Text('4')),
        ],
        unselectedItemColor: Colors.grey,
        selectedItemColor: Colors.black,
        showUnselectedLabels: true,
      ),
    );
  }
}

【讨论】:

    【解决方案2】:

    或者,如果您的目标是只放置一个borderRadius,您可以只使用ClipRRect 并将您想要的borderRadius 应用到它。 这是我的解决方案实现:

    
      ClipRRect _getBtmNavBar() {
        return ClipRRect(
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(25),
            topRight: Radius.circular(25),
          ),
          child: BottomNavigationBar(
            currentIndex: _selectedIndex,
            onTap: _onTabTapped,
            selectedLabelStyle: TextStyle(
              color: Colors.black87,
              fontSize: 16,
            ),
            iconSize: 35,
            selectedItemColor: Colors.white,
            unselectedItemColor: Colors.black54,
            items: <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                backgroundColor: kBottomNavBarBgColor,
                icon: Icon(Icons.home),
                title: Text('Home'),
              ),
    // more BottomNavigationBarItem() goes here.
    

    然后将它直接插入你的bottomNavigationBar

    代码示例:

    return Scaffold(
    // more Scaffold code goes here
    
    //bottom navigationBar
          bottomNavigationBar: _getBtmNavBar(),
    );
    

    【讨论】:

      【解决方案3】:

      您可以像下面这样用户。

      1. 我的底部导航栏图像看起来像

      2。这是代码

              import 'package:flutter/material.dart';
      
          class BottomTab extends StatefulWidget {
            @override
            State<StatefulWidget> createState() {
              return _BottomTab();
            }
          }
      
          class _BottomTab extends State<BottomTab> {
            int _selectedTabIndex = 0;
      
            List _pages = [
              Text("Home"),
              Text("Order"),
              Text("Notfication"),
              Text("More"),
            ];
      
            _changeIndex(int index) {
              setState(() {
                _selectedTabIndex = index;
                print("index..." + index.toString());
              });
            }
      
            @override
            Widget build(BuildContext context) {
              return Scaffold(
                appBar: AppBar(
                  title: Text('bottom nav bar'),
                ),
                body: Center(child: _pages[_selectedTabIndex]),
                bottomNavigationBar: bottomNavigationBar,
              );
            }
      
            Widget get bottomNavigationBar {
              return Container(
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.only(
                        topRight: Radius.circular(30), topLeft: Radius.circular(30)),
                    boxShadow: [
                      BoxShadow(color: Colors.black38, spreadRadius: 0, blurRadius: 10),
                    ],
                  ),
                  child: ClipRRect(
                    borderRadius: BorderRadius.only(
                      topLeft: Radius.circular(30.0),
                      topRight: Radius.circular(30.0),
                    ),
                    child: BottomNavigationBar(
                      currentIndex: _selectedTabIndex,
                      onTap: _changeIndex,
                      type: BottomNavigationBarType.fixed,
                      selectedFontSize: 12,
                      unselectedFontSize: 12,
                      selectedItemColor: Colors.amber[800],
                      unselectedItemColor: Colors.grey[500],
                      showUnselectedLabels: true,
                      items: <BottomNavigationBarItem>[
                        BottomNavigationBarItem(
                          icon: new Icon(Icons.home),
                          title: new Text('Home'),
                        ),
                        BottomNavigationBarItem(
                          icon: new Icon(Icons.shopping_cart_outlined),
                          title: new Text('Order'),
                        ),
                        BottomNavigationBarItem(
                          icon: new Icon(Icons.mail),
                          title: new Text('Messages'),
                        ),
                        BottomNavigationBarItem(
                            icon: Icon(Icons.more_horiz_rounded), title: Text('More')),
                      ],
                    ),
                  ));
            }
          }
      

      【讨论】:

        【解决方案4】:

        只需将 BottomNavigationBar 包含在 body 内,在圆形边框容器内。 Like this (See the picture attached!)

            Widget build(BuildContext context) {
            return MaterialApp(
                home: Scaffold(
              body: Container(
                  decoration: BoxDecoration(
                      image: DecorationImage(
                          image: new AssetImage("assets/images/background.jpg"),
                          fit: BoxFit.cover)),
                  child: Stack(
                    alignment: Alignment.bottomCenter,
                    children: <Widget>[
                      Expanded(
                        child: Column(
                          children: <Widget>[
                            Expanded(child: _children[_currentIndex]),
                          ],
                        ),
                      ),
                      Container(
                        decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.only(
                              topLeft: Radius.circular(20),
                              topRight: Radius.circular(20),
                              bottomLeft: Radius.circular(0),
                              bottomRight: Radius.circular(0)),
                          boxShadow: [
                            BoxShadow(
                                offset: Offset(0.0, 1.00), //(x,y)
                                blurRadius: 4.00,
                                color: Colors.grey,
                                spreadRadius: 1.00),
                          ],
                        ),
                        height: 70,
                        child: ClipRRect(
                            clipBehavior: Clip.hardEdge,
                            borderRadius: BorderRadius.only(
                                topLeft: Radius.circular(25),
                                topRight: Radius.circular(25),
                                bottomLeft: Radius.circular(0),
                                bottomRight: Radius.circular(0)),
                            child: Container(
                              child: BottomNavigationBar(
                                backgroundColor: Color.fromRGBO(255, 255, 255, 50),
                                showSelectedLabels: false,
                                showUnselectedLabels: false,
                                onTap: onTabTapped,
                                // new
                                currentIndex: _currentIndex,
                                // new
                                items: [
                                  new BottomNavigationBarItem(
                                    icon: Icon(
                                      Icons.phone,
                                      size: 30,
                                    ),
                                    title: Text('Calls'),
                                  ),
                                  new BottomNavigationBarItem(
                                    icon: Icon(
                                      Icons.mail,
                                      size: 30,
                                    ),
                                    title: Text('Messages'),
                                  ),
                                  new BottomNavigationBarItem(
                                      icon: Icon(
                                        Icons.person,
                                        size: 30,
                                      ),
                                      title: Text('Profile'))
                                ],
                              ),
                            )),
                      )
                    ],
                  )),
            ));
          }
        

        【讨论】:

        • 您实际上不必将底部导航栏单独包含在内,但在正文中也包含在圆形容器中。我已经做到了,检查所附照片
        猜你喜欢
        • 2016-10-24
        • 1970-01-01
        • 1970-01-01
        • 2020-11-06
        • 1970-01-01
        • 2021-11-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多