I have this flutter code, for now its short and is still manageable, but i foresee that it will be very long in the future so i would like to split the code in separate files. This code contains a navigation rail that is on the left side of the application. I would like for the navigation rail part (Starting from the Row widget) of the code be transferred to another file
//*main.dart*//
class _MainAppState extends State<MainApp> {
int index = 0;
bool expanded = false;
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Stack(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(left: 72.0),
child: Column(
children: <Widget>[
Expanded(
child: SfPdfViewer.network(
"https://cdn.syncfusion.com/content/PDFViewer/flutter-succinctly.pdf",
),
),
],
),
),
Row(
children: <Widget>[
MouseRegion(
onEnter: (PointerEnterEvent event) {
expanded = true;
setState(() {});
},
onHover: (PointerHoverEvent event) {},
onExit: (PointerExitEvent event) {
expanded = false;
setState(() {});
},
child: NavigationRail(
selectedIndex: index,
extended: expanded,
destinations: const <NavigationRailDestination>[
NavigationRailDestination(
icon: Icon(Icons.one_k),
label: Text("one"),
),
NavigationRailDestination(
icon: Icon(Icons.two_k),
label: Text("two"),
),
NavigationRailDestination(
icon: Icon(Icons.three_k),
label: Text("three"),
),
],
onDestinationSelected: (int i) {
index = i;
setState(() {});
},
),
),
],
),
],
),
),
);
}
}
I attempted to create a function that returns the whole row widget, but i am faced with the problem that MouseRegion will loose access to expanded variable and setstate function
//*nav_rail.dart*//
Widget buildNavRail(BuildContext context){
return
Row(
children: <Widget>[
MouseRegion(
onEnter: (PointerEnterEvent event) {
expanded = true;
setState(() {});
},
onHover: (PointerHoverEvent event) {},
onExit: (PointerExitEvent event) {
expanded = false;
setState(() {});
},
child: NavigationRail(
selectedIndex: index,
extended: expanded,
destinations: const <NavigationRailDestination>[
NavigationRailDestination(
icon: Icon(Icons.one_k),
label: Text("one"),
),
NavigationRailDestination(
icon: Icon(Icons.two_k),
label: Text("two"),
),
NavigationRailDestination(
icon: Icon(Icons.three_k),
label: Text("three"),
),
],
onDestinationSelected: (int i) {
index = i;
setState(() {});
},
),
),
],
);
}
I have imported the file via import 'nav_rail.dart'
. What is the proper way to do it in flutter? If it cant be done is there something like a C/C++ #include where the preprocessor will just literally copy past the contents of the file to where #include was
2
Answers
By default, a different file is a different library, and namespacing becomes significant. Any identifiers with a leading underscore are visible only within the library in which they were defined.
Otherwise, yes, move your code to a different file, and use
import
to pull it in. Pretty simple.You can make some paramaters.
Here is a example:
Then u can use this widget like this:
You can make anything to paramater, like ur
destinations
, just make aList< NavigationRailDestination> destinations
as a new paramater.