gpt4 book ai didi

flutter - 局部屏幕不在 Flutter 中滚动

转载 作者:IT王子 更新时间:2023-10-29 06:58:52 25 4
gpt4 key购买 nike

我正在尝试创建一个屏幕,其中通过 API 响应填充 ListView,但我遇到的问题是屏幕仅在滚动指向 ListView 上方时垂直滚动,但屏幕在滚动时不滚动指向 ListView 上的任何其他点。

    import 'package:flutter/material.dart';
import 'package:walk_himalaya/pages/Test/TestPlaceAttr.dart';
import 'package:walk_himalaya/utils/UserConstants.dart';
import 'package:walk_himalaya/utils/himayan_walk_constant.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
void main() {
runApp(new MaterialApp(
home: TravelHomePage(),
));
}

class TravelHomePage extends StatefulWidget {
TravelHomePageState createState() => new TravelHomePageState();
}

class TravelHomePageState extends State<TravelHomePage> {
String access_token = UserConstants.userAccessToken;
String token_type = UserConstants.bearerType;

Future<List<dynamic>> getCategories() async {
http.Response response =
await http.get(HimalayanWalkConstant.baseURL + "places", headers: {
'Accept': 'application/json',
'Content-type': 'application/json',
'Authorization': '$token_type' + ' $access_token'
});
Map<String, dynamic> decodedCategories = json.decode(response.body);
return decodedCategories['data'];
}

@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: new AppBar(
title: new Text("Destination Page"),
),
body: ListView(
scrollDirection: Axis.vertical,
children: <Widget>[
Container(
padding: EdgeInsets.all(16.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(UserConstants.userDisplayName,
style: TextStyle(
fontSize: 18.0, fontWeight: FontWeight.bold)),
Text(
"Where do you want to go?",
style: TextStyle(color: Colors.grey.shade700),
)
],
),
CircleAvatar(
backgroundImage:
new NetworkImage(UserConstants.userProfileUrl),
radius: 40,
)
],
),
),
Container(
padding: EdgeInsets.only(left: 16.0, right: 16.0, bottom: 8.0),
child: Material(
elevation: 5.0,
child: TextField(
decoration: InputDecoration(
hintText: "Find destination",
prefixIcon: Icon(Icons.location_on),
border: InputBorder.none),
),
),
),
Container(
child: FutureBuilder(
future: getCategories(),
builder: (BuildContext context,
AsyncSnapshot<List<dynamic>> snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return ListView.builder(
shrinkWrap: true,
scrollDirection: Axis.vertical,
itemBuilder: (context, index) {
Map<String, String> data =
snapshot.data[index].cast<String, String>();
return SingleChildScrollView(
child: new SingleChildScrollView(
child: GestureDetector(
onTap: () => _openDestinationPage(context),
child: _buildFeaturedItem(
image: data['featured_image_url'],
title: data['title'],
subtitle: data['slug'],
),
),
),
);
},
itemCount: snapshot.data.length,
);
} else {
return Center(
child: CircularProgressIndicator(),
);
}
},
),
)
],
),
);
}
}

Container _buildFeaturedItem({String image, String title, String subtitle}) {
return Container(
padding: EdgeInsets.only(left: 16.0, top: 8.0, right: 16.0, bottom: 16.0),
child: Material(
elevation: 5.0,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(5.0)),
child: Stack(
children: <Widget>[
ClipRRect(
borderRadius: BorderRadius.circular(5.0),
child: Image.network(
image,
fit: BoxFit.cover,
)),
Positioned(
right: 10.0,
top: 10.0,
child: IconButton(
onPressed: () {},
icon: Icon(Icons.favorite_border, color: Colors.white),
),
),
Positioned(
bottom: 20.0,
child: Container(
padding: EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
color: Colors.black.withOpacity(0.7),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Text(title,
style: TextStyle(
color: Colors.white,
fontSize: 20.0,
fontWeight: FontWeight.bold)),
Text(subtitle, style: TextStyle(color: Colors.white))
],
),
),
),
],
),
),
);
}

_openDestinationPage(BuildContext context) {
Navigator.push(context, MaterialPageRoute(builder: (_) =>
DestinationPage()));
}

