I created in flutter a fitness app and I am on my profile screen. I want the user to show another data when a new category is created, I will show the issue here : https://imgur.com/a/XiStzun
Here it’s the code for the page where workout schedule appears and all widgets( most essentially because all code doesn’t fit)
class WorkoutContentAdded extends StatefulWidget {
final String programId;
const WorkoutContentAdded({Key? key, required this.programId}) : super(key: key);
@override
_WorkoutContentAddedState createState() => _WorkoutContentAddedState();
}
class _WorkoutContentAddedState extends State<WorkoutContentAdded> {
late Stream<QuerySnapshot> _workoutScheduleStream;
@override
void initState() {
super.initState();
_initializeWorkoutScheduleStream();
}
@override
void didUpdateWidget(WorkoutContentAdded oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.programId != widget.programId) {
_initializeWorkoutScheduleStream();
}
}
void _initializeWorkoutScheduleStream() {
print('Initializing workout schedule stream for program: ${widget.programId}');
_workoutScheduleStream = FirebaseFirestore.instance
.collection('programs')
.doc(widget.programId)
.collection('workout_schedule')
.orderBy('order')
.snapshots();
}
@override
Widget build(BuildContext context) {
print('Building WorkoutContentAdded for program: ${widget.programId}');
return Scaffold(
// ... other scaffold properties
body: CustomScrollView(
slivers: [
// ... other slivers
SliverToBoxAdapter(
child: Padding(
padding: EdgeInsets.fromLTRB(16, 16, 16, 0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Workout Schedule',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
StreamBuilder<QuerySnapshot>(
stream: _workoutScheduleStream,
builder: (context, scheduleSnapshot) {
print('Workout schedule stream update for program: ${widget.programId}');
print('Connection state: ${scheduleSnapshot.connectionState}');
if (scheduleSnapshot.hasError) {
print('Error in schedule stream: ${scheduleSnapshot.error}');
}
if (scheduleSnapshot.hasData) {
print('Number of workout days: ${scheduleSnapshot.data!.docs.length}');
}
if (scheduleSnapshot.connectionState == ConnectionState.waiting) {
return CircularProgressIndicator();
}
if (scheduleSnapshot.hasError) {
return Text('Error: ${scheduleSnapshot.error}');
}
if (!scheduleSnapshot.hasData || scheduleSnapshot.data!.docs.isEmpty) {
return Padding(
padding: EdgeInsets.only(top: 16),
child: Text('No workout schedule added for this program.'),
);
}
List<DocumentSnapshot> sortedDocs = scheduleSnapshot.data!.docs;
return ListView.builder(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemCount: sortedDocs.length,
padding: EdgeInsets.only(top: 10),
itemBuilder: (context, index) {
Map<String, dynamic> data = sortedDocs[index].data() as Map<String, dynamic>;
String day = data['day'] ?? 'Unknown Day';
return GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => VideoContent(
day: day,
allowEditing: true,
programId: widget.programId,
),
),
);
},
child: Card(
// Card content
),
);
},
);
},
),
],
),
),
),
],
),
floatingActionButton: FloatingActionButton(
onPressed: _showAddCategoryBottomSheet,
backgroundColor: Colors.black,
child: Icon(Icons.add, color: Colors.white),
),
floatingActionButtonLocation: FloatingActionButtonLocation.startFloat,
);
}
void _showAddCategoryBottomSheet() {
String selectedOrdinal = '1st';
String selectedDay = 'Monday';
String estimatedKcal = '';
showModalBottomSheet(
context: context,
isScrollControlled: true,
backgroundColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(top: Radius.circular(20)),
),
builder: (BuildContext context) {
return StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return Padding(
padding: EdgeInsets.only(
bottom: MediaQuery.of(context).viewInsets.bottom,
left: 24,
right: 24,
top: 24,
),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Choose the day of the week',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
SizedBox(height: 20),
Row(
children: [
Expanded(
child: DropdownButtonFormField<String>(
value: selectedOrdinal,
decoration: InputDecoration(
border: OutlineInputBorder(),
contentPadding: EdgeInsets.symmetric(horizontal: 10, vertical: 5),
),
items: ['1st', '2nd', '3rd', '4th']
.map((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
onChanged: (String? newValue) {
if (newValue != null) {
setState(() {
selectedOrdinal = newValue;
});
}
},
),
),
SizedBox(width: 10),
Expanded(
child: DropdownButtonFormField<String>(
value: selectedDay,
decoration: InputDecoration(
border: OutlineInputBorder(),
contentPadding: EdgeInsets.symmetric(horizontal: 10, vertical: 5),
),
items: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
.map((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
onChanged: (String? newValue) {
if (newValue != null) {
setState(() {
selectedDay = newValue;
});
}
},
),
),
],
),
SizedBox(height: 20),
TextField(
decoration: InputDecoration(
hintText: 'Estimative kcal burnt',
border: OutlineInputBorder(),
),
keyboardType: TextInputType.number,
onChanged: (value) {
estimatedKcal = value;
},
),
SizedBox(height: 20),
Center(
child: ElevatedButton(
onPressed: () async {
await FirebaseFirestore.instance.collection('workout_schedule').add({
'ordinal': selectedOrdinal,
'day': selectedDay,
'estimatedKcal': estimatedKcal,
});
Navigator.pop(context);
},
child: Text('Save'),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.black,
foregroundColor: Colors.white,
padding: EdgeInsets.symmetric(horizontal: 32, vertical: 16),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30),
),
),
),
),
SizedBox(height: 20),
],
),
);
}
);
},
);
}
Recognized by Google Cloud Collective
2