Reputation: 308
I am very new to using Flutter and can't figure out how to include a calendar on a widget I am using as the 'second route'.
class FirstRoute extends StatefulWidget {
FirstRoute({Key key, this.title}) : super(key: key);
final String title;
@override
_FirstRoute createState() => _FirstRoute();
}
class _FirstRoute extends State<FirstRoute> {
CalendarController _controller;
@override
void initState(){
super.initState();
_controller = CalendarController();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Project'),
),
body: new Center(
child: new ListView(
children: <Widget>[
TableCalendar(calendarController: _controller,)
...
Currently, this works to show the calendar on the first page but I am wanting it on the second page which I have as:
class SecondRoute extends StatelessWidget {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Second Route"),
),
body: SingleChildScrollView(
child: new ListView(
children: <Widget>[
...
And navigate to using:
...
ListTile(
title: Text('Calendar'),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondRoute())
);
},
),
...
Is there a way of using the TableCalendar(calendarController: _controller,)
in the second route? I tried to add of the calendar code into the second route but this does not work as you can't use the initState()
in a widget. Thank you!
Upvotes: 0
Views: 187
Reputation: 4773
In you want to use initState in the SecondRoute page, it must extend StatefulWidget as following
class SecondRoute extends StatefulWidget {
@override
_SecondRoute createState() => _SecondRoute();
}
class _SecondRoute extends State<SecondRoute> {
CalendarController _controller;
@override
void initState(){
super.initState();
_controller = CalendarController();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Second Route"), ),
body: ListView(
children: [
TableCalendar(calendarController: _controller,)
],
),
),
}
}
Upvotes: 1