Flutter

Flutter BottomNavigationBar

import 'package:blog/Item0.dart';
import 'package:blog/Item1.dart';
import 'package:blog/Item2.dart';
import 'package:flutter/material.dart';

class BottomNavigationBarPage extends StatefulWidget {
  @override
  _BottomNavigationBarPageState createState() => _BottomNavigationBarPageState();
}

class _BottomNavigationBarPageState extends State<BottomNavigationBarPage> {

  int _index;

  List<Widget> _list = [
    Item0Page(),
    Item1Page(),
    Item2Page(),
  ];

  @override
  void initState() {
    super.initState();
    _index = 0;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _list[_index],
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _index,
        onTap: (index) {
          setState(() {
            _index = index;
            print("아이탬 : $_index");
          });
        },
        items: <BottomNavigationBarItem>[
          BottomNavigationBarItem(icon: Icon(Icons.home), title: Text("홈")),
          BottomNavigationBarItem(icon: Icon(Icons.local_post_office), title: Text("메세지")),
          BottomNavigationBarItem(icon: Icon(Icons.settings), title: Text("설정")),
        ],
      ),
    );
  }
}

Item0Page.dart

import 'package:flutter/material.dart';

class Item1Page extends StatefulWidget {
  @override
  _Item1PageState createState() => _Item1PageState();
}

class _Item1PageState extends State<Item1Page> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text(
          "0",
          style: TextStyle(fontSize: 30),
        ),
      ),
    );
  }
}

Item1Page.dart

import 'package:flutter/material.dart';

class Item1Page extends StatefulWidget {
  @override
  _Item1PageState createState() => _Item1PageState();
}

class _Item1PageState extends State<Item1Page> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text(
          "1",
          style: TextStyle(fontSize: 30),
        ),
      ),
    );
  }
}

Item2Page.dart

import 'package:flutter/material.dart';

class Item1Page extends StatefulWidget {
  @override
  _Item1PageState createState() => _Item1PageState();
}

class _Item1PageState extends State<Item1Page> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text(
          "2",
          style: TextStyle(fontSize: 30),
        ),
      ),
    );
  }
}

댓글 남기기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다