Files
FlutterUnit/lib/views/widgets/MultiChildRenderObjectWidget/Viewport/node1_base.dart
2020-08-10 12:43:35 +08:00

93 lines
3.0 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
/// create by 张风捷特烈 on 2020/8/2
/// contact me by email 1981462002@qq.com
/// 说明: 340 Viewport 视口组件 通常用于为滑动视图提供视口,仅构建显示和预加载的部位。可指定预加载的长度、滑动轴向等。是ScrollView的核心实现组件之一一般不直接使用。
// {
// "widgetId": 340,
// "name": "Viewport的基本使用",
// "priority": 1,
// "subtitle": "【offset】 : *视口偏移量 【ViewportOffset】\n"
// "【cacheExtentStyle】: 预加载类型 【CacheExtentStyle】\n"
// "【cacheExtent】: 预加载量 【double】\n"
// "【axisDirection】: 滑动方向 【AxisDirection】\n"
// "【slivers】: 子Sliver组件集 【List<Widget>】\n"
// "【anchor】: 锚点 【double】\n"
// "可以运行这些代码查看ColorItem的构建情况128个色条并非一次性全部构建。",
// }
class ViewportDemo extends StatelessWidget {
final data = List.generate(128, (i) => Color(0xFF6600FF - 2 * i));
@override
Widget build(BuildContext context) {
return Container(
height: 250,
child: Scrollable(
axisDirection: AxisDirection.down,
physics: BouncingScrollPhysics(),
dragStartBehavior: DragStartBehavior.start,
viewportBuilder: (ctx, position) => Viewport(
axisDirection: AxisDirection.down,
cacheExtent: 200,
anchor: 0,
cacheExtentStyle: CacheExtentStyle.pixel,
offset: position,
slivers: <Widget>[_buildSliverList()],
),
),
);
}
Widget _buildSliverList() => SliverList(
delegate: SliverChildBuilderDelegate(
(_, int index) =>ColorItem(color: data[index],),
childCount: data.length),
);
String colorString(Color color) =>
"#${color.value.toRadixString(16).padLeft(8, '0').toUpperCase()}";
}
class ColorItem extends StatefulWidget {
final Color color;
ColorItem({Key key,this.color}) : super(key: key);
@override
_ColorItemState createState() => _ColorItemState();
}
class _ColorItemState extends State<ColorItem> {
@override
void initState() {
super.initState();
print('-----initState----${colorString(widget.color)}-----------');
}
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.only(top: 1),
alignment: Alignment.center,
width: 100,
height: 60,
color: widget.color,
child: Text(
colorString(widget.color),
style: TextStyle(color: Colors.white, shadows: [
Shadow(
color: Colors.black,
offset: Offset(.5, .5),
blurRadius: 2)
]),
),
);
}
String colorString(Color color) =>
"#${color.value.toRadixString(16).padLeft(8, '0').toUpperCase()}";
}