The*_*ior 11 flutter flutter-layout
我正在尝试创建一个个人资料页面,其中用户信息位于顶部。然后在该标签下有一个用于不同视图的选项卡视图。
这是我目前正在使用的代码,当我TabBarView拿出它时不会出现错误,并且如果将它们包装TabBarView进去,则会Expanded出现错误RenderFlex children have non-zero flex but incoming height constraints are unbounded.。
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(''),
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: EdgeInsets.all(10.0),
child: Row(
children: <Widget>[
CircleAvatar(
minRadius: 45.0,
backgroundImage: NetworkImage(
'https://www.ienglishstatus.com/wp-content/uploads/2018/04/Anonymous-Whatsapp-profile-picture.jpg'),
),
Padding(
padding: EdgeInsets.only(left: 10.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'Testing Name',
style: TextStyle(
fontSize: 22.0,
color: Colors.grey.shade800,
),
),
Text(
'@testing_username',
style: TextStyle(
fontSize: 13.0,
color: Colors.grey.shade800,
),
),
],
),
),
],
),
),
DefaultTabController(
length: 3,
child: Column(
children: <Widget>[
TabBar(
tabs: <Widget>[
Tab(
icon: Padding(
padding: EdgeInsets.all(6.0),
child: Image.asset(
"assets/images/icons/butterlike.png",
color: Colors.grey.shade800,
),
),
),
Tab(
icon: Padding(
padding: EdgeInsets.all(6.0),
child: Image.asset(
"assets/images/icons/butterlike.png",
color: Colors.grey.shade800,
),
),
),
Tab(
icon: Padding(
padding: EdgeInsets.all(6.0),
child: Image.asset(
"assets/images/icons/butterlike.png",
color: Colors.grey.shade800,
),
),
),
],
),
TabBarView(
children: <Widget>[
Container(
color: Colors.grey,
),
Container(
color: Colors.green,
),
Container(
color: Colors.purple,
),
],
),
],
),
)
],
),
);
}
Run Code Online (Sandbox Code Playgroud)
我也尝试的变化这个,但它不能去工作。
tem*_*bek 14
我通过添加TabBarinsideContainer和TabBarViewinside解决了它Expanded:
DefaultTabController(
length: 3,
child: Column(
children: <Widget>[
Container(child: TabBar(..)),
Expanded(child: TabBarView(..)),
],
),
);
Run Code Online (Sandbox Code Playgroud)
Sho*_*din 12
根据@Yamin 的回答,我使用了像下面这样的 SizeBox 来获取整页
SizedBox.expand(
child: TabBarView(),
)
Run Code Online (Sandbox Code Playgroud)
或任何其他尺寸:
SizedBox(
height: height:MediaQuery.of(context).size.height // or every other size ,
child: TabBarView(),
)
Run Code Online (Sandbox Code Playgroud)
Yam*_*min 10
错误描述很清楚,TabBarView没有限制的height。父窗口小部件也没有高度限制。因此,扩展小部件将无法解决此问题。
编辑:下面的解决方案是针对上面的问题(带有列)。通常情况下,请使用带有shrinkWrap: true。的ListView (或带有rinkWrap的任何其他小部件)
有一些选项:
第一个解决方案:
用高度限制的控件(如SizedBox或AspectRatio)包装父控件(列)。然后使用展开的小部件,如下所示:
child: SizedBox(
height: 300.0,
child: Column(
children: <Widget>[
.
.
.
Expanded(
child: TabBarView(
children: <Widget>[
Container(
height: 200.0,
color: Colors.grey,
),
Container(
height: 200.0,
color: Colors.green,
),
Container(
height: 200.0,
color: Colors.purple,
),
],
),
),
],
),
),
Run Code Online (Sandbox Code Playgroud)
第二种解决方案:
在TabBarView本身上使用有界小部件,例如SizedBox或AspectRatio:
SizedBox(
height: 300.0,
child: TabBarView(
children: <Widget>[
Container(
height: 200.0,
color: Colors.grey,
),
Container(
height: 200.0,
color: Colors.green,
),
Container(
height: 200.0,
color: Colors.purple,
),
],
),
),
Run Code Online (Sandbox Code Playgroud)
注意如果高度不是静态的,您还可以动态计算高度。
尝试使用IndexedStack而不是TabBarView
我试过Expanded,,shrinkWrap = true...但没有人工作得很好,只是试试例子。
例子:
class Product extends StatefulWidget {
@override
_ProductState createState() => _ProductState();
}
class _ProductState extends State<Product> with SingleTickerProviderStateMixin {
TabController tabController;
int selectedIndex = 0;
@override
void initState() {
super.initState();
tabController = TabController(length: 5, vsync: this, initialIndex: 0);
}
@override
void dispose() {
tabController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return DefaultTabController(
length: 3,
initialIndex: 0,
child: Scaffold(
body: ListView(
shrinkWrap: true,
children: [
TabBar(
tabs: <Widget>[
Tab(
text: 'one',
),
Tab(
text: 'two',
),
Tab(
text: 'three',
),
],
controller: tabController,
onTap: (index) {
setState(() {
selectedIndex = index;
tabController.animateTo(index);
});
},
),
IndexedStack(
children: <Widget>[
Visibility(
child: Text('test1'),
maintainState: true,
visible: selectedIndex == 0,
),
Visibility(
child: Text('test2'),
maintainState: true,
visible: selectedIndex == 1,
),
Visibility(
child: Text('test3'),
maintainState: true,
visible: selectedIndex == 2,
),
],
index: selectedIndex,
),
],
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
特别感谢@arbalest
| 归档时间: |
|
| 查看次数: |
8042 次 |
| 最近记录: |