如何解决我们可以用抽屉按钮控制颤动标签的位置吗?
我想创建一个UI,在该UI中,人们可以通过单击选项卡或按抽屉上的按钮来切换选项卡。有点像2个控件可以做一件事情。
这是可能的还是我问的是错误的问题?
解决方法
这不是一个非常错误的问题。对的,这是可能的。您可以为两个控件定义相同的方法。
编辑
这是完整的代码示例:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
final appTitle = 'Drawer Demo';
@override
Widget build(BuildContext context) {
return MaterialApp(
title: appTitle,home: MyHomePage(title: appTitle),);
}
}
class MyHomePage extends StatefulWidget {
final String title;
MyHomePage({Key key,this.title}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage>
with SingleTickerProviderStateMixin {
TabController _tabController;
final List<Tab> myTabs = <Tab>[
new Tab(text: 'LEFT'),new Tab(text: 'RIGHT'),];
@override
void initState() {
super.initState();
_tabController = new TabController(vsync: this,length: myTabs.length);
}
@override
void dispose() {
_tabController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return DefaultTabController(
length: 2,child: Scaffold(
appBar: AppBar(
bottom: TabBar(
onTap: (index) {
print("index");
print(index);
},controller: _tabController,tabs: myTabs,),title: Text('Tabs Demo'),body: TabBarView(
controller: _tabController,children: myTabs.map((Tab tab) {
return Center(child: Text(tab.text));
}).toList(),drawer: Drawer(
// Add a ListView to the drawer. This ensures the user can scroll
// through the options in the drawer if there isn't enough vertical
// space to fit everything.
child: ListView(
// Important: Remove any padding from the ListView.
padding: EdgeInsets.zero,children: <Widget>[
DrawerHeader(
child: Text('Drawer Header'),decoration: BoxDecoration(
color: Colors.blue,ListTile(
title: Text('LEFT'),onTap: () {
// Update the state of the app
// ...
// Then close the drawer
setState(() {
_tabController.index = 0;
});
Navigator.pop(context);
},ListTile(
title: Text('RIGHT'),onTap: () {
// Update the state of the app
// ...
// Then close the drawer
setState(() {
_tabController.index = 1;
});
Navigator.pop(context);
},],);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。