我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'),
)
],
),
],
),
);
}
}
主要飞镖:
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),
...,
],
));
}
}