我是新手,所以我找不到这个代码中的问题。所有的事情都很好,但是我尝试使用两行的Grid列表,当我给列表的父容器赋予高度时,这些行工作得很好,但是我想按照条目包装高度。
void main() {
runApp(new MaterialApp(
home: new MyHome(),
));
}
class MyHome extends StatefulWidget {
@override
_AppState createState() => _AppState();
}
TextEditingController controller = new TextEditingController();
class _AppState extends State<MyHome> {
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
appBar: PreferredSize(
preferredSize: Size(null, 180),
child: CustomAppBar(_scaffoldKey, controller),
),
drawer: createDrawer(),
body: SingleChildScrollView(
child: Container(
color: Colors.black12,
//=========Main Container For Scrollview==============//
child: Padding(
padding: const EdgeInsets.fromLTRB(0, 15, 0, 0),
child: Column(
children: <Widget>[
Container(
//================Container for Categories==================//
color: Colors.white,
child: Padding(
padding: const EdgeInsets.fromLTRB(15, 10, 15, 10),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Column(
children: <Widget>[
CircleAvatar(
backgroundImage:
ExactAssetImage('images/user_icon.png'),
minRadius: 20,
maxRadius: 30,
),
Text(
'Women',
style: TextStyle(
fontSize: 13,
color: Colors.black,
fontFamily: 'SFProRegular'),
)
],
),
Column(
children: <Widget>[
CircleAvatar(
backgroundImage:
ExactAssetImage('images/user_icon.png'),
minRadius: 20,
maxRadius: 30,
),
Text(
'Women',
style: TextStyle(
fontSize: 13,
color: Colors.black,
fontFamily: 'SFProRegular'),
)
],
),
Column(
children: <Widget>[
CircleAvatar(
backgroundImage:
ExactAssetImage('images/user_icon.png'),
minRadius: 20,
maxRadius: 30,
),
Text(
'Women',
style: TextStyle(
fontSize: 13,
color: Colors.black,
fontFamily: 'SFProRegular'),
)
],
),
Column(
children: <Widget>[
CircleAvatar(
backgroundImage:
ExactAssetImage('images/user_icon.png'),
minRadius: 20,
maxRadius: 30,
),
Text(
'Women',
style: TextStyle(
fontSize: 13,
color: Colors.black,
fontFamily: 'SFProRegular'),
)
],
),
],
),
),
),
Card(
child: SizedBox(
height: 200.0,
child: Carousel(
images: [
NetworkImage(
'https://cdn-images-1.medium.com/max/2000/1*GqdzzfB_BHorv7V2NV7Jgg.jpeg'),
NetworkImage(
'https://cdn-images-1.medium.com/max/2000/1*wnIEgP1gNMrK5gZU7QS0-A.jpeg'),
],
dotSize: 4.0,
dotSpacing: 15.0,
indicatorBgPadding: 5.0,
borderRadius: false,
)),
),
//=//。
GridView.count(
childAspectRatio: 4.0,
// Create a grid with 2 columns. If you change the scrollDirection to
// horizontal, this produces 2 rows.
crossAxisCount: 2,
// Generate 100 widgets that display their index in the List.
children: List.generate(100, (index) {
return Center(
child: Text(
'Item $index',
style: Theme.of(context).textTheme.headline,
),
);
}),
)
],
),
),
),
),
);
}
}
因为你用SingleChildScrollView作为您的父部件GridView,所以您需要指定primary: false和shrinkWrap: true因此,GridView根据项目计数取最小的高度。
完整代码:
void main() {
runApp(new MaterialApp(
home: new MyHome(),
));
}
class MyHome extends StatefulWidget {
@override
_AppState createState() => _AppState();
}
TextEditingController controller = new TextEditingController();
class _AppState extends State<MyHome> {
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
appBar: PreferredSize(
preferredSize: Size(null, 180),
child: CustomAppBar(_scaffoldKey, controller),
),
drawer: createDrawer(),
body: SingleChildScrollView(
child: Container(
color: Colors.black12,
//=========Main Container For Scrollview==============//
child: Padding(
padding: const EdgeInsets.fromLTRB(0, 15, 0, 0),
child: Column(
children: <Widget>[
Container(
//================Container for Categories==================//
color: Colors.white,
child: Padding(
padding: const EdgeInsets.fromLTRB(15, 10, 15, 10),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Column(
children: <Widget>[
CircleAvatar(
backgroundImage:
ExactAssetImage('images/user_icon.png'),
minRadius: 20,
maxRadius: 30,
),
Text(
'Women',
style: TextStyle(
fontSize: 13,
color: Colors.black,
fontFamily: 'SFProRegular'),
)
],
),
Column(
children: <Widget>[
CircleAvatar(
backgroundImage:
ExactAssetImage('images/user_icon.png'),
minRadius: 20,
maxRadius: 30,
),
Text(
'Women',
style: TextStyle(
fontSize: 13,
color: Colors.black,
fontFamily: 'SFProRegular'),
)
],
),
Column(
children: <Widget>[
CircleAvatar(
backgroundImage:
ExactAssetImage('images/user_icon.png'),
minRadius: 20,
maxRadius: 30,
),
Text(
'Women',
style: TextStyle(
fontSize: 13,
color: Colors.black,
fontFamily: 'SFProRegular'),
)
],
),
Column(
children: <Widget>[
CircleAvatar(
backgroundImage:
ExactAssetImage('images/user_icon.png'),
minRadius: 20,
maxRadius: 30,
),
Text(
'Women',
style: TextStyle(
fontSize: 13,
color: Colors.black,
fontFamily: 'SFProRegular'),
)
],
),
],
),
),
),
Card(
child: SizedBox(
height: 200.0,
child: Carousel(
images: [
NetworkImage(
'https://cdn-images-1.medium.com/max/2000/1*GqdzzfB_BHorv7V2NV7Jgg.jpeg'),
NetworkImage(
'https://cdn-images-1.medium.com/max/2000/1*wnIEgP1gNMrK5gZU7QS0-A.jpeg'),
],
dotSize: 4.0,
dotSpacing: 15.0,
indicatorBgPadding: 5.0,
borderRadius: false,
)),
),
GridView.count(
shrinkWrap: true,
primary: false,
childAspectRatio: 4.0,
// Create a grid with 2 columns. If you change the scrollDirection to
// horizontal, this produces 2 rows.
crossAxisCount: 2,
// Generate 100 widgets that display their index in the List.
children: List.generate(100, (index) {
return Center(
child: Text(
'Item $index',
style: Theme.of(context).textTheme.headline,
),
);
}),
)
],
),
),
),
),
);
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。