Flutter列表详情基础插件list_detail_base的使用
Flutter列表详情基础插件list_detail_base的使用
标题
Flutter列表详情基础插件list_detail_base的使用
内容
This README describes the package. If you publish this package to pub.dev, this README’s contents appear on the landing page for your package.
For information about how to write a good package README, see the guide for writing package pages.
For general information about developing packages, see the Dart guide for creating packages and the Flutter guide for developing packages and plugins.
ListDetailBase #
A template package for List-Detail or Master-Detail layouts #
Features #
- Buildss for large or small screens in one simple widget.
- Easily modifiable.
- Handles all state management with no dependencies and no fuss.
- Inherited lookup of controller
ListDetail<T>.of(context)
- Simple package. Look at the code. There's not much complexity.
Getting started #
Add package
flutter pub add list_detail_base
Usage #
Import
import 'package:list_detail_base/list_detail_base.dart';
Example (explore example code in the example app - it's short).
/// Example of ListDetailBase
class _MyHomePageState extends State<MyHomePage> {
/// Make a controller with the same type as your list.
/// [ColorEtymology] is an example data class
late final ListDetailController<ColorEtymology> controller;
@override
void initState() {
super.initState();
/// 5 second delay for show. Transform turns JSON list into a
/// list of your model
controller = ListDetailController(
fetch: () => Future.delayed(const Duration(seconds: 5), () => Future.value(colorMapList)),
transform: ColorEtymology.fromMap,
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(widget.title)),
body: ListDetailLayout(
controller: controller,
listBuilder: (context, items, isSplitScreen) => ListView.builder(
itemCount: items.length,
// [Item] is an example view - a ListTile
itemBuilder: (context, index) => Item(
colorModel: items[index],
isSplitScreen: isSplitScreen,
onSelect: (value) => controller.select = value,
),
),
detailBuilder: (context, item, isSplitScreen) => item == null
? Container()
// [ColorModelDetail] is an example view - a Card
: ColorModelDetail(
colorModel: item,
isSplitScreen: isSplitScreen,
colorModelListenable: controller.selectedItem,
),
),
);
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
}
Additional information #
This is a quick helper package I put together based on other work I was doing. If you find it useful or want to add some feature please let me know.
示例代码
// ignore_for_file: public_member_api_docs, sort_constructors_first
import 'package:example/views/example_views.dart';
import 'package:flutter/material.dart';
import 'package:list_detail_base/list_detail_base.dart';
import 'data/example_data.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Color Etymology'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
[@override](/user/override)
State<MyHomePage> createState() => _MyHomePageState();
}
/// Example of ListDetailBase
class _MyHomePageState extends State<MyHomePage> {
/// Make a controller with the same type as your list
late final ListDetailController<ColorEtymology> controller;
// final GlobalKey<ListDetailLayoutState> layoutKey = GlobalKey();
[@override](/user/override)
void initState() {
super.initState();
controller = ListDetailController();
}
[@override](/user/override)
Widget build(BuildContext context) {
final list = List<ColorEtymology>.from(
colorMapList.map(ColorEtymology.fromMap),
);
return Scaffold(
appBar: AppBar(title: Text(widget.title)),
body: ListDetail<ColorEtymology>(
controller: controller,
child: Builder(builder: (innerContext) {
return ListDetailLayout(
// key: layoutKey,
controller: ListDetail.of<ColorEtymology>(innerContext).controller,
listBuilder: (innerContext) => ColorList(list: list),
detailBuilder: (innerContext) => const ColorDetail(),
});
}),
),
);
}
[@override](/user/override)
void dispose() {
controller.dispose();
super.dispose();
}
}
更多关于Flutter列表详情基础插件list_detail_base的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter列表详情基础插件list_detail_base的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是一个关于如何在Flutter项目中使用假设的list_detail_base
插件的示例代码。请注意,由于list_detail_base
并非一个官方或广泛认可的插件,以下代码将基于一个假设的插件API和功能进行编写。
假设list_detail_base
插件提供了一些基础的列表和详情页功能,我们可以这样使用它:
-
添加依赖: 首先,在你的
pubspec.yaml
文件中添加对list_detail_base
的依赖(这里假设它存在于pub.dev上,但实际上你需要根据真实的插件名称和版本进行替换):dependencies: flutter: sdk: flutter list_detail_base: ^x.y.z # 替换为实际的版本号
-
导入插件: 在你的Dart文件中导入该插件:
import 'package:list_detail_base/list_detail_base.dart';
-
使用插件: 假设
list_detail_base
提供了一个ListDetailScaffold
小部件,用于创建列表和详情页。以下是一个简单的示例,展示如何使用这个假设的小部件:import 'package:flutter/material.dart'; import 'package:list_detail_base/list_detail_base.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter List Detail Base Example', theme: ThemeData( primarySwatch: Colors.blue, ), home: MyHomePage(), ); } } class MyHomePage extends StatefulWidget { @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { final List<String> items = List<String>.generate(10, (i) => "Item $i"); @override Widget build(BuildContext context) { return ListDetailScaffold<String>( // 假设ListDetailScaffold接受一个泛型类型,用于列表项的数据类型 title: 'My List', items: items, itemBuilder: (context, item) { // 构建列表项的Widget return ListTile( title: Text(item), ); }, onItemSelected: (item) { // 当用户点击列表项时触发 Navigator.push( context, MaterialPageRoute<void>( builder: (context) => DetailPage(item: item), ), ); }, ); } } class DetailPage extends StatelessWidget { final String item; DetailPage({Key key, @required this.item}) : super(key: key); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Detail of $item'), ), body: Center( child: Text('This is the detail page of $item'), ), ); } }
在这个示例中,我们假设ListDetailScaffold
小部件接受以下参数:
title
:列表页面的标题。items
:列表项的数据集合。itemBuilder
:一个函数,用于构建列表项的Widget。onItemSelected
:当用户点击列表项时触发的回调函数。
DetailPage
是一个简单的详情页,显示被点击列表项的详细信息。
请注意,由于list_detail_base
并非真实存在的插件,上述代码是一个基于假设API的示例。如果你正在使用一个真实的插件,请参考该插件的官方文档和API以获取正确的使用方法和参数。