0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Flutter BottomNavigationBarItem の必須属性 label

Last updated at Posted at 2023-10-10

BottomNaviationBarを必要最低限書いて実行したところ。実行時エラーとなった。コンパイルエラーにはなっていなかった。

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Home(),
    );
  }
}

class Home extends StatelessWidget {
  const Home({super.key});
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Home'),),
      bottomNavigationBar: BottomNavigationBar(
        items: const [
          BottomNavigationBarItem(icon: Icon(Icons.home),),
          BottomNavigationBarItem(icon: Icon(Icons.article),),
          BottomNavigationBarItem(icon: Icon(Icons.accessibility),),
        ],
      ),
    );
  }
}

原因は、BottomNavigationBarItemの属性であるlabelが未設定(null)となったことで実行時エラー。コンパイル時にチェックはできないものなのだろうか。Flutterの仕様を理解すれば当たり前のことなのか、まだこれから学んで行きたいと思う。以下のように、適当なlabelを追記する。
スクリーンショット 2023-10-10 23.21.58.png

BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home',),
BottomNavigationBarItem(icon: Icon(Icons.article), label: 'Article',),
BottomNavigationBarItem(icon: Icon(Icons.accessibility), label: 'Settings',),

今度は期待の通りアイコンが下に表示された。もちろん、onTapを実装していないので、アイコンを押しても画面の切り替わりなどは一切ない。
スクリーンショット 2023-10-10 23.28.49.png

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?