如何在 flutter 中滚动页面
How to scroll page in flutter
我的页面代码是这样的。我需要滚动应用栏下方的部分。
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(... ),
body: new Stack(
children: <Widget>[
new Container(
decoration: BoxDecoration(
image: DecorationImage(...),
new Column(children: [
new Container(...),
new Container(...... ),
new Padding(
child: SizedBox(
child: RaisedButton(..),
),
new Divider(),
new Column(
children: <Widget>[
new Container(
child: new Row(
children: <Widget>[
new Expanded(
child: new Text( ),
),
new IconButton(
icon: IconButton(..),
onPressed: () {
_changed(true, "type");
},
),
],
),
),
visibilityPropertyType
? new Container(
margin: EdgeInsets.all(24.0),
child: new Column(
children: <Widget>[
new Row(
children: <Widget>[
new Expanded(... ),
new RaisedButton(..)
],
),
new Padding(
padding: const EdgeInsets.only(top: 10.0),
child: new Row(
mainAxisAlignment:
MainAxisAlignment.spaceEvenly,
children: <Widget>[
new Column(
children: <Widget>[
new Row(
children: <Widget>[.. ]),
new Row(
children: <Widget>[]),
new Row(
children: <Widget>[]),
new Row(
children: <Widget>[],
),
new Column(
children: <Widget>[
new Row(
children: <Widget>[],
),
],
),
],
),
),
],
))
: new Container(),
],
),
new Divider(
color: Colors.white,
)
])
],
),
);
}
我需要使正文部分可滚动。我怎样才能实现那个滚动。
如果存在任何自定义滚动方法。 Scrollable
和 SingleChildScrollView
都试过了,但都不符合我的需求。
当我使用 SinglechildScrollView
时发生错误 BoxConstraints forces an infinite height。如果我删除了 LayoutBuilder,它会导致 RenderFlex overflowed by 22 pixels on the bottom
错误
你可以试试CustomScrollView
。将您的 CustomScrollView 放在 Column Widget 中。
举个例子-
class App extends StatelessWidget {
App({Key key}): super(key: key);
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: AppBar(
title: const Text('AppBar'),
),
body: new Container(
constraints: BoxConstraints.expand(),
decoration: new BoxDecoration(
image: new DecorationImage(
alignment: Alignment.topLeft,
image: new AssetImage('images/main-bg.png'),
fit: BoxFit.cover,
)
),
child: new Column(
children: <Widget>[
Expanded(
child: new CustomScrollView(
scrollDirection: Axis.vertical,
shrinkWrap: false,
slivers: <Widget>[
new SliverPadding(
padding: const EdgeInsets.symmetric(vertical: 0.0),
sliver: new SliverList(
delegate: new SliverChildBuilderDelegate(
(context, index) => new YourRowWidget(),
childCount: 5,
),
),
),
],
),
),
],
)),
);
}
}
在上面的代码中,我在 CustomScrollView 中显示了一个项目列表(总共 5 个)。
YourRowWidget
小部件作为列表项呈现 5 次。通常你应该根据一些数据渲染每一行。
您可以删除 Container 小部件的装饰 属性,它仅用于提供背景图像。
将您的小部件树包裹在 SingleChildScrollView
:
body: SingleChildScrollView(
child: Stack(
children: <Widget>[
new Container(
decoration: BoxDecoration(
image: DecorationImage(...),
new Column(children: [
new Container(...),
new Container(...... ),
new Padding(
child: SizedBox(
child: RaisedButton(..),
),
....
...
); // Single child scroll view
请记住,SingleChildScrollView
只能有一个直接小部件(就像 Android 中的 ScrollView
)。
根据您的建议,我得出了这样的解决方案:
new LayoutBuilder(
builder:
(BuildContext context, BoxConstraints viewportConstraints) {
return SingleChildScrollView(
child: ConstrainedBox(
constraints:
BoxConstraints(minHeight: viewportConstraints.maxHeight),
child: Column(children: [
// remaining stuffs
]),
),
);
},
)
在页面中添加Scroll的两种方式
1.使用 SingleChildScrollView :
SingleChildScrollView(
child: Column(
children: [
Container(....),
SizedBox(...),
Container(...),
Text(....)
],
),
),
2。使用ListView: ListView是默认提供的Scroll 不需要添加额外的widget用于滚动
ListView(
children: [
Container(..),
SizedBox(..),
Container(...),
Text(..)
],
),
看看这个,也许对你有帮助。
class ScrollView extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new LayoutBuilder(
builder:
(BuildContext context, BoxConstraints viewportConstraints) {
return SingleChildScrollView(
scrollDirection: Axis.vertical,
child: ConstrainedBox(
constraints: BoxConstraints(minHeight: viewportConstraints.maxHeight),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text("Hello world!!"),
//You can add another children
]),
),
);
},
);
}
}
您可以通过两种方式滚动内容的任何部分...
- 可以直接使用列表视图
- 或 SingleChildScrollView
大部分时间我直接使用列表视图,只要在那个特定屏幕上有键盘输入,这样内容就不会被键盘重叠,更多的内容会滚动到顶部....
这个技巧多次有用....
You can use this one and it's best practice.
SingleChildScrollView(
child: Column(
children: <Widget>[
//Your Widgets
//Your Widgets,
//Your Widgets
],
),
);
如果您已经在使用 statelessWidget,则非常简单 checkOut 我的代码
class _MyThirdPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Understanding Material-Cards'),
),
body: SingleChildScrollView(
child: Column(
children: <Widget>[
_buildStack(),
_buildCard(),
SingleCard(),
_inkwellCard()
],
)),
);
}
}
使用LayoutBuilder
并得到你想要的输出
用LayoutBuilder
包装SingleChildScrollView
并实现Builder函数。
我们可以使用 LayoutBuilder
来获得盒子 contains
或 space 可用的数量。
LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints){
return SingleChildScrollView(
child: Stack(
children: <Widget>[
Container(
height: constraints.maxHeight,
),
topTitle(context),
middleView(context),
bottomView(context),
],
),
);
}
)
我的页面代码是这样的。我需要滚动应用栏下方的部分。
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(... ),
body: new Stack(
children: <Widget>[
new Container(
decoration: BoxDecoration(
image: DecorationImage(...),
new Column(children: [
new Container(...),
new Container(...... ),
new Padding(
child: SizedBox(
child: RaisedButton(..),
),
new Divider(),
new Column(
children: <Widget>[
new Container(
child: new Row(
children: <Widget>[
new Expanded(
child: new Text( ),
),
new IconButton(
icon: IconButton(..),
onPressed: () {
_changed(true, "type");
},
),
],
),
),
visibilityPropertyType
? new Container(
margin: EdgeInsets.all(24.0),
child: new Column(
children: <Widget>[
new Row(
children: <Widget>[
new Expanded(... ),
new RaisedButton(..)
],
),
new Padding(
padding: const EdgeInsets.only(top: 10.0),
child: new Row(
mainAxisAlignment:
MainAxisAlignment.spaceEvenly,
children: <Widget>[
new Column(
children: <Widget>[
new Row(
children: <Widget>[.. ]),
new Row(
children: <Widget>[]),
new Row(
children: <Widget>[]),
new Row(
children: <Widget>[],
),
new Column(
children: <Widget>[
new Row(
children: <Widget>[],
),
],
),
],
),
),
],
))
: new Container(),
],
),
new Divider(
color: Colors.white,
)
])
],
),
);
}
我需要使正文部分可滚动。我怎样才能实现那个滚动。
如果存在任何自定义滚动方法。 Scrollable
和 SingleChildScrollView
都试过了,但都不符合我的需求。
当我使用 SinglechildScrollView
时发生错误 BoxConstraints forces an infinite height。如果我删除了 LayoutBuilder,它会导致 RenderFlex overflowed by 22 pixels on the bottom
错误
你可以试试CustomScrollView
。将您的 CustomScrollView 放在 Column Widget 中。
举个例子-
class App extends StatelessWidget {
App({Key key}): super(key: key);
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: AppBar(
title: const Text('AppBar'),
),
body: new Container(
constraints: BoxConstraints.expand(),
decoration: new BoxDecoration(
image: new DecorationImage(
alignment: Alignment.topLeft,
image: new AssetImage('images/main-bg.png'),
fit: BoxFit.cover,
)
),
child: new Column(
children: <Widget>[
Expanded(
child: new CustomScrollView(
scrollDirection: Axis.vertical,
shrinkWrap: false,
slivers: <Widget>[
new SliverPadding(
padding: const EdgeInsets.symmetric(vertical: 0.0),
sliver: new SliverList(
delegate: new SliverChildBuilderDelegate(
(context, index) => new YourRowWidget(),
childCount: 5,
),
),
),
],
),
),
],
)),
);
}
}
在上面的代码中,我在 CustomScrollView 中显示了一个项目列表(总共 5 个)。
YourRowWidget
小部件作为列表项呈现 5 次。通常你应该根据一些数据渲染每一行。
您可以删除 Container 小部件的装饰 属性,它仅用于提供背景图像。
将您的小部件树包裹在 SingleChildScrollView
:
body: SingleChildScrollView(
child: Stack(
children: <Widget>[
new Container(
decoration: BoxDecoration(
image: DecorationImage(...),
new Column(children: [
new Container(...),
new Container(...... ),
new Padding(
child: SizedBox(
child: RaisedButton(..),
),
....
...
); // Single child scroll view
请记住,SingleChildScrollView
只能有一个直接小部件(就像 Android 中的 ScrollView
)。
根据您的建议,我得出了这样的解决方案:
new LayoutBuilder(
builder:
(BuildContext context, BoxConstraints viewportConstraints) {
return SingleChildScrollView(
child: ConstrainedBox(
constraints:
BoxConstraints(minHeight: viewportConstraints.maxHeight),
child: Column(children: [
// remaining stuffs
]),
),
);
},
)
在页面中添加Scroll的两种方式
1.使用 SingleChildScrollView :
SingleChildScrollView(
child: Column(
children: [
Container(....),
SizedBox(...),
Container(...),
Text(....)
],
),
),
2。使用ListView: ListView是默认提供的Scroll 不需要添加额外的widget用于滚动
ListView(
children: [
Container(..),
SizedBox(..),
Container(...),
Text(..)
],
),
看看这个,也许对你有帮助。
class ScrollView extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new LayoutBuilder(
builder:
(BuildContext context, BoxConstraints viewportConstraints) {
return SingleChildScrollView(
scrollDirection: Axis.vertical,
child: ConstrainedBox(
constraints: BoxConstraints(minHeight: viewportConstraints.maxHeight),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text("Hello world!!"),
//You can add another children
]),
),
);
},
);
}
}
您可以通过两种方式滚动内容的任何部分...
- 可以直接使用列表视图
- 或 SingleChildScrollView
大部分时间我直接使用列表视图,只要在那个特定屏幕上有键盘输入,这样内容就不会被键盘重叠,更多的内容会滚动到顶部....
这个技巧多次有用....
You can use this one and it's best practice.
SingleChildScrollView( child: Column( children: <Widget>[ //Your Widgets //Your Widgets, //Your Widgets ], ), );
如果您已经在使用 statelessWidget,则非常简单 checkOut 我的代码
class _MyThirdPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Understanding Material-Cards'),
),
body: SingleChildScrollView(
child: Column(
children: <Widget>[
_buildStack(),
_buildCard(),
SingleCard(),
_inkwellCard()
],
)),
);
}
}
使用LayoutBuilder
并得到你想要的输出
用LayoutBuilder
包装SingleChildScrollView
并实现Builder函数。
我们可以使用 LayoutBuilder
来获得盒子 contains
或 space 可用的数量。
LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints){
return SingleChildScrollView(
child: Stack(
children: <Widget>[
Container(
height: constraints.maxHeight,
),
topTitle(context),
middleView(context),
bottomView(context),
],
),
);
}
)