好的,我的程序有问题,我知道为什么它无法按我想要的方式工作,但是我不知道如何解决它。
我正在使用以下代码在应用程序中切换标签。
child: CupertinoTabView(builder: (context) {
switch (i) {
case 0:
titleChanged = false;
firstPage = Page1();
return firstPage;
break;
case 1:
pageTitle = 'Page 2';
titleChanged = true;
secondPage = SecondPage();
return secondPage;
break;
default:
return Container();
}
}
现在在代码的前面(小部件树中的较高位置),我有整个页面的标题,我想在页面1和页面2之间来回切换。我可以更改文本,但我需要进行setState()以便使更改生效。
不幸的是,tabview“按钮”不能像普通按钮一样操作,因此,我无法将setState()函数应用于它们,否则它将在构建过程中调用setState。
也许有人告诉我要使用观察者,但是我在网上找不到的所有资源都无法真正指导我如何做到这一点。
我不确定观察者是正确的方法,但我还是处于末尾,因为我很陌生。有什么想法吗?
答案 0 :(得分:0)
如果选项卡具有子属性,请尝试使用GestureDetector()
答案 1 :(得分:0)
您可以在下面复制粘贴运行完整代码
您可以使用onTap
中的CupertinoTabBar
示例代码更新AppBar
title
代码snppet
tabBar: CupertinoTabBar(
items: [
...
],
onTap: (int index) {
switch (index) {
case 0:
_title = "1 first";
setState(() {});
break;
case 1:
_title = "2 second";
setState(() {});
break;
case 2:
_title = "3 third";
setState(() {});
break;
}
},
工作演示
完整代码
import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String _title = "1 first";
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(_title)),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(
child: CupertinoTabScaffold(
tabBar: CupertinoTabBar(
items: [
BottomNavigationBarItem(
title: Text("First"), icon: Icon(Icons.menu)),
BottomNavigationBarItem(
title: Text("Second"), icon: Icon(Icons.business)),
BottomNavigationBarItem(
title: Text("Third"), icon: Icon(Icons.account_box)),
],
onTap: (int index) {
switch (index) {
case 0:
_title = "1 first";
setState(() {});
break;
case 1:
_title = "2 second";
setState(() {});
break;
case 2:
_title = "3 third";
setState(() {});
break;
}
},
),
tabBuilder: (BuildContext context, int index) {
return CupertinoTabView(
builder: (context) {
switch (index) {
case 0:
return FirstPage();
break;
case 1:
return SecondPage();
break;
case 2:
return ThirdPage();
break;
}
},
);
},
),
),
],
),
),
);
}
}
class FirstPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(
middle: Text("First"),
),
child: Center(
child: CupertinoButton(
child: Text("First button"),
onPressed: () {},
),
),
);
}
}
class SecondPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(
middle: Text("Second"),
),
child: Center(
child: CupertinoButton(
child: Text("Second button"),
onPressed: () {},
),
),
);
}
}
class ThirdPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(
middle: Text("Thrid"),
),
child: Center(
child: CupertinoButton(
child: Text("Third button"),
onPressed: () {},
),
),
);
}
}