Dart FixedExtentScroll物理颤振
我想在我的代码中将get-response JSON数据作为Dart FixedExtentScroll物理颤振,dart,flutter,Dart,Flutter,我想在我的代码中将get-response JSON数据作为FixedExtentScrollPhysicsViewList 无论我尝试什么,总会出现一些错误 我实际上做的是 我有一个FixedExtentScroll物理代码和一个get响应代码,我混合了它们 但出现了错误 我的密码是 import 'package:flutter/material.dart'; import 'dart:async'; import 'dart:convert'; import 'package:http/h
FixedExtentScrollPhysics
ViewList
无论我尝试什么,总会出现一些错误
我实际上做的是
我有一个FixedExtentScroll物理代码和一个get响应代码,我混合了它们
但出现了错误
我的密码是
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
MyAppState createState() => MyAppState();
}
class MyAppState extends State<MyApp> {
final FixedExtentScrollController fixedExtentScrollController =
new FixedExtentScrollController();
List data;
final String url = "https://jsonplaceholder.typicode.com/todos";
Future<String> getTitle() async {
var res = await http
.get(Uri.encodeFull(url), headers: {"Accept": "application/json"});
setState(() {
var resBody = json.decode(res.body);
data = resBody;
});
return "Success!";
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
backgroundColor: Colors.deepPurple,
title: const Text('List'),
),
body:new Expanded(
flex: 1,
child:ListView.builder(
controller: fixedExtentScrollController,
physics: FixedExtentScrollPhysics(),
itemCount: data == null ? 0 : data.length,
itemBuilder: (BuildContext context, int index) {
return new Container(
child: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Card(
child: Container(
padding: EdgeInsets.all(15.0),
child: Row(
children: <Widget>[
Text("Title: "),
Text(data[index]["title"],
style: TextStyle(
fontSize: 11.0, color: Colors.black87)),
],
)),
),
],
),
),
);
},
),
),
),
);
}
@override
void initState() {
super.initState();
this.getTitle();
}
}
导入“包装:颤振/材料.省道”;
导入“dart:async”;
导入“dart:convert”;
将“package:http/http.dart”导入为http;
void main()=>runApp(MyApp());
类MyApp扩展了StatefulWidget{
@凌驾
MyAppState createState()=>MyAppState();
}
类MyAppState扩展了状态{
最终固定扩展滚动控制器固定扩展滚动控制器=
新的FixedExtentScrollController();
列出数据;
最终字符串url=”https://jsonplaceholder.typicode.com/todos";
Future getTitle()异步{
var res=等待http
.get(Uri.encodeFull(url),头:{“接受”:“应用程序/json”});
设置状态(){
var resBody=json.decode(res.body);
数据=resBody;
});
返回“成功!”;
}
@凌驾
小部件构建(构建上下文){
返回材料PP(
家:脚手架(
appBar:appBar(
背景颜色:颜色。深紫色,
标题:常量文本(“列表”),
),
正文:新增(
弹性:1,
子项:ListView.builder(
控制器:fixedExtentScrollController,
物理:FixedExtentScrollPhysics(),
itemCount:data==null?0:data.length,
itemBuilder:(构建上下文,int索引){
退回新货柜(
儿童:中心(
子:列(
crossAxisAlignment:crossAxisAlignment.stretch,
儿童:[
卡片(
子:容器(
填充:所有边缘设置(15.0),
孩子:排(
儿童:[
正文(“标题:”),
文本(数据[索引][“标题”],
样式:TextStyle(
fontSize:11.0,颜色:Colors.black87),
],
)),
),
],
),
),
);
},
),
),
),
);
}
@凌驾
void initState(){
super.initState();
this.getTitle();
}
}
有什么帮助吗?检查错误日志:
FixedExtentScrollController can only be used with ListWheelScrollViews
因此,您不能将FixedExtentScrollController
与ListView
一起使用,您需要使用ListWheelScrollView
这是您的代码修复:
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
backgroundColor: Colors.deepPurple,
title: const Text('List'),
),
body: data == null
? Center(
child: CircularProgressIndicator(),
)
: ListWheelScrollView(
itemExtent: 100.0,
controller: fixedExtentScrollController,
children: data
.map((val) => Container(
child: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Card(
child: Container(
padding: EdgeInsets.all(15.0),
child: Row(
children: <Widget>[
Text("Title: "),
Expanded(
child: Text(val["title"],
style: TextStyle(
fontSize: 11.0,
color: Colors.black87)),
),
],
)),
),
],
),
),
))
.toList()),
),
);
}
@覆盖
小部件构建(构建上下文){
返回材料PP(
家:脚手架(
appBar:appBar(
背景颜色:颜色。深紫色,
标题:常量文本(“列表”),
),
正文:数据==null
?中心(
子对象:CircularProgressIndicator(),
)
:ListWheelScrollView(
项目范围:100.0,
控制器:fixedExtentScrollController,
儿童:数据
.map((val)=>容器(
儿童:中心(
子:列(
crossAxisAlignment:crossAxisAlignment.stretch,
儿童:[
卡片(
子:容器(
填充:所有边缘设置(15.0),
孩子:排(
儿童:[
正文(“标题:”),
扩大(
子项:文本(val[“title”],
样式:TextStyle(
字体大小:11.0,
颜色:颜色。黑色87),
),
],
)),
),
],
),
),
))
.toList()),
),
);
}