[英]for loop isn't selecting the dates as I used the datewidget() in Flutter
實際上,我正在研究一個 github 項目,我下載了它並進行了練習,但我一直堅持選擇用於循環列出日期的日期,但是當我點擊它時,它不會只選擇第一個日期已選擇日期但未選擇其他日期。
Leme 向您展示 output:這是我正在嘗試的日期 select 但學習者類別和時間也沒有任何反應,如果您可以幫助我確定日期,我可以理解問題並解決其他問題。 謝謝!
也請原諒我糟糕的英語語法。
這是正文部分:
body: Column(
children: [
Row(
children: [
Container(
width: 200,
height: 260,
child: Stack(
children: [
Positioned(
left: 0,
top: 0,
child: Container(
width: 200,
height: 240,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('asset/images/iconBg.png'),
fit: BoxFit.contain)),
),
),
Positioned(
bottom: 0,
left: 20,
child: Hero(
tag: "boy2",
child: Container(
height: 220,
width: 200,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('asset/images/boy2.png'))),
),
),
)
],
),
),
Expanded(
child: Container(
padding: EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"Mr. Muneeb",
style: TextStyle(
fontSize: 21,
fontWeight: FontWeight.w700,
fontFamily: 'product'),
),
SizedBox(
height: 5,
),
Text(
"React Native",
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w500,
color: darkBlue,
fontFamily: 'circe'),
),
SizedBox(
height: 15,
),
Row(
children: [
Container(
height: 20,
width: 20,
child: RotatedBox(
quarterTurns: 2,
child: Icon(
Icons.star,
color: darkBlue,
),
),
),
SizedBox(
width: 5,
),
Text(
"4.9 Star Rating",
style: TextStyle(fontFamily: 'circe'),
)
],
),
SizedBox(
height: 10,
),
Row(
children: [
Container(
height: 20,
width: 20,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(
'asset/images/palette.png'))),
),
SizedBox(
width: 5,
),
Text(
"5 Years Experience",
style: TextStyle(fontFamily: 'circe'),
)
],
),
],
),
),
)
],
),
Expanded(
child: Container(
width: MediaQuery.of(context).size.width,
padding: EdgeInsets.all(30),
color: Colors.white,
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
"About Muneeb",
style: TextStyle(
fontWeight: FontWeight.w700,
fontSize: 17,
fontFamily: 'product'),
),
SizedBox(
height: 10,
),
Text(
"I have been working on a React Native in a industry for almost 5 years working as a front-end developer, I can insure that i can teach you the best methods of working on a react native.",
style: TextStyle(
fontFamily: 'circe',
fontSize: 12,
),
),
SizedBox(
height: 20,
),
Text(
"Courses by Muneeb",
style: TextStyle(
fontWeight: FontWeight.w700,
fontSize: 17,
fontFamily: 'product'),
),
SizedBox(
height: 10,
),
Container(
height: 100,
width: MediaQuery.of(context).size.width,
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: [
courseWidget("icon1", "Young \nLearners",
"GRADE 0-1", lightBlue, darkBlue),
courseWidget("icon2", "Creative \nBloomers",
"GRADE 0-2", yellow, Color(0xff4d4d4d)),
courseWidget("icon3", "Early \nAchievers",
"GRADE 0-3", pink, Color(0xff4a155f))
],
),
),
),
SizedBox(
height: 20,
),
Text(
"Availability",
style: TextStyle(
fontWeight: FontWeight.w700,
fontSize: 17,
fontFamily: 'product'),
),
SizedBox(
height: 10,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
for (int i = 0; i < 7; i++) dateWidget(i),
],
),
SizedBox(
height: 10,
),
Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(
children: [
timeSlotWidget("11:00 AM", false),
timeSlotWidget("12:00 PM", false),
timeSlotWidget("01:00 PM", false),
timeSlotWidget("03:00 PM", true),
],
),
Row(
children: [
timeSlotWidget("04:00 PM", false),
timeSlotWidget("06:00 PM", false),
],
),
],
),
)
],
),
),
),
),
這是日期小部件:
InkWell dateWidget(int i) {
DateTime tempDate = DateTime.now().add(Duration(days: i));
return InkWell(
onTap: () {},
child: Container(
margin: EdgeInsets.all(2),
height: 60,
width: MediaQuery.of(context).size.width * 0.11,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(10)),
color: (selectedDate == tempDate.day)
? yellow
: lightBlue.withOpacity(0.5),
),
child: Container(
padding: EdgeInsets.symmetric(vertical: 10, horizontal: 10),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
dayValue(tempDate.weekday),
style: TextStyle(fontSize: 10),
),
Text(
tempDate.day.toString(),
style: TextStyle(fontSize: 18, fontWeight: FontWeight.w500),
)
],
),
),
),
);
}
基於所提供的少量信息;
我建議使用 ListView Builder 而不是小部件樹中的行和 for 循環,它將更有效地實現您的目標。
例如:
//All the available dates for the person
List<DateTime> _dates = <DateTime>[];
//Where to store the selected date
late DateTime _selectedDate;
ListView.builder(
scrollDirection = Axis.horizontal
primary: false,
shrinkWrap: true,
controller: scrollController,
itemBuilder: (_, index) => dateWidget(_dates[index]),
itemCount: _dates.length),
)
//Date WIdget
Widget dateWidget(DateTime date){
return GestureDetector(
onTap: (){
setState(() {
_selectedDate = date;
});
},
child: Container(
child: ...
),
);
}
我希望這能讓您了解如何 go 了解它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.