РЕДАКТИРОВАТЬ: Вот целый пример для ОП.
Если это не решит вашу проблему, я бы предложил немного яснее с вашими намерениями.
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
final Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: LoginWidget(),
),
),
);
}
}
class LoginWidget extends StatelessWidget {
final TextEditingController _usernameController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();
final _formKey = GlobalKey<FormState>();
// Simple password & email validators, returns text if password is empty, if not returns null
String _validateemail(String value) {
if (value.isEmpty) {
return "email must not be empty";
}
return null;
}
String _validatepassword(String value) {
if (value.isEmpty) {
return "password must not be empty";
}
return null;
}
final bool validated = true;
@override
Widget build(BuildContext context) {
return Stack(
children: <Widget>[
Form(
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextFormField(
validator: _validateemail,
controller: _usernameController,
decoration: InputDecoration(labelText: 'Username'),
),
TextFormField(
validator: _validatepassword,
controller: _passwordController,
decoration: InputDecoration(labelText: 'Password'),
),
],
),
),
Align(
alignment: Alignment.bottomCenter,
child: MaterialButton(
height: 70,
minWidth: MediaQuery.of(context).size.width,
onPressed: () {
if (_formKey.currentState.validate()) {
// checks if the username and password match the following conditions and if they do, navigate to next page.
if (_passwordController.text == "password" &&
_usernameController.text == "example") {
SystemChannels.textInput.invokeMethod('TextInput.hide');
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SuccessPage()),
);
}
// prints the following if the controllers' text do not match the preceding condition
print("Invalid username or password entered. You entered " + _usernameController.text + " and " + _passwordController.text);
} else {
print(_usernameController.text);
print(_passwordController.text);
}
},
color: Colors.white,
child: Text(
"LOGIN",
style: TextStyle(color: Colors.blueGrey),
),
),
)
],
);
}
}
class SuccessPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Working! Navigated from login page."),
Padding(
padding: const EdgeInsets.only(top: 40),
child: MaterialButton(
color: Colors.white,
child: Text(
"Go back",
style: TextStyle(color: Colors.blueGrey),
),
onPressed: () {
Navigator.pop(context);
},
),
)
],
),
),
);
}
}