性能优化

develop^2
LXD312569496 5 years ago committed by xiaodong
parent ea83dc2739
commit 8b45252b9f

@ -124,3 +124,14 @@ class DateModel {
return this.getDateTime().isBefore(dateModel.getDateTime()); return this.getDateTime().isBefore(dateModel.getDateTime());
} }
} }
class X {
var _y;
get y => null == _y ? initY() : _y;
initY() {
//do some computation
_y = "result";
}
}

@ -22,11 +22,12 @@ class MonthView extends StatefulWidget {
final CalendarConfiguration configuration; final CalendarConfiguration configuration;
const MonthView({ const MonthView({
Key key,
@required this.year, @required this.year,
@required this.month, @required this.month,
this.day, this.day,
this.configuration, this.configuration,
}); }) : super(key: key);
@override @override
_MonthViewState createState() => _MonthViewState(); _MonthViewState createState() => _MonthViewState();
@ -34,13 +35,13 @@ class MonthView extends StatefulWidget {
class _MonthViewState extends State<MonthView> class _MonthViewState extends State<MonthView>
with AutomaticKeepAliveClientMixin { with AutomaticKeepAliveClientMixin {
List<DateModel> items; List<DateModel> items = List();
int lineCount; int lineCount;
double itemHeight; // double itemHeight;
double totalHeight; // double totalHeight;
double mainSpacing = 10; // double mainSpacing = 10;
DateModel minSelectDate; DateModel minSelectDate;
DateModel maxSelectDate; DateModel maxSelectDate;
@ -68,38 +69,48 @@ class _MonthViewState extends State<MonthView>
items = CacheData.getInstance().monthListCache[firstDayOfMonth]; items = CacheData.getInstance().monthListCache[firstDayOfMonth];
} else { } else {
LogUtil.log(TAG: this.runtimeType, message: "缓存中无数据"); LogUtil.log(TAG: this.runtimeType, message: "缓存中无数据");
items = DateUtil.initCalendarForMonthView( getItems();
widget.year, widget.month, DateTime.now(), DateTime.sunday,
minSelectDate: minSelectDate,
maxSelectDate: maxSelectDate,
extraDataMap: extraDataMap);
CacheData.getInstance().monthListCache[firstDayOfMonth] = items; CacheData.getInstance().monthListCache[firstDayOfMonth] = items;
} }
lineCount = DateUtil.getMonthViewLineCount(widget.year, widget.month); lineCount = DateUtil.getMonthViewLineCount(widget.year, widget.month);
} }
getItems() async {
items = await compute(initCalendarForMonthView, {
'year': widget.year,
'month': widget.month,
'minSelectDate': minSelectDate,
'maxSelectDate': maxSelectDate,
'extraDataMap': extraDataMap
});
setState(() {});
}
static Future<List<DateModel>> initCalendarForMonthView(Map map) async {
return DateUtil.initCalendarForMonthView(
map['year'], map['month'], DateTime.now(), DateTime.sunday,
minSelectDate: map['minSelectDate'],
maxSelectDate: map['maxSelectDate'],
extraDataMap: map['extraDataMap']);
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
super.build(context); super.build(context);
LogUtil.log(TAG: this.runtimeType, message: "_MonthViewState build"); LogUtil.log(TAG: this.runtimeType, message: "_MonthViewState build");
itemHeight = MediaQuery.of(context).size.width / 7;
totalHeight = itemHeight * lineCount + mainSpacing * (lineCount - 1);
return Container(height: totalHeight, child: getView());
}
Widget getView() {
CalendarProvider calendarProvider = CalendarProvider calendarProvider =
Provider.of<CalendarProvider>(context, listen: false); Provider.of<CalendarProvider>(context, listen: false);
CalendarConfiguration configuration = CalendarConfiguration configuration =
calendarProvider.calendarConfiguration; calendarProvider.calendarConfiguration;
return new GridView.builder( return new GridView.builder(
physics: NeverScrollableScrollPhysics(), addAutomaticKeepAlives: true,
gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount( physics: const NeverScrollableScrollPhysics(),
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 7, mainAxisSpacing: 10), crossAxisCount: 7, mainAxisSpacing: 10),
itemCount: 7 * lineCount, itemCount: items.isEmpty ? 0 : 7 * lineCount,
itemBuilder: (context, index) { itemBuilder: (context, index) {
DateModel dateModel = items[index]; DateModel dateModel = items[index];
// //
@ -119,8 +130,6 @@ class _MonthViewState extends State<MonthView>
return ItemContainer( return ItemContainer(
dateModel: dateModel, dateModel: dateModel,
// configuration: configuration,
// calendarProvider: calendarProvider,
); );
}); });
} }
@ -135,9 +144,6 @@ class _MonthViewState extends State<MonthView>
class ItemContainer extends StatefulWidget { class ItemContainer extends StatefulWidget {
final DateModel dateModel; final DateModel dateModel;
// CalendarConfiguration configuration;
// CalendarProvider calendarProvider;
const ItemContainer({ const ItemContainer({
Key key, Key key,
this.dateModel, this.dateModel,
@ -161,9 +167,26 @@ class ItemContainerState extends State<ItemContainer> {
isSelected = ValueNotifier(dateModel.isSelected); isSelected = ValueNotifier(dateModel.isSelected);
} }
/**
* item
*/
void refreshItem() {
/**
Exception caught by gesture
The following assertion was thrown while handling a gesture:
setState() called after dispose()
*/
if (mounted) {
setState(() {
dateModel.isSelected = !dateModel.isSelected;
// isSelected.value = !isSelected.value;
});
}
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
// LogUtil.log(TAG: this.runtimeType,message: "ItemContainerState build"); LogUtil.log(TAG: this.runtimeType, message: "ItemContainerState build");
calendarProvider = Provider.of<CalendarProvider>(context, listen: false); calendarProvider = Provider.of<CalendarProvider>(context, listen: false);
configuration = calendarProvider.calendarConfiguration; configuration = calendarProvider.calendarConfiguration;
@ -215,29 +238,26 @@ class ItemContainerState extends State<ItemContainer> {
refreshItem(); refreshItem();
}, },
child: configuration.dayWidgetBuilder(dateModel), child: configuration.dayWidgetBuilder(dateModel),
// child: ValueListenableBuilder(
// valueListenable: isSelected,
// builder: (BuildContext context, bool value, Widget child) {
// return configuration.dayWidgetBuilder(dateModel);
// }),
); );
} }
/** @override
* item void deactivate() {
*/ LogUtil.log(
void refreshItem() { TAG: this.runtimeType, message: "ItemContainerState deactivate");
/** super.deactivate();
* }
Exception caught by gesture
The following assertion was thrown while handling a gesture: @override
setState() called after dispose() void dispose() {
*/ LogUtil.log(TAG: this.runtimeType, message: "ItemContainerState dispose");
if (mounted) { super.dispose();
setState(() { }
dateModel.isSelected = !dateModel.isSelected;
// isSelected.value = !isSelected.value; @override
}); void didUpdateWidget(ItemContainer oldWidget) {
} LogUtil.log(
TAG: this.runtimeType, message: "ItemContainerState didUpdateWidget");
super.didUpdateWidget(oldWidget);
} }
} }

