gpt4 book ai didi

flutter - BottomNavigationBar 内的顶部 TabBar

转载 作者:IT王子 更新时间:2023-10-29 06:55:48 24 4
gpt4 key购买 nike

我无法让顶部 TabBar 在我的 BottomNavigationBar 中呈现。我只希望顶部 TabBar 显示在首页 BottomNavigationBarItem 内。我知道我可以在主页中设置另一个脚手架,但我显然不希望脚手架内有一个脚手架。使用以下代码,我收到运行时错误:

Another exception was thrown: RenderBox was not laid out: RenderViewport#d0e83 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE

home.dart:

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class Home extends StatelessWidget {
static const routeName = 'home';

@override
Widget build(BuildContext context) {
return DefaultTabController(
length: 2,
child: Column(
children: <Widget>[
TabBar(
tabs: <Widget>[
Tab(
text: '1',
icon: Icon(Icons.add_a_photo),
),
Tab(
text: '2',
icon: Icon(Icons.adb),
),
],
),
TabBarView(
children: <Widget>[
Center(
child: Text('1'),
),
Center(
child: Text('2'),
)
],
),
],
),
);
}
}

主 Dart :

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: ...,
theme: ThemeData(primaryColor: Colors.red, accentColor: Colors.white),
// home: Categories(),
initialRoute: '/',
routes: {
'/': (ctx) => MyHomePage(0),
...,
},
// onGenerateRoute: (settings){
//incase you're creating routes on the fly that arent named routes/generating route names during the app lifecycle
// print(settings.arguments)
// if(settings.name=='mealdetail'){
// return ... materialpageroute...
// }
// else
// return MaterialPageRoute(builder: (context) => Categories());
// },
onUnknownRoute: (settings) {
//useful for catching routes as a last resort/couldnt find the page etc.../ 404 fallback page like web
return MaterialPageRoute(builder: (context) => Home());
},
);
}
}

class MyHomePage extends StatefulWidget {
final int selectedScreenIndex;

MyHomePage(this.selectedScreenIndex);

@override
_MyHomePageState createState() => _MyHomePageState(selectedScreenIndex);
}

class _MyHomePageState extends State<MyHomePage> {
final List<Map<String, Object>> screens = [
{'title': 'Home', 'screen': Home()},
{...},
];

_MyHomePageState(this.selectedScreenIndex);

int selectedScreenIndex = 0;

void selectScreen(int index) {
setState(() {
selectedScreenIndex = index;
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
// resizeToAvoidBottomInset: true,

appBar: AppBar(
title: Text(screens[selectedScreenIndex]['title']),
),
drawer: MainDrawer(),
body: screens[selectedScreenIndex]['screen'],
bottomNavigationBar: BottomNavigationBar(
onTap: selectScreen,
backgroundColor: Theme.of(context).primaryColor,
unselectedItemColor: Colors.white,
selectedItemColor: Theme.of(context).accentColor,
currentIndex: selectedScreenIndex,
type: BottomNavigationBarType.fixed,
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text('Home'),
backgroundColor: Theme.of(context).primaryColor),
...,
],
));
}
}

最佳答案

最简单的方法 (不是最好的) 是在应用栏参数中使用内联条件,如果所选索引为 = 0,则返回带标签栏的应用栏,否则返回普通应用栏。

示例

 appBar:selectedSreenIndex==0? AppBar(title:Text(screens[selectedScreenIndex['title'], 
bottom: TabBar())
: AppBar(
title: Text(screens[selectedScreenIndex]['title']),
),

关于flutter - BottomNavigationBar 内的顶部 TabBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57542767/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com