So I'm picking up flutter, and slowly but surely starting to learn it but am still very new to it at the moment. I've created a Material Button in one class (Named LoginButton), representing a login button, and want to utilize it in another class that creates the actual page for the login button to be used on. I am however struggling with placing said button into the scaffold I'm using on this page.
What do I need to do to link to another file with my button widgets? My understanding from the following post was that using LoginButton(), would be fine, however i'm getting an error saying "LoginButton isn't a function. Try correcting the name to match an existing function, or define a method or function named 'LoginButton'." Any help with this would be greatly appreciated. I've marked the line causing the error, its at the bottom of MyHomePage.dart
LoginButton.dart
class LoginButton extends StatelessWidget {
static const String routeName = '/LoginButton';
@override
Widget build(BuildContext context) {
final TextEditingController email = TextEditingController();
final TextEditingController password = TextEditingController();
return Material(
elevation: 5.0,
borderRadius: BorderRadius.circular(30.0),
color: Colors.black,
child: MaterialButton(
minWidth: MediaQuery.of(context).size.width,
padding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
textColor: Colors.white,
child: Text("Login"),
onPressed: () async {
var result = await Authenticator()
.signIn(email: email.text, password: password.text);
if (result) {
Navigator.push(context, MaterialPageRoute<void>(
builder: (BuildContext context) {
// 1. make this a page, make a class for login and extend it to register
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.black,
title: Text("HomeChef")),
drawer: Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: const <Widget>[
DrawerHeader(
decoration: BoxDecoration(
color: Colors.black,
),
child: Text(
'My Account',
style: TextStyle(
color: Colors.white,
fontSize: 24,
),
),
),
ListTile(
leading: Icon(Icons.person_outline_sharp),
title: Text('Profile'),
),
ListTile(
leading: Icon(Icons.favorite_border),
title: Text("Liked Recipies"),
),
ListTile(
leading: Icon(Icons.play_circle_outline),
title: Text('Playlists'),
)
],
)));
},
));
}
}));
}
}
MyHomePage.dart
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
FirebaseAuth auth = FirebaseAuth.instance;
TextStyle style = TextStyle(fontFamily: 'Montserrat', fontSize: 20.0);
@override
Widget build(BuildContext context) {
final emailField = TextField(
obscureText: false,
controller: email,
style: style,
decoration: InputDecoration(
contentPadding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
hintText: "Email",
border:
OutlineInputBorder(borderRadius: BorderRadius.circular(32.0))),
);
// Text field for The first name of a User when registering
// Text field for a password when Registering/ Logging in
final passwordField = TextField(
obscureText: true,
style: style,
controller: password,
decoration: InputDecoration(
contentPadding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
hintText: "Password",
border:
OutlineInputBorder(borderRadius: BorderRadius.circular(32.0))),
);
final signUpButon = Material(
elevation: 5.0,
borderRadius: BorderRadius.circular(30.0),
color: Colors.black,
child: MaterialButton(
minWidth: MediaQuery.of(context).size.width,
padding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
textColor: Colors.white,
child: Text("Sign Up"),
onPressed: () {
Navigator.pop(context); // returning user to login page
}));
final firstNameField = TextField(
obscureText: false,
style: style,
decoration: InputDecoration(
contentPadding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
hintText: "First Name",
border:
OutlineInputBorder(borderRadius: BorderRadius.circular(32.0))),
);
// Text field for the last name of a User when registering
final lastNameField = TextField(
obscureText: false,
style: style,
decoration: InputDecoration(
contentPadding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
hintText: "Last Name",
border:
OutlineInputBorder(borderRadius: BorderRadius.circular(32.0))),
);
final registerButon = Material(
elevation: 5.0,
borderRadius: BorderRadius.circular(30.0),
color: Colors.black,
child: MaterialButton(
minWidth: MediaQuery.of(context).size.width,
padding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
textColor: Colors.white,
child: Text("Create An Account"),
onPressed: () {
// Changing routes
Navigator.push(context, MaterialPageRoute<void>(
builder: (BuildContext context) {
return Scaffold(
body: Center(
child: Container(
color: Colors.white,
child: Padding(
padding: const EdgeInsets.all(36.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
SizedBox(height: 45.0),
firstNameField,
SizedBox(
height: 25.0,
),
lastNameField,
SizedBox(height: 25.0),
emailField,
SizedBox(height: 25.0),
passwordField,
SizedBox(height: 25.0),
signUpButon,
],
),
),
)));
},
));
}));
// Setting scene for intial Route
// Button for Users to create a new account
// Upon clicking, will switch routes to a new Route
// with new fields
return Scaffold(
body: Center(
child: Container(
color: Colors.white,
child: Padding(
padding: const EdgeInsets.all(36.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
SizedBox(height: 45.0),
emailField,
SizedBox(height: 25.0),
passwordField,
SizedBox(height: 35.0),
LoginButton(), // This is the line in question causing the error
SizedBox(height: 35.0),
registerButon,
SizedBox(height: 15.0),
],
),
),
)));
}
final TextEditingController email = TextEditingController();
final TextEditingController password = TextEditingController();
}