[英]Make the showModalBottomSheet() height adjust according to content
[英]TapBarView adjust height according to content in it
我試圖在我創建的第一個選項卡的列表視圖中顯示項目。 但是,當標簽不可見時,除非我用固定尺寸的容器包裝TabBarview。 在不為父容器提供固定高度的情況下,我應該怎么做才能使這些項目可見?
我希望紅色容器根據其中的內容是動態的。
https://zapp.run/edit/zg5w062ig5x0?theme=dark&lazy=false
除非鏈接斷開。
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Tab inside body widget',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.green,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Tabs Example'),
),
body: Container(
child: Column(crossAxisAlignment: CrossAxisAlignment.stretch, children: <Widget>[
SizedBox(height: 20.0),
Text('Tabs Inside Body', textAlign: TextAlign.center, style: TextStyle(fontSize: 22)),
DefaultTabController(
length: 4, // length of tabs
initialIndex: 0,
child: Column(crossAxisAlignment: CrossAxisAlignment.stretch, children: <Widget>[
Container(
child: TabBar(
labelColor: Colors.green,
unselectedLabelColor: Colors.black,
tabs: [
Tab(text: 'Tab 1'),
Tab(text: 'Tab 2'),
Tab(text: 'Tab 3'),
Tab(text: 'Tab 4'),
],
),
),
Container(
height: 400, //height of TabBarView
decoration: BoxDecoration(
color: Colors.red,
border: Border(top: BorderSide(color: Colors.grey, width: 0.5))
),
child: TabBarView(children: <Widget>[
Container(
child: Center(
child: Text('Display Tab 1', style: TextStyle(fontSize: 22, fontWeight: FontWeight.bold)),
),
),
Container(
child: Center(
child: Text('Display Tab 2', style: TextStyle(fontSize: 22, fontWeight: FontWeight.bold)),
),
),
Container(
child: Center(
child: Text('Display Tab 3', style: TextStyle(fontSize: 22, fontWeight: FontWeight.bold)),
),
),
Container(
child: Center(
child: Text('Display Tab 4', style: TextStyle(fontSize: 22, fontWeight: FontWeight.bold)),
),
),
])
)
])
),
]),
),
);
}
}
每當使用Column
、 Row
或任何其他具有無限高度或寬度的父 Widget 的 Widget 時,請根據您的要求使用Flex
或Expanded
Widget
示例代碼:-
Expanded(child: TabBarView(children: [],))
完整代碼:-
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Tab inside body widget',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.green,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Tabs Example'),
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
const SizedBox(height: 20.0),
const Text('Tabs Inside Body',
textAlign: TextAlign.center, style: TextStyle(fontSize: 22)),
DefaultTabController(
length: 4, // length of tabs
initialIndex: 0,
child: Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
const TabBar(
labelColor: Colors.green,
unselectedLabelColor: Colors.black,
tabs: [
Tab(text: 'Tab 1'),
Tab(text: 'Tab 2'),
Tab(text: 'Tab 3'),
Tab(text: 'Tab 4'),
],
),
Expanded(
child: Container(
decoration: const BoxDecoration(
color: Colors.red,
border: Border(
top: BorderSide(
color: Colors.grey, width: 0.5))),
child: const TabBarView(children: <Widget>[
Center(
child: Text('Display Tab 1',
style: TextStyle(
fontSize: 22,
fontWeight: FontWeight.bold)),
),
Center(
child: Text('Display Tab 2',
style: TextStyle(
fontSize: 22,
fontWeight: FontWeight.bold)),
),
Center(
child: Text('Display Tab 3',
style: TextStyle(
fontSize: 22,
fontWeight: FontWeight.bold)),
),
Center(
child: Text('Display Tab 4',
style: TextStyle(
fontSize: 22,
fontWeight: FontWeight.bold)),
),
])),
)
]),
)),
]),
);
}
}
Output:-
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.