LoginSignup
0
0

More than 1 year has passed since last update.

[Flutter] BottomNavigationBarのボタンをオリジナルにする方法

Posted at

こんにちわ。いせきです〜〜〜
久しぶりのFlutterの投稿です!!

今回は、BottomNavigationBarの押すところを色々な形に変更できるとわかったので、共有しようと思います〜〜。

コード紹介

*補足*
写真や背景の調整のため、iconのサイズやcolorをいじっています。


BottomNavigationBarItem(icon: Icon(Icons.add,color: Colors.black,),label: ''),
BottomNavigationBarItem(icon: Text('タブ!?'), label: ''),
BottomNavigationBarItem(icon: Image.network('https://cdn-images-1.medium.com/max/1200/1*ilC2Aqp5sZd1wi0CopD1Hw.png',width: 40,height: 40,),label: ''),

写真

IMG_2003.JPG

解説

BottomNavigationBarItemiconの引数は、iconDataかと思っていましたが、Widgetだったので、Widgetなら基本的に使うことができます。

ソースコード

import 'package:flutter/material.dart';

class HomeScreen extends StatefulWidget {
  const HomeScreen({Key? key}) : super(key: key);

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

class _HomeScreenState extends State<HomeScreen> {
  int _selectedIndex = 0;

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: IndexedStack(
        index: _selectedIndex,
        children: [
          Container(
            alignment: Alignment.center,
            child: const Text('1ページ'),
          ),
          Container(
            alignment: Alignment.center,
            child: const Text('2ページ'),
          ),
          Container(
            alignment: Alignment.center,
            child: const Text('3ページ'),
          ),
        ],
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: [
          BottomNavigationBarItem(icon: const Icon(Icons.add,color: Colors.black,),label: ''),
          BottomNavigationBarItem(icon: const Text('タブ!?'), label: ''),
          BottomNavigationBarItem(icon: Image.network('https://cdn-images-1.medium.com/max/1200/1*ilC2Aqp5sZd1wi0CopD1Hw.png',width: 40,height: 40,),label: ''),
        ],
        currentIndex: _selectedIndex,
        onTap: _onItemTapped,
      ),
    );
  }
}


最後に、

便利だね。。てっきりそれ用のDart Packageを探そうと思っていました。ありがたや

参考文献

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0