im getting strange error. I want to create a widget for every cell in a list, but when I write "itemCount: RemindTitle.length" I have that red screen. It appears even when I enter just 1, 2 or any Int number
The second issue is that I have a Time Picker, but when I want to output numbers I've entered, it shows just random numbers, like 0 min, 30hrs
import 'dart:io';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(debugShowCheckedModeBanner: false,
home: HomePage());
}
}
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
var RemindTitle = ["First", "Second", "Third", "Fourth", "sdf"],
RemindText = ["TexttF", "TextS", "TextT", "TextFr", "sdf"];
var Counter = -1;
DateTime? date;
TimeOfDay _time = TimeOfDay.now();
var DateText = "Enter the date", ChosenTime = " ";
bool AlreadyBuilt = false;
Widget build(BuildContext context) {
return Scaffold(
body: ListView.builder(
padding: EdgeInsets.all(20),
itemCount: RemindTitle.length,
itemBuilder: (BuildContext context, int index) {
if(AlreadyBuilt == false) {
AlreadyBuilt = true;
return Row(
children: [
Expanded(
flex: 4,
child: Container(
height: 75,
child: ElevatedButton(
child: Center(
child: Text("Add Remind"),
),
onPressed: () {
showModalBottomSheet<void>(
context: context,
builder: (BuildContext context) {
return Container(
child: Center(
child: Column(children: [
Row(
children: [
Padding(padding: EdgeInsets.all(10)),
Expanded(
child: ElevatedButton(
onPressed: () async {
date = await showDatePicker(
context: context,
initialDate:
new DateTime.now(),
firstDate:
new DateTime(2015),
lastDate:
new DateTime(2030),
);
},
child: Text("Enter the date"))),
Padding(padding: EdgeInsets.all(10)),
Expanded(
child: ElevatedButton(
onPressed: () async {
showTimePicker(
context: context,
initialTime:
TimeOfDay.now())
.then((value) {
setState(() {
_time = value!;
});
});
if (date != null) {
ChosenTime =
"Chosen time: " +
date!.day
.toString() +
":" +
date!.month
.toString() +
":" +
date!.year
.toString() +
", time: " + _time.minute.toString() +
":" +
_time.hour
.toString();
}
},
child: Text("Enter the time"))),
Padding(padding: EdgeInsets.all(10)),
],
),
Padding(padding: EdgeInsets.all(10)),
Text(ChosenTime)
]),
));
});
},
),
),
),
Padding(padding: EdgeInsets.fromLTRB(10, 0, 0, 0)),
Expanded(
flex: 4,
child: Container(
height: 75,
child: ElevatedButton(
onPressed: () {},
child: Center(
child: Text("Delete Remind"),
)),
),
)
],
);
}
if(Counter < RemindTitle.length){
Counter++;
}
return Container(
height: 130,
width: 175,
child: ElevatedButton(
onPressed: () {},
child: Center(
child: Column(
children: [
Padding(padding: EdgeInsets.fromLTRB(0, 10, 0, 10)),
Text(
RemindTitle[Counter],
style: TextStyle(fontSize: 20),
),
Padding(padding: EdgeInsets.fromLTRB(0, 10, 0, 10)),
Text(RemindText[Counter])
],
),
)),
padding: EdgeInsets.fromLTRB(0, 20, 0, 0),
);
}),
appBar: AppBar(
title: (Text("Reminder :)")),
centerTitle: true,
),
);
}
}
This is because your remindTitle length is 5 and you are trying to access the 6th item which is not available. And Time is also showing correctly. But you are displaying minutes at first and the hour which is in 24 hours format and setState is not working correctly inside the model, show whatever time you choose, it is always the current time when you press the button.
Hope this solves your problem.
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
debugShowCheckedModeBanner: false, home: const HomePage());
}
}
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
List<String> remindTitle = ["First", "Second", "Third", "Fourth", "sdf"],
remindText = ["TexttF", "TextS", "TextT", "TextFr", "sdf"];
int counter = -1;
DateTime? date;
TimeOfDay _time = TimeOfDay.now();
String dateText = "Enter the date", chosenTime = "";
bool alreadyBuilt = false;
showDateTime() {
if (date != null) {
int hour = _time.hour > 12
? _time.hour - 12
: _time.hour;
String meridian =
_time.hour > 12
? "PM"
: "AM";
chosenTime =
"Chosen Date: ${date!.day}:${date!.month < 10 ? '0' : ''}${date!.month}:${date!.year}, time: ${hour < 10 ? '0' : ''}$hour:${_time.minute} $meridian";
setState((){});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
Container(
padding: const EdgeInsets.fromLTRB(20, 20, 20, 0),
height: 100,
child: Row(
children: [
Expanded(
flex: 4,
child: SizedBox(
height: 75,
child: ElevatedButton(
child: const Center(
child: Text("Add Remind"),
),
onPressed: () {
showModalBottomSheet<void>(
context: context,
builder: (BuildContext context) {
return Center(
child: Column(children: [
Row(
children: [
const Padding(
padding: EdgeInsets.all(10)),
Expanded(
child: ElevatedButton(
onPressed: () async {
date = await showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(2015),
lastDate: DateTime(2030),
);
showDateTime();
},
child: const Text(
"Enter the date"))),
const Padding(
padding: EdgeInsets.all(10)),
Expanded(
child: ElevatedButton(
onPressed: () async {
showTimePicker(
context: context,
initialTime:
TimeOfDay.now())
.then((value) {
_time = value!;
showDateTime();
});
},
child: const Text(
"Enter the time"))),
const Padding(
padding: EdgeInsets.all(10)),
],
),
const Padding(padding: EdgeInsets.all(10)),
Text(chosenTime)
]),
);
});
},
),
),
),
const Padding(padding: EdgeInsets.fromLTRB(10, 0, 0, 0)),
Expanded(
flex: 4,
child: SizedBox(
height: 75,
child: ElevatedButton(
onPressed: () {},
child: const Center(
child: Text("Delete Remind"),
)),
),
)
],
),
),
Expanded(
flex: 1,
child: ListView.builder(
padding: const EdgeInsets.all(20),
itemCount: remindTitle.length,
itemBuilder: (BuildContext context, int index) {
return Container(
height: 130,
width: 175,
padding: const EdgeInsets.fromLTRB(0, 20, 0, 0),
child: ElevatedButton(
onPressed: () {},
child: Center(
child: Column(
children: [
const Padding(
padding: EdgeInsets.fromLTRB(0, 10, 0, 10)),
Text(
remindTitle[index],
style: const TextStyle(fontSize: 20),
),
const Padding(
padding: EdgeInsets.fromLTRB(0, 10, 0, 10)),
Text(remindText[index])
],
),
)),
);
}),
),
],
),
appBar: AppBar(
title: (const Text("Reminder :)")),
centerTitle: true,
),
);
}
}