🔥🔥🔥Easier and more flexible to create multiple types for Flutter ListView.
Add flutter_multitype: ^0.9.2
to your pubspec.yaml
file:
dependencies:
flutter_multitype: ^0.9.2
Import get in files that it will be used:
import 'package:flutter_multitype/multitype.dart';
flutter_multitype
can be used to decouple and improve readability when items in a ListView have
different types of layouts and are dynamically indeterminate .
one data bind one widget
class CategoryName {
String? title;
CategoryName(this.title);
}
class CategorySubContent {
String? title;
String? url;
CategorySubContent(this.title, this.url);
}
class CategoryViewBinder extends ItemViewBinder<CategoryName> {
@override
Widget buildWidget(BuildContext context, CategoryName item, int index) {
return const Text("Category");
}
}
class ContentViewBinder extends ItemViewBinder<List<CategorySubContent>> {
@override
Widget buildWidget(BuildContext context, List<CategorySubContent> item, int index) {
return const Text("Content");
}
}
class _MediaPageState extends State<MediaPage> {
List<dynamic> items = Data.getMediaData();
MultiTypeAdapter adapter = MultiTypeAdapter.newInstance((adapter) {
adapter.register(CategoryViewBinder());
adapter.register(ContentViewBinder());
adapter.setDebugViewBinderEnable(isEnable: true);
});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Media Page"),
),
body: ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
return adapter.getItemBuilder(context, index, items[index]);
},
),
);
}
}
Multi type item widget ListView is complete
one data bind many widget,for example:
/// ChatMessage will be bind TextMeViewBinder、TextOtherViewBinder、UnknownMeViewBinder、UnknownOtherViewBinder four Widget
MultiTypeAdapter adapter = MultiTypeAdapter.newInstance((adapter) {
adapter.registerOneToMany<ChatMessage>((position, item) {
var message = item as ChatMessage;
if (message.isMe == true) {
switch (message.type) {
case 0:
{
return TextMeViewBinder();
}
default:
{
return UnknownMeViewBinder();
}
}
} else {
switch (message.type) {
case 1:
{
return TextOtherViewBinder();
}
default:
{
return UnknownOtherViewBinder();
}
}
}
});
If someone items data have not register, it will be bind unsupportedViewBinder. For example: Old version received new version data, you can show this unsupportedViewBinder for tips user update app. But if your data is same type,this function is not suitable.You can use in different type data.
void registerUnsupportedViewBinder(ItemViewBinder unsupportedViewBinder)
If someone items data have not register, it will be bind debugViewBinder if someone items data have
not register,at the same time register unsupportedViewBinder and debugViewBinder, debugViewBinder
will be cover unsupportedViewBinder. Don't worry,debugViewBinder is never show in release,it just
show in !bool.fromEnvironment("dart.vm.product")
.
void setDebugViewBinderEnable({bool isEnable = !inProduction, ItemViewBinder? debugViewBinder})
GitHub : Wenlong-Guo
Email : guowenlong20000@sina.com
WeChat : xiaoguo9745