Flutter - SingleChildScrollView 固定小部件
Flutter - SingleChildScrollView fixed widget
可以将 SingleChildScrollView 放入脚手架中,该脚手架在 SingleChildScrollView 之上有另一个不需要滚动的小部件。
例如我有一个SingleChildScrollView,我需要在指定位置添加一个固定按钮,但是这个按钮不会滚动。
我尝试使用 Stack 小部件,但是如果我在 SingleChildScrollView 之前添加一个按钮小部件,则无法单击,否则如果我在 SingleChildScrollView 之后添加按钮,则滚动触摸不起作用。
有人遇到问题吗?
现在我们了解了您想要实现的目标,可以使用 Stack 小部件、ListView 并使用 Align 小部件来定位包含您要用作标题的小部件的 Container:
Stack(
children: <Widget>[
ListView.builder(
itemCount: 20,
itemBuilder: (context, index) {
return Container(
child: Text('item $index'),
height: 40,
color: Colors.blueGrey,
margin: EdgeInsets.all(14),
);
}
),
Align(
alignment: Alignment.topCenter,
child: Container(
margin: EdgeInsets.only(top: 20),
alignment: Alignment.center,
height: 30,
width: 300,
color: Colors.blue,
child: Text('This is a title')
),
),
],
)
之前的回答才终于明白用户想要什么。
如果我正确理解了您的问题,并且您希望在视图中的顶部有一个固定的 Widget 并在其下方有一个可滚动项目列表,那么带有 SliverAppBar 和 SliverList 的 CustomScrollView 将是您的解决方案。请看下面的例子:
CustomScrollView(
slivers: <Widget>[
SliverAppBar(
pinned: true,
title: Center(child: Text('Title')),
),
SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
return Container(
height: 40,
margin: EdgeInsets.all(10),
alignment: Alignment.center,
color: Colors.teal[100 * (index % 9)],
child: Text('grid item $index'),
);
},
childCount: 20
),
),
],
)
如果想让固定的Widget在视图的底部,可以使用Scaffold的bottomNavigationBar
属性`:
Scaffold(
...
bottomNavigationBar: Container(
alignment: Alignment.center,
height: 50,
child: Text('title')
),
)
Joao 非常感谢你,我制作了一个应用程序屏幕来向你解释我的问题。
这是我的应用程序,如您所见,"My LOGO" 位于应用程序栏下方:
问题是我需要将我的徽标堆叠在文本下方 "This is my appbar"
Scaffold(
appBar: buildAppBar('Some cool appbar'),
body: Column(
children: [
Expanded(
child: SingleChildScrollView(
child: Column(
children: [
PackageCard(),
PackageCard(),
PackageCard(),
],
),
),
),
Container(
child: Text('Your super cool Footer'),
color: Colors.amber,
)
],
),
);
可以将 SingleChildScrollView 放入脚手架中,该脚手架在 SingleChildScrollView 之上有另一个不需要滚动的小部件。 例如我有一个SingleChildScrollView,我需要在指定位置添加一个固定按钮,但是这个按钮不会滚动。
我尝试使用 Stack 小部件,但是如果我在 SingleChildScrollView 之前添加一个按钮小部件,则无法单击,否则如果我在 SingleChildScrollView 之后添加按钮,则滚动触摸不起作用。
有人遇到问题吗?
现在我们了解了您想要实现的目标,可以使用 Stack 小部件、ListView 并使用 Align 小部件来定位包含您要用作标题的小部件的 Container:
Stack(
children: <Widget>[
ListView.builder(
itemCount: 20,
itemBuilder: (context, index) {
return Container(
child: Text('item $index'),
height: 40,
color: Colors.blueGrey,
margin: EdgeInsets.all(14),
);
}
),
Align(
alignment: Alignment.topCenter,
child: Container(
margin: EdgeInsets.only(top: 20),
alignment: Alignment.center,
height: 30,
width: 300,
color: Colors.blue,
child: Text('This is a title')
),
),
],
)
之前的回答才终于明白用户想要什么。
如果我正确理解了您的问题,并且您希望在视图中的顶部有一个固定的 Widget 并在其下方有一个可滚动项目列表,那么带有 SliverAppBar 和 SliverList 的 CustomScrollView 将是您的解决方案。请看下面的例子:
CustomScrollView(
slivers: <Widget>[
SliverAppBar(
pinned: true,
title: Center(child: Text('Title')),
),
SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
return Container(
height: 40,
margin: EdgeInsets.all(10),
alignment: Alignment.center,
color: Colors.teal[100 * (index % 9)],
child: Text('grid item $index'),
);
},
childCount: 20
),
),
],
)
如果想让固定的Widget在视图的底部,可以使用Scaffold的bottomNavigationBar
属性`:
Scaffold(
...
bottomNavigationBar: Container(
alignment: Alignment.center,
height: 50,
child: Text('title')
),
)
Joao 非常感谢你,我制作了一个应用程序屏幕来向你解释我的问题。
这是我的应用程序,如您所见,"My LOGO" 位于应用程序栏下方:
问题是我需要将我的徽标堆叠在文本下方 "This is my appbar"
Scaffold(
appBar: buildAppBar('Some cool appbar'),
body: Column(
children: [
Expanded(
child: SingleChildScrollView(
child: Column(
children: [
PackageCard(),
PackageCard(),
PackageCard(),
],
),
),
),
Container(
child: Text('Your super cool Footer'),
color: Colors.amber,
)
],
),
);