TabBar 的索引在隐藏时发生变化
TabBar's index changes when it hides
我正在构建一个脚手架,将 TabBar 作为 BottomNavigationBar,将 TabView 作为主体。问题是,当我打开键盘以某种原因在表单中输入数据时,TabBar 会更改索引(即使 TabView 内部没有)。为什么会这样?我该如何证明?
class _SignState extends State<Sign> with TickerProviderStateMixin {
static Container navBar;
static TabBarView pages;
@override
Widget build(BuildContext context) {
final tabController = TabController(length: 2, vsync: this);
print(tabController.index);
void _goLogin() {
// tabController.animateTo(tabController.previousIndex);
}
pages = TabBarView(controller: tabController, children: [
LoginPage(
keyRoot: widget.keyRoot,
),
RegisterPage(
onRegistered: _goLogin,
),
]);
navBar = Container(
height: 50,
alignment: Alignment.center,
padding: EdgeInsets.only(left: 50, right: 50),
child: Stack(
alignment: Alignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(10.0),
child: Container(
decoration: BoxDecoration(
color: Colors.transparent,
borderRadius: BorderRadius.circular(30),
border: Border.all(
width: 0.3,
color: Colors.black.withOpacity(0.5),
style: BorderStyle.solid))),
),
TabBar(
controller: tabController,
labelPadding: EdgeInsets.all(10),
labelColor: Colors.white,
unselectedLabelColor: Theme.of(context).primaryColor,
indicator: BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.black,
blurRadius: 1,
)
],
borderRadius: BorderRadius.circular(30),
color: Theme.of(context).primaryColor,
),
tabs: [Text('Accedi'), Text('Registrati')]),
],
),
);
// Main page
return Scaffold(
body: Column(
children: <Widget>[
_returnTop(),
Expanded(
child: pages,
),
],
),
persistentFooterButtons: _returnButtons(),
bottomNavigationBar: Padding(
padding: const EdgeInsets.only(bottom: 15),
child: navBar,
),
);
}
Widget _returnTop() {
return Stack(children: <Widget>[
Center(child: Image.asset('assets/logo_sign.png')),
Positioned(
bottom: 0,
right: 20,
child: InkWell(
splashColor: Colors.transparent,
child: Text('salta >'),
onTap: () {
Navigator.pushNamed(context, "/home", arguments: widget.keyRoot);
},
),
)
]);
}
List<Widget> _returnButtons() {
return [
Text('oppure con'),
IconButton(
//TODO: Aggiungere funzioni
color: Theme.of(context).accentColor,
icon: Icon(FontAwesomeIcons.googlePlusG),
tooltip: 'Login con Google',
onPressed: () {}),
IconButton(
//TODO: Aggiungere funzioni
color: Theme.of(context).accentColor,
icon: Icon(FontAwesomeIcons.facebook),
tooltip: 'Login con Facebook',
onPressed: () {}),
IconButton(
//TODO: Aggiungere funzioni
color: Theme.of(context).accentColor,
icon: Icon(FontAwesomeIcons.twitter),
tooltip: 'Login con Twitter',
onPressed: () {}),
];
}
}
修改:这是我的全部代码:)
问题是您在构建方法中添加了 TabController 初始值设定项。然后发生的事情是当键盘出现时,构建方法是 运行 并且选项卡控制器再次被初始化。正确的做法是在initState方法中初始化TabController。
@override
void initState() {
super.initState();
_tabController = TabController(vsync: this, length: choices.length);
}
我正在构建一个脚手架,将 TabBar 作为 BottomNavigationBar,将 TabView 作为主体。问题是,当我打开键盘以某种原因在表单中输入数据时,TabBar 会更改索引(即使 TabView 内部没有)。为什么会这样?我该如何证明?
class _SignState extends State<Sign> with TickerProviderStateMixin {
static Container navBar;
static TabBarView pages;
@override
Widget build(BuildContext context) {
final tabController = TabController(length: 2, vsync: this);
print(tabController.index);
void _goLogin() {
// tabController.animateTo(tabController.previousIndex);
}
pages = TabBarView(controller: tabController, children: [
LoginPage(
keyRoot: widget.keyRoot,
),
RegisterPage(
onRegistered: _goLogin,
),
]);
navBar = Container(
height: 50,
alignment: Alignment.center,
padding: EdgeInsets.only(left: 50, right: 50),
child: Stack(
alignment: Alignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(10.0),
child: Container(
decoration: BoxDecoration(
color: Colors.transparent,
borderRadius: BorderRadius.circular(30),
border: Border.all(
width: 0.3,
color: Colors.black.withOpacity(0.5),
style: BorderStyle.solid))),
),
TabBar(
controller: tabController,
labelPadding: EdgeInsets.all(10),
labelColor: Colors.white,
unselectedLabelColor: Theme.of(context).primaryColor,
indicator: BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.black,
blurRadius: 1,
)
],
borderRadius: BorderRadius.circular(30),
color: Theme.of(context).primaryColor,
),
tabs: [Text('Accedi'), Text('Registrati')]),
],
),
);
// Main page
return Scaffold(
body: Column(
children: <Widget>[
_returnTop(),
Expanded(
child: pages,
),
],
),
persistentFooterButtons: _returnButtons(),
bottomNavigationBar: Padding(
padding: const EdgeInsets.only(bottom: 15),
child: navBar,
),
);
}
Widget _returnTop() {
return Stack(children: <Widget>[
Center(child: Image.asset('assets/logo_sign.png')),
Positioned(
bottom: 0,
right: 20,
child: InkWell(
splashColor: Colors.transparent,
child: Text('salta >'),
onTap: () {
Navigator.pushNamed(context, "/home", arguments: widget.keyRoot);
},
),
)
]);
}
List<Widget> _returnButtons() {
return [
Text('oppure con'),
IconButton(
//TODO: Aggiungere funzioni
color: Theme.of(context).accentColor,
icon: Icon(FontAwesomeIcons.googlePlusG),
tooltip: 'Login con Google',
onPressed: () {}),
IconButton(
//TODO: Aggiungere funzioni
color: Theme.of(context).accentColor,
icon: Icon(FontAwesomeIcons.facebook),
tooltip: 'Login con Facebook',
onPressed: () {}),
IconButton(
//TODO: Aggiungere funzioni
color: Theme.of(context).accentColor,
icon: Icon(FontAwesomeIcons.twitter),
tooltip: 'Login con Twitter',
onPressed: () {}),
];
}
}
修改:这是我的全部代码:)
问题是您在构建方法中添加了 TabController 初始值设定项。然后发生的事情是当键盘出现时,构建方法是 运行 并且选项卡控制器再次被初始化。正确的做法是在initState方法中初始化TabController。
@override
void initState() {
super.initState();
_tabController = TabController(vsync: this, length: choices.length);
}