実装イメージ
実装方法
タブの小画面を準備する
真ん中に文字があるだけの画面です。
これを2つ(FirstPage、SecondPage)作成します。
FirstPage.dart
import 'package:flutter/material.dart';
class FirstPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.orange,
body: Center(
child: Text(
"First Page",
style: TextStyle(
fontSize: 20
),
),
),
);
}
}
タブ画面を作成します。
BottomTabPage.dart
import 'package:flutter/material.dart';
import 'package:flutter_app/FirstPage.dart';
import 'package:flutter_app/SecondPage.dart';
class BottomTabPage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _BottomTabPageState();
}
}
class _BottomTabPageState extends State<BottomTabPage> {
int _currentIndex = 0;
final _pageWidgets = [
FirstPage(),
SecondPage()
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('BottomNavigationBar'),
),
body: _pageWidgets.elementAt(_currentIndex),
bottomNavigationBar: BottomNavigationBar(
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(icon: Icon(Icons.home), title: Text('First')),
BottomNavigationBarItem(icon: Icon(Icons.photo_album), title: Text('Second')),
],
currentIndex: _currentIndex,
fixedColor: Colors.blueAccent,
onTap: _onItemTapped,
type: BottomNavigationBarType.fixed,
),
);
}
void _onItemTapped(int index) => setState(() => _currentIndex = index );
}
Main.dartに設定
最後にMain.dart
に設定して完了です。
Main.dart
import 'package:flutter/material.dart';
import 'package:flutter_app/BottomTabPage.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: BottomTabPage(),
);
}
}
参考