Recently I have been creating a layout which is a Dialog
that contains a TabBar
to hold the child tabs. The issue is that the height of this Dialog
takes the height of the screen instead of fitting the height of the child tabs inside it. I want the height of this Dialog
to be flexible instead of being fixed, and of course, it should adjust according to the height of the tabs inside it. If anyone has any solution, please let me know, thank you. Here’s my code:
class DialogAuthentication extends StatelessWidget {
const DialogAuthentication({super.key});
@override
Widget build(BuildContext context) {
return Dialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
),
child: Padding(
padding: const EdgeInsets.all(12),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Row(
children: [
Expanded(
child: SvgPicture.asset(AppIcons.moveLogo.svgAssetPath)),
GestureDetector(
onTap: () {
Navigator.pop(context);
},
child: SvgPicture.asset(
AppIcons.exit.svgAssetPath,
height: 16,
width: 16,
)),
],
),
const SizedBox(height: 22),
Flexible(
child: CustomTabBar(
tabTitles: [Constants.login, Constants.signUp],
widgets: [LoginBody(), SignUpBody()],
),
),
],
),
),
);
}
}
/// TabBar
class CustomTabBar extends StatefulWidget {
final List<String> tabTitles;
final List<Widget> widgets;
const CustomTabBar(
{super.key, required this.tabTitles, required this.widgets});
@override
State<CustomTabBar> createState() => _CustomTabBarState();
}
class _CustomTabBarState extends State<CustomTabBar>
with SingleTickerProviderStateMixin {
late TabController _tabController;
@override
void initState() {
super.initState();
int length = widget.tabTitles.length > widget.widgets.length
? widget.tabTitles.length
: widget.widgets.length;
_tabController = TabController(vsync: this, length: length);
}
@override
void dispose() {
_tabController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
List<String> adjustedTabTitles =
widget.tabTitles.length < widget.widgets.length
? widget.tabTitles +
List.generate(widget.widgets.length - widget.tabTitles.length,
(index) => "Tab")
: widget.tabTitles;
List<Widget> adjustedWidgets = widget.widgets.length <
widget.tabTitles.length
? widget.widgets +
List.generate(
widget.tabTitles.length - widget.widgets.length,
(index) => Center(
child:
Text(widget.tabTitles[widget.widgets.length + index])))
: widget.widgets;
return Container(
color: Colors.amber,
child: Column(
children: [
TabBar(
tabs: adjustedTabTitles.map((title) => Tab(text: title)).toList(),
controller: _tabController,
isScrollable: true,
labelStyle: AppTextStyles.montserratStyle.bold16tiffanyBlue,
unselectedLabelStyle: AppTextStyles.montserratStyle.regular16Black,
tabAlignment: TabAlignment.center,
indicator: const UnderlineTabIndicator(
borderSide: BorderSide(
width: 4.0,
color: AppColors.tiffanyBlue,
),
insets: EdgeInsets.symmetric(horizontal: 0),
),
indicatorSize: TabBarIndicatorSize.label,
dividerHeight: 1,
),
Flexible(child: TabBarView(controller: _tabController, children: adjustedWidgets))
],
),
);
}
}
// and the sign up body
class SignUpBody extends StatefulWidget {
const SignUpBody({super.key});
@override
State<SignUpBody> createState() => _SignUpBodyState();
}
class _SignUpBodyState extends State<SignUpBody> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
color: Colors.green,
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 16),
child: Column(
// mainAxisSize: MainAxisSize.min,
children: [
CustomButton(
iconPath: AppIcons.googleLogo.svgAssetPath,
title: Constants.signUpWithGoogle,
onTap: () {},
marginBottom: 8,
),
CustomButton(
iconPath: AppIcons.facebookLogo.svgAssetPath,
title: Constants.signUpWithFaceBook,
onTap: () {},
marginBottom: 21,
),
const CustomDividerWith(),
SizedBox(height: 15,),
GestureDetector(
onTap: () {},
child: Text(
Constants.signUpWithEmail,
style: AppTextStyles.montserratStyle.bold14tiffanyBlue,
),
),
const SizedBox(
height: 14,
),
Visibility(
visible: false,
child: Column(
children: [
const CustomEditText(
title: Constants.email,
),
const CustomEditText(
title: Constants.password,
isShowText: true,
maxLength: 8,
suffix: Icon(Icons.visibility),
),
const CustomEditText(
title: Constants.confirmPassword,
isShowText: true,
maxLength: 8,
suffix: Icon(Icons.visibility),
),
const CustomEditText(
title: Constants.referralCode,
textCapitalization: TextCapitalization.characters,
textInputType: TextInputType.text,
maxLength: 6,
marginBottom: 17,
),
RichText(
textAlign: TextAlign.justify,
text: TextSpan(
style: AppTextStyles.montserratStyle.regular14Black,
children: [
const TextSpan(text: Constants.byClickingSignUp),
TextSpan(
text: Constants.termsOfService,
style: AppTextStyles
.montserratStyle.regular14tiffanyBlue,
),
const TextSpan(
text: Constants.and,
),
TextSpan(
text: Constants.privacyNotice,
style: AppTextStyles
.montserratStyle.regular14tiffanyBlue,
),
const TextSpan(
text: '.',
),
],
),
),
const SizedBox(
height: 23,
),
CustomButton(
title: Constants.signUp,
textStyle: AppTextStyles.montserratStyle.bold16White,
backgroundColor: AppColors.spanishGray,
onTap: () {},
),
],
))
],
),
),
),
),
);
}
}