Flutter之Scaffold、TabBar、底部导航
2020-12-03 本文已影响0人
yanshihao
Screenshot_1606717458.png
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(debugShowCheckedModeBanner: false, home: YshScaffold());
}
}
class YshScaffold extends StatefulWidget {
@override
_YshScaffoldState createState() => _YshScaffoldState();
}
class _YshScaffoldState extends State<YshScaffold>
with SingleTickerProviderStateMixin {
TabController _tabController; //需要定义一个Controller
List tabs = ["新闻", "历史", "图片"];
int _selectedIndex = 1;
PageController _pageController = PageController();
@override
void initState() {
super.initState();
_tabController = TabController(length: tabs.length, vsync: this);
_tabController.addListener(() {
setState(() {
var index = _tabController.index;
_selectedIndex = index + 1;
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
actions: [
Container(
margin: EdgeInsets.only(right: 8),
child: Icon(
Icons.share,
color: Colors.white,
),
)
],
title: Text("App Name"),
/* bottom: TabBar(
//生成Tab菜单
controller: _tabController,
tabs: tabs.map((e) => Tab(text: e)).toList())*/
),
floatingActionButton:
FloatingActionButton(child: Icon(Icons.add), onPressed: () {}),
drawer: MyDrawer(),
body: PageView(
physics: NeverScrollableScrollPhysics(), //禁止滑动
controller: _pageController,
onPageChanged: (index) {
setState(() {
_selectedIndex = index;
});
},
children: tabs.map((e) {
return Center(
child: Text(
e,
textScaleFactor: 5,
));
}).toList(),
),
bottomNavigationBar: BottomNavigationBar(
items: [
BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),
BottomNavigationBarItem(
icon: Icon(Icons.business), label: 'Business'),
BottomNavigationBarItem(icon: Icon(Icons.school), label: 'School'),
],
currentIndex: _selectedIndex,
onTap: (index) {
setState(() {
_selectedIndex = index;
_pageController.jumpToPage(index);
});
},
));
}
}
class MyDrawer extends StatelessWidget {
const MyDrawer({
Key key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Drawer(
child: MediaQuery.removePadding(
context: context,
//移除抽屉菜单顶部默认留白
removeTop: true,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 38.0),
child: Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: ClipOval(
child: Image.asset(
"assets/images/test.png",
width: 80,
),
),
),
Text(
"Wendux",
style: TextStyle(fontWeight: FontWeight.bold),
)
],
),
),
Expanded(
child: ListView(
children: <Widget>[
ListTile(
leading: const Icon(Icons.add),
title: const Text('Add account'),
),
ListTile(
leading: const Icon(Icons.settings),
title: const Text('Manage accounts'),
),
],
),
),
],
),
),
);
}
}