I have created an AppBar on a separate page. So, that I can include it wherever required. This App Bar contains a popup menu. I want if the user selects items from the menu then it will redirect to that page.
AppBar.dart
import 'package:flutter/material.dart';
import 'package:fnapp/home_screen.dart';
class Choice {
const Choice({this.title, this.icon});
final String title;
final IconData icon;
}
const List<Choice> choices = const <Choice>[
const Choice(title: 'Car', icon: Icons.directions_car),
const Choice(title: 'Bicycle', icon: Icons.directions_bike),
const Choice(title: 'Boat', icon: Icons.directions_boat),
const Choice(title: 'Bus', icon: Icons.directions_bus),
const Choice(title: 'Train', icon: Icons.directions_railway),
const Choice(title: 'Walk', icon: Icons.directions_walk),
];
class BaseAppBar extends StatelessWidget with PreferredSizeWidget {
final Color backgroundColor = Colors.red;
@override
Widget build(BuildContext context) {
return AppBar(
backgroundColor: Colors.transparent,
elevation: 0.0,
leading: BackButton(
color: Colors.black87,
onPressed: (){// onPressed: () => Navigator.of(context).pop(),
print('back pressed');
},
),
actions: <Widget>[
PopupMenuButton<Choice>(
icon: Icon(
Icons.more_vert,
color: Colors.black,
),
onSelected: _select,
itemBuilder: (BuildContext context) {
return choices.skip(0).map((Choice choice) {
return PopupMenuItem<Choice>(
value: choice,
child: Text(choice.title)
);
}).toList();
},
),
],
);
}
@override
// Size get preferredSize => new Size.fromHeight(appBar.preferredSize.height);
Size get preferredSize => Size.fromHeight(kToolbarHeight);
}
void _select(Choice choice) {
if(choice.title == 'Bus'){
print('Bus');
}else if(choice.title == 'Car'){
// print('Car');
_navigateToHome();
}
}
_navigateToHome() Plan is using this Function to basically redirect it on another page.
void _navigateToHome(){
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (BuildContext context) => HomeScreen()
)
);
}
HomeScreen() page is added in AppBar.dart file.
Profile.dart
import 'dart:math';
import 'package:fnapp/appbar.dart';
import 'package:flutter/material.dart';
import 'package:fnapp/util/data.dart';
import 'package:flutter/services.dart';
import 'package:fnapp/home_screen.dart';
class OthersProfile extends StatefulWidget {
@override
_ProfileState createState() => _ProfileState();
}
class _ProfileState extends State<OthersProfile> {
@override
void _navigateToHome(){
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (BuildContext context) => HomeScreen()
)
);
}
int currentIndex = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: BaseAppBar(),
body:----some long codes here----
),
},
},
I tried to using _navigateToHome() function to inside/outside the Scaffold/Widget Build even in both files (AppBar and Profile). But it is giving me below error.
Compiler message: lib/appbar.dart:66:6: Error: Method not found: '_navigateToHome'. _navigateToHome();
Compiler message: lib/appbar.dart:88:18: Error: Getter not found: 'context'. Navigator.of(context).pushReplacement(
How can I fix this issue?
Edit
After suggestions, I have removed underscore from function. But it is still giving me an error.
I removed _ from both files. I removed from AppBar under the _Select function. But it is not working giving the same error. Also, I tried to move Select function from AppBar to Profile but it is also giving error.
I think similar issue was raised in GitHub by the user. https://github.com/flutter/flutter/issues/21728