我有這樣的頁面結構:
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'First App',
theme: ThemeData(
primaryColor: appPrimaryColour,
scaffoldBackgroundColor: appBackGroupCoulour,
),
home: const WelcomePage(),
);
class WelcomePage extends StatelessWidget {
const WelcomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Body(),
);
}
}
歡迎頁面正文:
//Body for the welcome page
class Body extends StatelessWidget {
const Body({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return Background(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
"Welcome To My First App",
style: TextStyle(fontWeight: FontWeight.bold),
),
SizedBox(height: size.height * 0.05),
SizedBox(height: size.height * 0.05),
RoundedButton(
text: "LOGIN",
color: appPrimaryColour,
onPressed: () => {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => const LoginPage(),
),
),
},
),
],
),
),
);
}
}
登錄頁面通過登錄頁面的正文小部件
class LoginPage extends StatelessWidget {
const LoginPage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Body(),
);
}
}
登錄正文:
//Body for login
class Body extends StatelessWidget {
const Body({
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return Background(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
"LOGIN",
style: TextStyle(fontWeight: FontWeight.bold),
),
SizedBox(height: size.height * 0.03),
RoundedInputField(
hintText: "Enter email address",
icon: const Icon(
Icons.email,
),
labelText: "Email",
onChanged: (value) {},
),
RoundedPasswordField(
hintText: "Enter password",
labelText: "Password",
icon: const Icon(
Icons.lock,
),
suffixIcon: const Icon(
Icons.visibility,
color: appPrimaryColour,
),
onChanged: (value) {},
),
RoundedButton(
text: "LOGIN",
onPressed: () {},
),
SizedBox(height: size.height * 0.03),
AlreadyHaveAnAccountCheck(
onTaped: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const SignUpPage(),
),
);
},
),
],
),
),
);
}
}
這是一個可重用的小部件,用于顯示背景影像或任何其他內容:
class Background extends StatelessWidget {
final Widget child;
const Background({
Key? key,
required this.child,
}) : super(key: key);
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return Container(
height: size.height,
width: double.infinity,
child: Stack(
alignment: Alignment.center,
children: <Widget>[
child,
],
),
);
}
}
class RoundedButton extends StatelessWidget {
final String text;
final Function onPressed;
final Color color, textColor;
const RoundedButton({
Key? key,
required this.text,
required this.onPressed,
this.color = appPrimaryColour,
this.textColor = Colors.white,
}) : super(key: key);
@override
Widget build(BuildContext context) {
var size = MediaQuery.of(context).size;
return Container(
margin: const EdgeInsets.symmetric(vertical: 10),
width: size.width * 0.8,
child: ClipRRect(
borderRadius: BorderRadius.circular(30),
child: newElevatedButton(),
),
);
}
//Used:ElevatedButton as FlatButton is deprecated.
//Here we have to apply customizations to Button by inheriting the styleFrom
Widget newElevatedButton() {
return ElevatedButton(
child: Text(
text,
style: TextStyle(color: textColor),
),
onPressed: () => onPressed,
style: ElevatedButton.styleFrom(
primary: color,
padding: const EdgeInsets.symmetric(horizontal: 40, vertical: 20),
textStyle: TextStyle(
color: textColor, fontSize: 14, fontWeight: FontWeight.w500)),
);
}
}
嘗試導航到登錄頁面時,沒有任何反應。有人可以幫我嗎?我現在已經沒有想法了,我現在看了太多教程。
uj5u.com熱心網友回復:
你正在onPressed: () => onPressed做newElevatedButton()。它正在創建另一個匿名方法,但在其中,您沒有呼叫onPressed().
你可以做
onPressed: onPressed,
或者
onPressed: () => onPressed()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/478457.html
