gpt4 book ai didi

user-interface - 如何自定义标签栏? flutter

转载 作者:IT王子 更新时间:2023-10-29 06:57:24 25 4
gpt4 key购买 nike

我想自定义我的 TabBar

现在我有这个 TabBar(stock UI):

stock TabBar

我想要这个用户界面:

desired TabBar

我已经编写了个性化标签,但我不知道如何实现它。

有关于我的 HomePage 的当前代码:

class HomePage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return new HomePageState();
}
}

class HomePageState extends State<HomePage>
with SingleTickerProviderStateMixin {
TabController _tabController;

@override
void initState() {
super.initState();
_tabController =
TabController(length: 6, vsync: this); // initialise it here
}

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
bottom: TabBar(
controller: _tabController,
isScrollable: true,
tabs: [
Tab(text: "NewPay1.1"),
Tab(text: "NewMall 1.0"),
Tab(text: "海报"),
Tab(text: "企业版"),
Tab(text: "个版"),
Tab(text: "poa"),
],
),
title: Text('tabBar'),
),
body: TabBarView(
controller: _tabController,
children: [
// these are your pages
TaskListPage(),
TestPage(),
],
),
bottomNavigationBar: BottomAppBar(
color: Colors.white,
shape: CircularNotchedRectangle(),
child: Row(
children: <Widget>[
IconButton(
onPressed: () => _tabController.animateTo(0),
icon: Icon(Icons.home),
),
SizedBox(),
IconButton(
onPressed: () => _tabController.animateTo(1),
icon: Icon(Icons.more))
],
mainAxisAlignment: MainAxisAlignment.spaceAround,
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
return TestPage().createState();
},
child: Icon(Icons.add),
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
),
);
}
}

然后是我的 CustomTab 类:

class CustomTab extends StatefulWidget {

final Function(int) tabSelected;
final List<String> items;

const CustomTab({Key key, this.tabSelected, this.items}) : super(key: key);

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

class _CustomTabState extends State<CustomTab> {
var categorySelected = 0;

@override
Widget build(BuildContext context) {
return _getListCategory();
}

Widget _getListCategory(){

ListView listCategory = new ListView.builder(
itemCount: widget.items.length,
scrollDirection: Axis.horizontal,
itemBuilder: (context, index){
return _buildCategoryItem(index);
}
);

return new Container(
height: 50.0,
child: listCategory,
color: Colors.grey[200].withAlpha(200),
);

}

Widget _buildCategoryItem(index){

return new InkWell(
onTap: (){
setSelectedItem(index);
print("click");
},
child: new Row(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
new Container(
margin: new EdgeInsets.only(left: 10.0),
child: new Material(
elevation: 2.0,
color: categorySelected == index ? Colors.black : Colors.grey,
borderRadius: const BorderRadius.all(const Radius.circular(25.0)),
child: new Container(
padding: new EdgeInsets.only(left: 12.0,top: 7.0,bottom: 7.0,right: 12.0),
child: new Text(widget.items[index],
style: new TextStyle(
color: categorySelected == index ? Colors.white : Colors.black),
),
),
),
)
],
),
);

}

void setSelectedItem(index) {

if(index != categorySelected) {
widget.tabSelected(index);
setState(() {
categorySelected = index;
});
}
}
}

最佳答案

这个样式的标签栏已经有一个插件。

https://pub.dartlang.org/packages/bubble_tab_indicator

我希望这就是您要找的:)

你的 Glup3

关于user-interface - 如何自定义标签栏? flutter ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55682466/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com