@ -15,7 +15,7 @@ class MonthViewPager extends StatefulWidget {
_MonthViewPagerState createState() => _MonthViewPagerState(); _MonthViewPagerState createState() => _MonthViewPagerState();
} }
class _MonthViewPagerState extends State<MonthViewPager> { class _MonthViewPagerState extends State<MonthViewPager> with AutomaticKeepAliveClientMixin{
CalendarProvider calendarProvider; CalendarProvider calendarProvider;
@override @override
@ -57,6 +57,8 @@ class _MonthViewPagerState extends State<MonthViewPager> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
super.build(context);
LogUtil.log(TAG: this.runtimeType, message: "MonthViewPager build");
// CalendarProvider,listenfalse // CalendarProvider,listenfalse
calendarProvider = Provider.of<CalendarProvider>(context, listen: false); calendarProvider = Provider.of<CalendarProvider>(context, listen: false);
CalendarConfiguration configuration = CalendarConfiguration configuration =
@ -110,4 +112,7 @@ class _MonthViewPagerState extends State<MonthViewPager> {
itemCount: configuration.monthList.length, itemCount: configuration.monthList.length,
); );
} }
@override
bool get wantKeepAlive => true;
} }

@ -14,7 +14,7 @@ class WeekViewPager extends StatefulWidget {
_WeekViewPagerState createState() => _WeekViewPagerState(); _WeekViewPagerState createState() => _WeekViewPagerState();
} }
class _WeekViewPagerState extends State<WeekViewPager> { class _WeekViewPagerState extends State<WeekViewPager> with AutomaticKeepAliveClientMixin{
int lastMonth; // int lastMonth; //
CalendarProvider calendarProvider; CalendarProvider calendarProvider;
@ -37,6 +37,7 @@ class _WeekViewPagerState extends State<WeekViewPager> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
super.build(context);
LogUtil.log(TAG: this.runtimeType, message: "WeekViewPager build"); LogUtil.log(TAG: this.runtimeType, message: "WeekViewPager build");
// CalendarProvider,listenfalse // CalendarProvider,listenfalse
@ -92,4 +93,7 @@ class _WeekViewPagerState extends State<WeekViewPager> {
), ),
); );
} }
@override
bool get wantKeepAlive => true;
} }

Loading…
Cancel
Save