如何使用 Flutter 在我的应用栏中构建可水平滚动的日历?
How do construct a horizontally scrollable calendar in my appbar with Flutter?
这就是我现在的样子。
我找不到任何插件或任何方法来构建这种特定类型的小部件。我在这方面遇到了麻烦,因此非常感谢您的帮助。
import 'package:flutter/material.dart';
import 'package:healthonify_mobile/constants/text_styles.dart';
import 'package:healthonify_mobile/widgets/cards/add_workout.dart';
import 'package:healthonify_mobile/widgets/cards/average_calories_burnt.dart';
import 'package:healthonify_mobile/widgets/cards/reminder_card.dart';
import 'package:healthonify_mobile/widgets/cards/steps_card.dart';
import 'package:healthonify_mobile/widgets/cards/track_workout.dart';
import 'package:healthonify_mobile/widgets/cards/workout_details.dart';
import 'package:healthonify_mobile/widgets/other/calendar_appbar.dart';
class WorkoutScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: CalendarAppBar(),
body: SingleChildScrollView(
physics: BouncingScrollPhysics(),
child: Column(
children: [
SizedBox(height: 12),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
WorkoutDetailsCard(),
],
),
SizedBox(height: 22),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
StepsCard(),
],
),
SizedBox(height: 22),
Row(
children: [
Padding(
padding: const EdgeInsets.only(left: 16),
child: Text(
'Track your workout',
style: openSans22DarkBold,
),
),
],
),
SizedBox(height: 12),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TrackWorkoutCard(),
],
),
SizedBox(height: 22),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
AddWorkoutCard(),
],
),
SizedBox(height: 22),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
AverageCalBurntCard(),
],
),
SizedBox(height: 22),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ReminderCard(),
],
),
SizedBox(height: 34),
],
),
),
),
);
}
}
import 'package:intl/intl.dart';
import 'package:flutter/material.dart';
class CalendarAppBar extends StatefulWidget implements PreferredSizeWidget {
const CalendarAppBar({Key? key}) : super(key: key);
@override
Size get preferredSize => const Size.fromHeight(148.0);
@override
State<CalendarAppBar> createState() => _CalendarAppBarState();
}
class _CalendarAppBarState extends State<CalendarAppBar> {
int selectedIndex = 0;
DateTime now = DateTime.now();
late DateTime lastDayOfMonth;
@override
void initState() {
super.initState();
lastDayOfMonth = DateTime(now.year, now.month + 1, 0);
}
@override
Widget build(BuildContext context) {
return AppBar(
backgroundColor: Colors.white,
toolbarHeight: 148.0,
title: Column(
children: [
Row(
children: const [
Icon(
Icons.arrow_back_ios,
color: Colors.orange,
),
Expanded(
child: Text(
"Workout",
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.black,
),
),
),
],
),
const SizedBox(height: 16.0),
SingleChildScrollView(
scrollDirection: Axis.horizontal,
physics: const ClampingScrollPhysics(),
child: Row(
children: List.generate(
lastDayOfMonth.day,
(index) {
final currentDate =
lastDayOfMonth.add(Duration(days: index + 1));
final dayName = DateFormat('E').format(currentDate);
return Padding(
padding: EdgeInsets.only(
left: index == 0 ? 16.0 : 0.0, right: 16.0),
child: GestureDetector(
onTap: () => setState(
() {
selectedIndex = index;
},
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
height: 42.0,
width: 42.0,
alignment: Alignment.center,
decoration: BoxDecoration(
color: selectedIndex == index
? Colors.orange
: Colors.transparent,
borderRadius: BorderRadius.circular(44.0),
),
child: Text(
dayName.substring(0, 1),
style: TextStyle(
fontSize: 24.0,
color: selectedIndex == index
? Colors.white
: Colors.black54,
fontWeight: FontWeight.bold,
),
),
),
const SizedBox(height: 8.0),
Text(
"${index + 1}",
style: const TextStyle(
fontSize: 16.0,
color: Colors.black54,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 8.0),
Container(
height: 2.0,
width: 28.0,
color: selectedIndex == index
? Colors.orange
: Colors.transparent,
),
],
),
),
);
},
),
),
),
],
),
);
}
}
这是一些使用应用栏的代码,应用中其他地方的链接不多,所以基本上只有 2 个屏幕。我不明白为什么即使我没有填充或偏移,appbar 也会自动向左对齐。
您只需使用带有参数 scrollDirection: Axis.horizontal,
的 Listview.builder 小部件
试试下面的代码,你会根据你的描述得到准确的输出
在pubspec.yaml
文件中添加包
intl: ^0.17.0
输出:-
代码:-
import 'package:intl/intl.dart';
import 'package:flutter/material.dart';
class DatePickerCustom extends StatefulWidget {
const DatePickerCustom({Key? key}) : super(key: key);
@override
State<DatePickerCustom> createState() => _DatePickerCustomState();
}
class _DatePickerCustomState extends State<DatePickerCustom> {
int selectedIndex = 0;
DateTime now = DateTime.now();
late DateTime lastDayOfMonth;
@override
void initState() {
super.initState();
lastDayOfMonth = DateTime(now.year, now.month + 1, 0);
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
backgroundColor: Colors.white,
toolbarHeight: 148.0,
title: Column(
children: [
Row(
children: const [
Icon(
Icons.arrow_back_ios,
color: Colors.orange,
),
Expanded(
child: Text("Workout",
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.black,
)))
],
),
const SizedBox(height: 16.0),
SingleChildScrollView(
scrollDirection: Axis.horizontal,
physics: const ClampingScrollPhysics(),
child: Row(
children: List.generate(
lastDayOfMonth.day,
(index) {
final currentDate =
lastDayOfMonth.add(Duration(days: index + 1));
final dayName = DateFormat('E').format(currentDate);
return Padding(
padding: EdgeInsets.only(
left: index == 0 ? 16.0 : 0.0, right: 16.0),
child: GestureDetector(
onTap: () => setState(() {
selectedIndex = index;
}),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
height: 42.0,
width: 42.0,
alignment: Alignment.center,
decoration: BoxDecoration(
color: selectedIndex == index
? Colors.orange
: Colors.transparent,
borderRadius: BorderRadius.circular(44.0),
),
child: Text(
dayName.substring(0, 1),
style: TextStyle(
fontSize: 24.0,
color: selectedIndex == index
? Colors.white
: Colors.black54,
fontWeight: FontWeight.bold,
),
),
),
const SizedBox(height: 8.0),
Text(
"${index + 1}",
style: const TextStyle(
fontSize: 16.0,
color: Colors.black54,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 8.0),
Container(
height: 2.0,
width: 28.0,
color: selectedIndex == index
? Colors.orange
: Colors.transparent,
),
],
),
),
);
},
),
),
),
],
),
),
);
}
}
这就是我现在的样子。
我找不到任何插件或任何方法来构建这种特定类型的小部件。我在这方面遇到了麻烦,因此非常感谢您的帮助。
import 'package:flutter/material.dart';
import 'package:healthonify_mobile/constants/text_styles.dart';
import 'package:healthonify_mobile/widgets/cards/add_workout.dart';
import 'package:healthonify_mobile/widgets/cards/average_calories_burnt.dart';
import 'package:healthonify_mobile/widgets/cards/reminder_card.dart';
import 'package:healthonify_mobile/widgets/cards/steps_card.dart';
import 'package:healthonify_mobile/widgets/cards/track_workout.dart';
import 'package:healthonify_mobile/widgets/cards/workout_details.dart';
import 'package:healthonify_mobile/widgets/other/calendar_appbar.dart';
class WorkoutScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: CalendarAppBar(),
body: SingleChildScrollView(
physics: BouncingScrollPhysics(),
child: Column(
children: [
SizedBox(height: 12),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
WorkoutDetailsCard(),
],
),
SizedBox(height: 22),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
StepsCard(),
],
),
SizedBox(height: 22),
Row(
children: [
Padding(
padding: const EdgeInsets.only(left: 16),
child: Text(
'Track your workout',
style: openSans22DarkBold,
),
),
],
),
SizedBox(height: 12),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TrackWorkoutCard(),
],
),
SizedBox(height: 22),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
AddWorkoutCard(),
],
),
SizedBox(height: 22),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
AverageCalBurntCard(),
],
),
SizedBox(height: 22),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ReminderCard(),
],
),
SizedBox(height: 34),
],
),
),
),
);
}
}
import 'package:intl/intl.dart';
import 'package:flutter/material.dart';
class CalendarAppBar extends StatefulWidget implements PreferredSizeWidget {
const CalendarAppBar({Key? key}) : super(key: key);
@override
Size get preferredSize => const Size.fromHeight(148.0);
@override
State<CalendarAppBar> createState() => _CalendarAppBarState();
}
class _CalendarAppBarState extends State<CalendarAppBar> {
int selectedIndex = 0;
DateTime now = DateTime.now();
late DateTime lastDayOfMonth;
@override
void initState() {
super.initState();
lastDayOfMonth = DateTime(now.year, now.month + 1, 0);
}
@override
Widget build(BuildContext context) {
return AppBar(
backgroundColor: Colors.white,
toolbarHeight: 148.0,
title: Column(
children: [
Row(
children: const [
Icon(
Icons.arrow_back_ios,
color: Colors.orange,
),
Expanded(
child: Text(
"Workout",
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.black,
),
),
),
],
),
const SizedBox(height: 16.0),
SingleChildScrollView(
scrollDirection: Axis.horizontal,
physics: const ClampingScrollPhysics(),
child: Row(
children: List.generate(
lastDayOfMonth.day,
(index) {
final currentDate =
lastDayOfMonth.add(Duration(days: index + 1));
final dayName = DateFormat('E').format(currentDate);
return Padding(
padding: EdgeInsets.only(
left: index == 0 ? 16.0 : 0.0, right: 16.0),
child: GestureDetector(
onTap: () => setState(
() {
selectedIndex = index;
},
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
height: 42.0,
width: 42.0,
alignment: Alignment.center,
decoration: BoxDecoration(
color: selectedIndex == index
? Colors.orange
: Colors.transparent,
borderRadius: BorderRadius.circular(44.0),
),
child: Text(
dayName.substring(0, 1),
style: TextStyle(
fontSize: 24.0,
color: selectedIndex == index
? Colors.white
: Colors.black54,
fontWeight: FontWeight.bold,
),
),
),
const SizedBox(height: 8.0),
Text(
"${index + 1}",
style: const TextStyle(
fontSize: 16.0,
color: Colors.black54,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 8.0),
Container(
height: 2.0,
width: 28.0,
color: selectedIndex == index
? Colors.orange
: Colors.transparent,
),
],
),
),
);
},
),
),
),
],
),
);
}
}
这是一些使用应用栏的代码,应用中其他地方的链接不多,所以基本上只有 2 个屏幕。我不明白为什么即使我没有填充或偏移,appbar 也会自动向左对齐。
您只需使用带有参数 scrollDirection: Axis.horizontal,
试试下面的代码,你会根据你的描述得到准确的输出
在pubspec.yaml
文件中添加包
intl: ^0.17.0
输出:-
代码:-
import 'package:intl/intl.dart';
import 'package:flutter/material.dart';
class DatePickerCustom extends StatefulWidget {
const DatePickerCustom({Key? key}) : super(key: key);
@override
State<DatePickerCustom> createState() => _DatePickerCustomState();
}
class _DatePickerCustomState extends State<DatePickerCustom> {
int selectedIndex = 0;
DateTime now = DateTime.now();
late DateTime lastDayOfMonth;
@override
void initState() {
super.initState();
lastDayOfMonth = DateTime(now.year, now.month + 1, 0);
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
backgroundColor: Colors.white,
toolbarHeight: 148.0,
title: Column(
children: [
Row(
children: const [
Icon(
Icons.arrow_back_ios,
color: Colors.orange,
),
Expanded(
child: Text("Workout",
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.black,
)))
],
),
const SizedBox(height: 16.0),
SingleChildScrollView(
scrollDirection: Axis.horizontal,
physics: const ClampingScrollPhysics(),
child: Row(
children: List.generate(
lastDayOfMonth.day,
(index) {
final currentDate =
lastDayOfMonth.add(Duration(days: index + 1));
final dayName = DateFormat('E').format(currentDate);
return Padding(
padding: EdgeInsets.only(
left: index == 0 ? 16.0 : 0.0, right: 16.0),
child: GestureDetector(
onTap: () => setState(() {
selectedIndex = index;
}),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
height: 42.0,
width: 42.0,
alignment: Alignment.center,
decoration: BoxDecoration(
color: selectedIndex == index
? Colors.orange
: Colors.transparent,
borderRadius: BorderRadius.circular(44.0),
),
child: Text(
dayName.substring(0, 1),
style: TextStyle(
fontSize: 24.0,
color: selectedIndex == index
? Colors.white
: Colors.black54,
fontWeight: FontWeight.bold,
),
),
),
const SizedBox(height: 8.0),
Text(
"${index + 1}",
style: const TextStyle(
fontSize: 16.0,
color: Colors.black54,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 8.0),
Container(
height: 2.0,
width: 28.0,
color: selectedIndex == index
? Colors.orange
: Colors.transparent,
),
],
),
),
);
},
),
),
),
],
),
),
);
}
}