I have a root class RootPage
which is a StatefulWidget
which is always in view. I would like to change the body
in RootPage
This was a tricky question but I got a solution for this question I to faced this problem so many times. So let's first solve your problem. And this is the solution for your code
Lets first save this file with a name feedPage.dart
`import 'package:flutter/material.dart';
class FeedPage extends StatefulWidget {
@override
_feedPageState createState() => new _feedPageState();
}
class _feedPageState extends State {
@override
Widget build(BuildContext context) {
return new FlatButton(
onPressed: () {
setState(() {
//change the currentPage in RootPage so it switches FeedPage away and gets a new class that I'll make
});
},
child: new Text('Go to a new page but keep root, just replace this feed part'),
);
}
}`
And then import that file into your main file
`
import 'package:flutter/material.dart';
import 'feedPage.dart'
class RootPage extends StatefulWidget {
@override
_RootPageState createState() => new _RootPageState();
}
class _RootPageState extends State {
FeedPage feedPage;
Widget currentPage;
@override
void initState() {
super.initState();
feedPage = FeedPage();
currentPage = feedPage;
}
@override
Widget build(BuildContext context) {
return new Scaffold(
//Current page to be changed from other classes too?
body: currentPage
);
}
}`
In flutter you cant write multiple Stateful widgets in one dart file you have to create that as a widget and use it in your code as setState is an attribute of a Stateful widget we have write this code in an diffrent file
Problem Solved......:)