如何通过ListView.Custom类型创建ListView
如何通过listview.Custom创建listview。我试过使用ListView.Builder和ListView.Separted。两者都工作正常如何通过ListView.Custom类型创建ListView,listview,flutter,flutter-layout,Listview,Flutter,Flutter Layout,如何通过listview.Custom创建listview。我试过使用ListView.Builder和ListView.Separted。两者都工作正常 import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { // This widget is the root of your application. @over
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: ListView.custom(
scrollDirection: Axis.vertical,
reverse: false,
childrenDelegate: new MyCustomDelegate(),));
}
}
List<String> listItems = ["One", "Two", "Three", "Four","Five","Six"];
class MyCustomDelegate extends SliverChildDelegate {
@override
int get estimatedChildCount => listItems.length;
@override
bool shouldRebuild(SliverChildDelegate oldDelegate) {
return true;
}
@override
Widget build(BuildContext context, int index) {
return Container(padding: new EdgeInsets.only(top: 16.0),
child: new Text(listItems[index]),);
}
}
导入“包装:颤振/材料.省道”;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
//此小部件是应用程序的根。
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
),
主页:MyHomePage(标题:“颤振演示主页”),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
//在这里,我们从MyHomePage对象中获取由创建的值
//使用App.build方法,并使用它设置appbar标题。
标题:文本(widget.title),
),
正文:ListView.custom(
滚动方向:轴垂直,
反面:错,
childrenDelegate:new MyCustomDelegate(),);
}
}
列表项=[“一”、“二”、“三”、“四”、“五”、“六”];
类MyCustomDelegate扩展了SliverChildDelegate{
@凌驾
int get estimatedChildCount=>listItems.length;
@凌驾
布尔应重建(SliverChildDelegate oldDelegate){
返回true;
}
@凌驾
小部件构建(BuildContext上下文,int索引){
返回容器(填充:仅限新边集(顶部:16.0),
子项:新文本(列表项[索引]);
}
}
谁能帮我一下吗。
提前感谢。您应该添加验证以确保您的物品在以下范围内:
@override
Widget build(BuildContext context, int index) {
return index < estimatedChildCount
? Container(
padding: new EdgeInsets.only(top: 16.0),
child: new Text(listItems[index]),
)
: null;
}
@覆盖
小部件构建(BuildContext上下文,int索引){
返回索引