Flutter 同时使用FutureBuilder/ListView.builder/ListView/Container时出现抖动/省道错误
当我尝试运行下面的代码时,它显示了很多错误。我用一根绳子固定了一根。 守则:Flutter 同时使用FutureBuilder/ListView.builder/ListView/Container时出现抖动/省道错误,flutter,listview,dart,containers,flutter-futurebuilder,Flutter,Listview,Dart,Containers,Flutter Futurebuilder,当我尝试运行下面的代码时,它显示了很多错误。我用一根绳子固定了一根。 守则: void main() { runApp(Page1()); } class Page1 extends StatelessWidget { //********************************** GOOGLE MAPS ***************************************** @override Widget build(BuildContext co
void main() {
runApp(Page1());
}
class Page1 extends StatelessWidget {
//********************************** GOOGLE MAPS *****************************************
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: MapView(),
);
}
}
class MapView extends StatefulWidget {
@override
_MapViewState createState() => _MapViewState();
}
class _MapViewState extends State<MapView> {
CameraPosition _initialLocation = CameraPosition(target: LatLng(0.0, 0.0));
GoogleMapController mapController;
final Geolocator _geolocator = Geolocator();
Position _currentPosition;
@override
void initState() {
super.initState();
_getCurrentLocation();
}
_getCurrentLocation() async {
await _geolocator
.getCurrentPosition(desiredAccuracy: LocationAccuracy.high)
.then((Position position) async {
setState(() {
// Store the position in the variable
_currentPosition = position;
print('CURRENT POS: $_currentPosition');
// For moving the camera to current location
mapController.animateCamera(
CameraUpdate.newCameraPosition(
CameraPosition(
target: LatLng(position.latitude, position.longitude),
zoom: 13.0,
),
),
);
});
}).catchError((e) {
print(e);
});
}
@override
Widget build(BuildContext context) {
// Determining the screen width & height
var height = MediaQuery.of(context).size.height;
var width = MediaQuery.of(context).size.width;
//********************************** GOOGLE MAPS SCREEN **********************************
return Container(
height: height,
width: width,
child: Scaffold(
body: Stack(
children: <Widget>[
GoogleMap(
initialCameraPosition: _initialLocation,
myLocationEnabled: true,
myLocationButtonEnabled: false,
mapType: MapType.normal,
zoomGesturesEnabled: true,
zoomControlsEnabled: false,
onMapCreated: (GoogleMapController controller) {
mapController = controller;
},
),
SafeArea(child:
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Container(
child: Material(
shape:StadiumBorder(),
color: Color(0xffeb5c68),
child: InkWell(
splashColor: Color(0xffda1b2b) ,
child: SizedBox(
width: 130,
height: 47,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("MY ORDERS", style: TextStyle(fontWeight: FontWeight.bold)),
SizedBox(width: 3),
Icon(Icons.arrow_forward),
]
),
),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Page2()),
);
},
),
),
)
]
),
),
SafeArea(child:
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
ClipOval(
child: Material(
color: Color(0xffeb5c68),
child: InkWell(
splashColor: Color(0xffda1b2b),
child: SizedBox(
width: 56,
height: 56,
child: Icon(Icons.my_location, size: 28,),
),
onTap: () {
mapController.animateCamera(
CameraUpdate.newCameraPosition(
CameraPosition(
target: LatLng(
_currentPosition.latitude,
_currentPosition.longitude,
),
zoom: 13.0,
),
),
);
},
),
),
),
]
),
),
//********************************** ORDERS **********************************
Container(
//padding: EdgeInsets.only(top: 550, bottom: 50),
height: 500,
child: FutureBuilder(
future: getTechies(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
print(snapshot.data);
if (snapshot.data == null) {
return Container(
child: Center(
child: Text("Loading...", style: TextStyle(fontSize: 30),)
)
);
} else {
return ListView.builder(
//padding: EdgeInsets.only(top: 550, bottom: 500),
scrollDirection: Axis.horizontal,
shrinkWrap: true,
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index){
return
ListView(
padding: EdgeInsets.only(left: 20),
children: <Widget>[
InkWell( // on tap...
child:
Container(
padding: EdgeInsets.all(10),
margin: EdgeInsets.only(right: 20),
width: 180,
height:200,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20)),
color: Colors.white,
boxShadow: [
BoxShadow(
color: Colors.grey,
blurRadius: 0.5,
),],
),
child:
Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
SizedBox(height: 5,),
Text("AS: " + snapshot.data[index].id.toString(), style: TextStyle(fontSize: 15)),
SizedBox(height: 10,),
Text("AC: " + snapshot.data[index].userId.toString(), style: TextStyle(fontSize: 15)),
SizedBox(height: 30,),
],
),
GestureDetector( //on Long Press...
child:
Container(
alignment: Alignment.bottomCenter,
width: 120.0,
height: 40.0,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(10)),
color: Color(0xffeb5c68),
),
child:
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("REQUEST", textAlign: TextAlign.center,style: TextStyle(color: Colors.white),),
]
)
),
onLongPress: (){
},
)
]
)
),
onTap: () {
}
)
],
);
}
);
}
}
)
),
],
)
),
);
}
Future<List<Technician>> getTechies() async {
try {
var data = await http.get("https://jsonplaceholder.typicode.com/posts");
var jsonData = json.decode(data.body);
List<Technician> techies = [];
for (var u in jsonData) {
Technician myTechy = Technician(u["userId"].toString(), u["id"].toString(), u["tittle"], u["body"]);
techies.add(myTechy);
}
return techies;
} catch(e) {
print(e);
}
}
}
void main(){
runApp(第1页());
}
类Page1扩展了无状态小部件{
//**********************************谷歌地图*****************************************
@凌驾
小部件构建(构建上下文){
返回材料PP(
debugShowCheckedModeBanner:false,
主页:MapView(),
);
}
}
类MapView扩展了StatefulWidget{
@凌驾
_MapViewState createState()=>\u MapViewState();
}
类_MapViewState扩展状态{
CameraPosition _initialLocation=CameraPosition(目标:LatLng(0.0,0.0));
谷歌地图控制器;
最终地理定位器_geologitor=geologitor();
位置_当前位置;
@凌驾
void initState(){
super.initState();
_getCurrentLocation();
}
_getCurrentLocation()异步{
等待地理定位器
.getCurrentPosition(所需精度:定位精度高)
.然后((位置)异步{
设置状态(){
//将位置存储在变量中
_当前位置=位置;
打印(“当前位置:$\u当前位置”);
//用于将相机移动到当前位置
mapController.animateCamera(
CameraUpdate.newCameraPosition(
摄像定位(
目标:LatLng(位置.纬度,位置.经度),
缩放:13.0,
),
),
);
});
}).catchError((e){
印刷品(e);
});
}
@凌驾
小部件构建(构建上下文){
//确定屏幕宽度和高度
var height=MediaQuery.of(context).size.height;
var width=MediaQuery.of(context).size.width;
//**********************************谷歌地图屏幕**********************************
返回容器(
高度:高度,,
宽度:宽度,
孩子:脚手架(
主体:堆栈(
儿童:[
谷歌地图(
initialCameraPosition:\u initialLocation,
myLocationEnabled:对,
myLocationButtonEnabled:false,
mapType:mapType.normal,
zoomGesturesEnabled:对,
ZoomControl启用:false,
onMapCreated:(谷歌地图控制器){
mapController=控制器;
},
),
安全区(儿童:
划船(
mainAxisAlignment:mainAxisAlignment.end,
儿童:[
容器(
儿童:材料(
形状:StadiumBorder(),
颜色:颜色(0xffeb5c68),
孩子:InkWell(
飞溅颜色:颜色(0xffda1b2b),
孩子:大小盒子(
宽度:130,
身高:47,
孩子:排(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
文本(“我的订单”,样式:TextStyle(fontWeight:fontWeight.bold)),
尺寸箱(宽度:3),
图标(图标。向前箭头),
]
),
),
onTap:(){
导航器。推(
上下文
MaterialPage路由(生成器:(上下文)=>Page2()),
);
},
),
),
)
]
),
),
安全区(儿童:
划船(
mainAxisAlignment:mainAxisAlignment.start,
儿童:[
斜坡(
儿童:材料(
颜色:颜色(0xffeb5c68),
孩子:InkWell(
飞溅颜色:颜色(0xffda1b2b),
孩子:大小盒子(
宽度:56,
身高:56,
子:图标(Icons.my_位置,大小:28,),
),
onTap:(){
mapController.animateCamera(
CameraUpdate.newCameraPosition(
摄像定位(
目标:拉丁美洲(
_当前位置。纬度,
_当前位置。经度,
),
缩放:13.0,
),
),
);
},
),
),
),
]
),
),
//**********************************命令**********************************
容器(
//填充:仅限边缘设置(顶部:550,底部:50),
身高:500,
孩子:未来建设者(
future:getTechies(),
生成器:(BuildContext上下文,异步快照){
打印(快照数据);
如果(snapshot.data==null){
返回容器(
儿童:中心(
子项:文本(“加载…”,样式:TextStyle(fontSize:30),)
)
);
}否则{
返回ListView.builder(
//填充:仅限边缘设置(顶部:550,底部:500),
滚动方向:轴水平,
收缩膜:对,
itemCount:snapshot.data.length,