您可以在下面复制粘贴运行完整代码
您可以使用onTap 的CupertinoTabBar
示例代码更新AppBartitle
代码片段
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: () {},
),
),
);
}
}