Flutter 簡單完成咸魚APP底部tab樣式

先來看一下咸魚APP底部tab欄


咸魚APP樣式.jpg

這次實現(xiàn)功能,主要是修改flutter中的FloatingActionButton的位置和形狀
修改發(fā)布按鈕的位置

floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,

下面是全部代碼

class Tabs extends StatefulWidget {
  int index = 0;
  Tabs({this.index=0});
  @override
  _TabsState createState() => _TabsState(currentIndex: index);
}

class _TabsState extends State<Tabs> {
  int currentIndex = 0;
  var currentPage;
  List pages = [HomePage(), CategoryPage(), SearchPage(), ShopPage(), ProfilePage()];
  _TabsState({this.currentIndex=0});
  @override
  void initState() {
    super.initState();
    currentPage = pages[currentIndex];
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: getBottomNavigationBar(currentIndex),
      //設(shè)置發(fā)布按鈕
      floatingActionButton: getFloatingActionButton(),
      //修改發(fā)布按鈕位置
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      body: pages[currentIndex],
    );
  }

  Widget getFloatingActionButton() {
    return Container(
      height: 70,
      width: 70,
      padding: EdgeInsets.all(8),
      margin: EdgeInsets.only(bottom: 8),
      decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(35),
          color: Colors.white
      ),
      child: FloatingActionButton(
        child: Icon(Icons.add, size: 35, color: Colors.black87),
        backgroundColor: Colors.yellow,
        onPressed: () {
          if (currentIndex != 2) {
            setState(() {
              currentIndex = 2;
            });
          }
        },
      ),
    );
}
//tab欄目信息
  Widget getBottomNavigationBar(index) {
    return BottomNavigationBar(
      type: BottomNavigationBarType.fixed,
      selectedFontSize: 14,
      unselectedFontSize: 14,
      selectedItemColor: Colors.yellow,
      selectedIconTheme: IconThemeData(
        color: Colors.yellow
      ),
      currentIndex: currentIndex,
      onTap: (index){
        if (currentIndex != index) {
          setState(() {
            currentIndex = index;
          });
        }
      },
      items: <BottomNavigationBarItem>[
        BottomNavigationBarItem(
          icon: Icon(Icons.home),
          title: Text("首頁"),
        ),
        BottomNavigationBarItem(
            icon: Icon(Icons.category),
            title: Text("分類")
        ),
        BottomNavigationBarItem(
            icon: Icon(Icons.add),
            title: Text("發(fā)布")
        ),
        BottomNavigationBarItem(
            icon: Icon(Icons.message),
            title: Text("消息")
        ),
        BottomNavigationBarItem(
            icon: Icon(Icons.settings),
            title: Text("我的")
        )
      ],
    );
  }
}
以上代碼完成效果
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容