eth*_*th0 3 android ios dart flutter flutter-layout
我需要创建一个 52x80 的方块网格。它看起来像这样:
但在模拟器中开发时性能特别慢(超过 1 秒“滞后”)。据我了解,Flutter 代码在物理设备上的发布模式下运行速度更快,如果设备是新设备,这对于我来说也是如此。但如果设备已经使用了几年(即三星 Galaxy S8 或 iPhone 8),那么加载视图和滚动时会出现令人沮丧的明显时间。我不能那样发布我的应用程序。我正在像这样构建我的 GridView:
GridView.count(
shrinkWrap: true,
primary: false,
padding: const EdgeInsets.all(5.0),
crossAxisCount: 52,
crossAxisSpacing: 1.0,
mainAxisSpacing: 1.0,
addAutomaticKeepAlives: true,
children: blocks.map((block) => // blocks is just a list of 4160 objects
FlatButton(
child: null,
color: block.backgroundColor,
onPressed: () {
// open a new route
},
splashColor: Colors.transparent,
highlightColor: Colors.transparent
)
).toList()
)
Run Code Online (Sandbox Code Playgroud)
我尝试过换成FlatButtonanImage或 a SizedBox,这有点帮助。关于如何加快速度有什么建议吗?
如果您需要向块添加 onTap 行为,您可以使用 CustomPainter 小部件创建自己的 CustomGridView,并绘制所有项目 + 添加一个手势检测器并计算触摸位置
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
backgroundColor: Colors.black,
body: SafeArea(
child: MyHomePage(),
),
),
);
}
}
final int redCount = 728;
final int greyCount = 3021;
final int allCount = 4160;
final int crossAxisCount = 52;
enum BlockTypes {
red,
gray,
green,
yellow,
}
class MyHomePage extends StatefulWidget {
MyHomePage()
: blocks = List<BlockTypes>.generate(allCount, (index) {
if (index < redCount) {
return BlockTypes.red;
} else if (index < redCount + greyCount) {
return BlockTypes.gray;
}
return BlockTypes.green;
});
final List<BlockTypes> blocks;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int columnsCount;
double blocSize;
int clickedIndex;
Offset clickOffset;
bool hasSizes = false;
List<BlockTypes> blocks;
final ScrollController scrollController = ScrollController();
@override
void initState() {
WidgetsBinding.instance.addPostFrameCallback(_afterLayout);
blocks = widget.blocks;
super.initState();
}
void _afterLayout(_) {
blocSize = context.size.width / crossAxisCount;
columnsCount = (allCount / crossAxisCount).ceil();
setState(() {
hasSizes = true;
});
}
void onTapDown(TapDownDetails details) {
final RenderBox box = context.findRenderObject();
clickOffset = box.globalToLocal(details.globalPosition);
}
void onTap() {
final dx = clickOffset.dx;
final dy = clickOffset.dy + scrollController.offset;
final tapedRow = (dx / blocSize).floor();
final tapedColumn = (dy / blocSize).floor();
clickedIndex = tapedColumn * crossAxisCount + tapedRow;
setState(() {
blocks[clickedIndex] = BlockTypes.yellow;
});
}
@override
Widget build(BuildContext context) {
print(blocSize);
return hasSizes
? SingleChildScrollView(
controller: scrollController,
child: GestureDetector(
onTapDown: onTapDown,
onTap: onTap,
child: CustomPaint(
size: Size(
MediaQuery.of(context).size.width,
columnsCount * blocSize,
),
painter: CustomGridView(
blocs: widget.blocks,
columnsCount: columnsCount,
blocSize: blocSize,
),
),
),
)
: Container();
}
}
class CustomGridView extends CustomPainter {
final double gap = 1;
final Paint painter = Paint()
..strokeWidth = 1
..style = PaintingStyle.fill;
final int columnsCount;
final double blocSize;
final List<BlockTypes> blocs;
CustomGridView({this.columnsCount, this.blocSize, this.blocs});
@override
void paint(Canvas canvas, Size size) {
blocs.asMap().forEach((index, bloc) {
setColor(bloc);
canvas.drawRRect(
RRect.fromRectAndRadius(
Rect.fromLTWH(
getLeft(index),
getTop(index),
blocSize - gap,
blocSize - gap,
),
Radius.circular(1.0)),
painter);
});
}
double getTop(int index) {
return (index / crossAxisCount).floor().toDouble() * blocSize;
}
double getLeft(int index) {
return (index % crossAxisCount).floor().toDouble() * blocSize;
}
@override
bool shouldRepaint(CustomGridView oldDelegate) => true;
@override
bool shouldRebuildSemantics(CustomGridView oldDelegate) => true;
void setColor(BlockTypes bloc) {
switch (bloc) {
case BlockTypes.red:
painter.color = Colors.red;
break;
case BlockTypes.gray:
painter.color = Colors.grey;
break;
case BlockTypes.green:
painter.color = Colors.green;
break;
case BlockTypes.yellow:
painter.color = Colors.yellow;
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3989 次 |
| 最近记录: |