I have a dart file with IndexedStack and the following function in the same file to change the stacks.
The file with method as follows-
class RootApp extends StatefulWidget with selectedTab {
@override
_RootAppState createState() => _RootAppState();
}
class _RootAppState extends State<RootApp> {
int pageIndex = 0;
List<Widget> pages = [
......
];
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
....
return AnimatedBottomNavigationBar(
.......
onTap: (index) {
selectedTab(index);
},
);
}
selectedTab(index) {
setState(() {
pageIndex = index;
});
}
}
The other file is as follows---
class CreatBudgetPage extends StatefulWidget {
@override
_CreatBudgetPageState createState() => _CreatBudgetPageState();
}
class _CreatBudgetPageState extends State<CreatBudgetPage> {
.......
FirebaseFirestore.instance
.collection('expenses/' userId '/' todayDate)
.add({
....
}).then((_) {
print("collection created");
void rootApp() => selectedTab(0);
}).catchError((error) {
print(error);
});
}
How can i call this function from another dart file?
Edit: I tried the methods suggested by Rohith but they aren't working. So i have updated my dart file.
CodePudding user response:
You can import the file in which your function is in for example
import "IndexedTabs.dart";
void main() => selectedTab(index)
or you can use mixin
like this
mixin SelectedTab {
selectedTab(index) {
setState(() {
pageIndex = index;
});
}
}
In main file
class Main with selectedTab {
void main() => selectedTab(index);
}
Update
This is in your main file
class RootApp extends StatefulWidget {
@override
_RootAppState createState() => _RootAppState();
}
class RootApp extends StatefulWidget {
@override
_RootAppState createState() => _RootAppState();
}
class _RootAppState extends State<RootApp> {
int pageIndex = 0;
List<Widget> pages = [
];
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
return BottomNavigationBar(
items: [],
onTap: (index) {
setState(() {
selectedTab(pageIndex, index);
});
},
);
}
}
This is the file with your function
void selectedTab(pageIndex, index) {
pageIndex = index;
}
CodePudding user response:
just import the file then you can freely call any function or class in that file :
import 'yourfile.dart'
yourmethode();