• 1000 bar.
У меня вопрос, можно ли переключиться с дочерней страницы Tab1 на дочернюю страницу Tab2, и если да, то каким образом. На этом изображении вы можете увидеть мое дерево виджетов: Дерево виджетов
Класс MyApp.dart
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
TabItem _currentTab = TabItem.tab1;
final Map<TabItem, GlobalKey<NavigatorState>> navigatorKeys = {
TabItem.tab1: GlobalKey<NavigatorState>(),
TabItem.tab1: GlobalKey<NavigatorState>(),
};
Map<TabItem, WidgetBuilder> get widgetBuilders {
return {
TabItem.tab1: (_) => Tab1(),
TabItem.tab2: Tab2(),
};
}
void _select(TabItem tabItem) {
if (tabItem == _currentTab) {
navigatorKeys[tabItem].currentState.popUntil((route) => route.isFirst);
} else {
setState(() => _currentTab = tabItem);
}
}
@override
Widget build(BuildContext context) {
return CupertinoScaffold(
currentTab: _currentTab,
onSelectTab: _select,
widgetBuilders: widgetBuilders,
navigatorKeys: navigatorKeys,
);
}
}
Класс CupertinoScaffold.dart (содержит CupertinoTabView)
class CupertinoScaffold extends StatelessWidget {
const CupertinoScaffold({
Key key,
@required this.currentTab,
@required this.onSelectTab,
@required this.widgetBuilders,
@required this.navigatorKeys,
}) : super(key: key);
final TabItem currentTab;
final ValueChanged<TabItem> onSelectTab;
final Map<TabItem, WidgetBuilder> widgetBuilders;
final Map<TabItem, GlobalKey<NavigatorState>> navigatorKeys;
@override
Widget build(BuildContext context) {
return CupertinoTabScaffold(
tabBar: CupertinoTabBar(
key: Key(Keys.tabBar),
items: [
BottomNavigationBarItem(title: Text('Tab1')),
BottomNavigationBarItem(title: Text('Tab2'))
],
onTap: (index) => onSelectTab(TabItem.values[index]),
),
tabBuilder: (context, index) {
final item = TabItem.values[index];
return CupertinoTabView(
navigatorKey: navigatorKeys[item],
builder: (context) => widgetBuilders[item](context),
onGenerateRoute: CupertinoTabViewRouter.generateRoute,
);
},
);
}
}
Класс Tab1.dart
class Tab1 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Tab1')),
body: Center(
child: Button(
child: Text(
'Open child tab1'
),
onPressed: () => Navigator.of(context).pushNamed('/tab1-child'),
),
),
);
}
}
Класс CupertinoTabViewRouter.dart
class CupertinoTabViewRouter {
static Route generateRoute(RouteSettings settings) {
switch (settings.name) {
case '/tab1-child':
return CupertinoPageRoute<dynamic>(
builder: (_) => Tab1Child(),
settings: settings,
fullscreenDialog: true,
);
case '/tab2-child':
return CupertinoPageRoute<dynamic>(
builder: (_) => Tab2Child(),
settings: settings,
fullscreenDialog: true,
);
}
return null;
}
}
Класс Tab1Child.dart
class Tab1Child extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('ChildTab1')),
body: Center(
child: Button(
child: Text(
'Open child tab2'
),
onPressed: () => //TODO!,
),
),
);
}
}