The screen is scrolling when it is scrolled by pointing on the red surface only.

总结我要做的是垂直滚动整个屏幕。

最佳答案

这是优化后的代码,请改用它。

import 'package:flutter/material.dart';
import 'package:walk_himalaya/pages/Test/TestPlaceAttr.dart';
import 'package:walk_himalaya/utils/UserConstants.dart';
import 'package:walk_himalaya/utils/himayan_walk_constant.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:cached_network_image/cached_network_image.dart';

void main() {
runApp(new MaterialApp(
home: TravelHomePage(),
));
}

class TravelHomePage extends StatefulWidget {
TravelHomePageState createState() => new TravelHomePageState();
}

class TravelHomePageState extends State<TravelHomePage> {
String access_token = UserConstants.userAccessToken;
String token_type = UserConstants.bearerType;

Future<List<dynamic>> getCategories() async {
http.Response response =
await http.get(HimalayanWalkConstant.baseURL + "places", headers: {
'Accept': 'application/json',
'Content-type': 'application/json',
'Authorization': '$token_type' + ' $access_token'
});
Map<String, dynamic> decodedCategories = json.decode(response.body);
return decodedCategories['data'];
}

@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: new AppBar(
title: new Text("Destination Page"),
),
body: ListView(
scrollDirection: Axis.vertical,
children: <Widget>[
Container(
padding: EdgeInsets.all(16.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(UserConstants.userDisplayName,
style: TextStyle(
fontSize: 18.0, fontWeight: FontWeight.bold)),
Text(
"Where do you want to go?",
style: TextStyle(color: Colors.grey.shade700),
)
],
),
CircleAvatar(
backgroundImage:
new NetworkImage(UserConstants.userProfileUrl),
radius: 40,
)
],
),
),
Container(
padding: EdgeInsets.only(left: 16.0, right: 16.0, bottom: 8.0),
child: Material(
elevation: 5.0,
child: TextField(
decoration: InputDecoration(
hintText: "Find destination",
prefixIcon: Icon(Icons.location_on),
border: InputBorder.none),
),
),
),
new Container(
padding: EdgeInsets.fromLTRB(8.0, 8.0, 8.0, 8.0),
child: Material(
child: buildListItems(getCategories()),
),
),
],
),
);
}
}

Card _buildFeaturedItem({String image, String title, String subtitle}) {
return Card(
child: Material(
elevation: 5.0,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(5.0)),
child: Stack(
children: <Widget>[
ClipRRect(
borderRadius: BorderRadius.circular(5.0),
child: new Image(
image: new CachedNetworkImageProvider(image),
fit: BoxFit.cover,
)),
Positioned(
right: 10.0,
top: 10.0,
child: IconButton(
onPressed: () {},
icon: Icon(Icons.favorite_border, color: Colors.white),
),
),
Positioned(
bottom: 20.0,
child: Container(
padding: EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
color: Colors.black.withOpacity(0.7),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Text(title,
style: TextStyle(
color: Colors.white,
fontSize: 20.0,
fontWeight: FontWeight.bold)),
Text(subtitle, style: TextStyle(color: Colors.white))
],
),
),
),
],
),
),
);
}

SizedBox buildListItems(Future<List> categories) {
final ScrollController controller = new ScrollController();
return SizedBox(
child: FutureBuilder(
future: categories,
builder: (BuildContext context, AsyncSnapshot<List<dynamic>> snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return ListView.builder(
shrinkWrap: true,
controller: controller,
scrollDirection: Axis.vertical,
itemBuilder: (context, index) {
Map<String, String> data =
snapshot.data[index].cast<String, String>();
return new Container(
child: InkWell(
onTap: () => _openDestinationPage(context),
child: _buildFeaturedItem(
image: data['featured_image_url'],
title: data['title'],
subtitle: data['slug'],
),
),
);
},
itemCount: snapshot.data.length,
);
} else {
return Center(
child: CircularProgressIndicator(),
);
}
},
),
);
}

_openDestinationPage(BuildContext context) {
Navigator.push(context, MaterialPageRoute(builder: (_) => DestinationPage()));
}

关于flutter - 局部屏幕不在 Flutter 中滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54283364/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com