class _MainPageState extends State<MainPage> {
String displaycode = '';
@override
Widget build(BuildContext context) {
var futureBuilder = FutureBuilder<Tasks>(
...
...
else
return createListView(context, snapshot);
}
},
);
return Scaffold(
body: futureBuilder,
);
}
//ListTile times
Widget createListView(BuildContext context, AsyncSnapshot snapshot){
return Container(
padding: const EdgeInsets.symmetric(vertical: 30, horizontal: 20),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
_mainList("Math", Text(DateFormat("hh:mm a").format(DateFormat("HH:mm").parse(snapshot.data.task1)))),
_mainList("English", Text(DateFormat("hh:mm a").format(DateFormat("HH:mm").parse(snapshot.data.task2)))),
_mainList("History", Text(DateFormat("hh:mm a").format(DateFormat("HH:mm").parse(snapshot.data.task3)))),
],
),
);
}
_mainList(String name, clock){
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 0),
child: ListTile(
title: Text(name, textScaleFactor: 1.2),
trailing: clock,
),
);
}
}
例如,如果下午4:35,则应如下图所示,应在晚上7:23突出显示时间表“历史记录”
谢谢您的时间,我真的很感激
答案 0 :(得分:1)
尝试一下:
class _MainPageState extends State<MainPage> {
String displaycode = '';
@override
Widget build(BuildContext context) {
var futureBuilder = FutureBuilder<Tasks>(
...
...
else
return createListView(context, snapshot);
}
},
);
return Scaffold(
body: futureBuilder,
);
}
//ListTile times
Widget createListView(BuildContext context, AsyncSnapshot snapshot){
return Container(
padding: const EdgeInsets.symmetric(vertical: 30, horizontal: 20),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
_mainList("Math", Text(DateFormat("hh:mm a").format(DateFormat("HH:mm").parse(snapshot.data.task1)))),
_mainList("English", Text(DateFormat("hh:mm a").format(DateFormat("HH:mm").parse(snapshot.data.task2)))),
_mainList("History", Text(DateFormat("hh:mm a").format(DateFormat("HH:mm").parse(snapshot.data.task3)))),
],
),
);
}
_mainList(String name, clock, bool isHighlighted){
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 0),
child: Container(
color:isHighlighted?Colors.grey:Colors.transparent
ListTile(
title: Text(name, textScaleFactor: 1.2),
trailing: clock,
),
);
}
}
为了便于阅读,我将在这里保留您的条件逻辑:
(DateFormat("hh:mm a").format(DateTime.parse(snapshot.data.task1)))==DateFormat("hh:mm a").format(DateTime.now()))?true:false;
在调用_mainList时,使用此条件逻辑传递bool。