我希望 bottomSheet 的高度与其包含的元素的高度相同,如下图所示。 我使用了一个普通的 bottomSheet,在它里面有一个包含不同项目的 pageView
import 'package:flutter/material.dart';
class Test extends StatelessWidget {
@override
Widget build(BuildContext context) {
final PageController controller = PageController(initialPage: 0);
return Scaffold(
body: Container(
padding: EdgeInsets.symmetric(horizontal: 20),
child: Center(
child: ElevatedButton(
child: Text('BottomSheet'),
onPressed: () {
showModalBottomSheet(
isScrollControlled: true,
context: context,
backgroundColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadiusDirectional.only(
topEnd: Radius.circular(25),
topStart: Radius.circular(25),
),
),
builder: (context) => Container(
padding: EdgeInsetsDirectional.only(
start: 20,
end: 20,
bottom: 30,
top: 8,
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(
height: 600,
child: PageView(
controller: controller,
children: <Widget>[
SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: List.generate(
5,
(index) => Container(
height: 60,
margin:
EdgeInsetsDirectional.only(bottom: 10),
width: double.infinity,
color: Colors.yellow,
child: Center(
child: Text(
'Widgets $index',
style: TextStyle(fontSize: 20),
),
),
),
),
),
),
SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: List.generate(
10, (index) => Container(
height: 60,
margin:
EdgeInsetsDirectional.only(bottom: 10),
width: double.infinity,
color: Colors.yellow,
child: Center(
child: Text(
'Widgets $index',
style: TextStyle(fontSize: 20),
),
),
),),
),
),
SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: List.generate(
12, (index) => Container(
height: 60,
margin:
EdgeInsetsDirectional.only(bottom: 10),
width: double.infinity,
color: Colors.yellow,
child: Center(
child: Text(
'Widgets $index',
style: TextStyle(fontSize: 20),
),
),
),),
),
),
],
),
),
],
),
),
);
},
),
),
),
);
}
}
尝试使用
wrap
并且不需要使用固定高度
import 'package:flutter/material.dart';
class Test extends StatelessWidget {
@override
Widget build(BuildContext context) {
final PageController controller = PageController(initialPage: 0);
return Scaffold(
body: Container(
padding: EdgeInsets.symmetric(horizontal: 20),
child: Center(
child: ElevatedButton(
child: Text('BottomSheet'),
onPressed: () {
showModalBottomSheet(
isScrollControlled: true,
context: context,
backgroundColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadiusDirectional.only(
topEnd: Radius.circular(25),
topStart: Radius.circular(25),
),
),
builder: (context) => SingleChildScrollView(
padding: EdgeInsetsDirectional.only(
start: 20,
end: 20,
bottom: 30,
top: 8,
),
child: Wrap(
children: [
Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: List.generate(
10,
(index) => Container(
height: 60,
margin: EdgeInsetsDirectional.only(bottom: 10),
width: double.infinity,
color: Colors.yellow,
child: Center(
child: Text(
'Widgets $index',
style: TextStyle(fontSize: 20),
),
),
),
),
),
Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: List.generate(
10,
(index) => Container(
height: 60,
margin: EdgeInsetsDirectional.only(bottom: 10),
width: double.infinity,
color: Colors.yellow,
child: Center(
child: Text(
'Widgets $index',
style: TextStyle(fontSize: 20),
),
),
),
),
),
Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: List.generate(
12,
(index) => Container(
height: 60,
margin: EdgeInsetsDirectional.only(bottom: 10),
width: double.infinity,
color: Colors.yellow,
child: Center(
child: Text(
'Widgets $index',
style: TextStyle(fontSize: 20),
),
),
),
),
),
],
),
),
);
},
),
),
),
);
}
}