我是 Flutter 的新手,所以很明顯我在代碼中的某個地方搞砸了,我無法弄清楚。基本上我有一個登錄螢屏,我想從中導航到第二個螢屏,但是當我選擇按鈕時,絕對沒有任何反應,也沒有出現錯誤。我一直在尋找答案,但沒有一個可以解決問題。我希望有一個人可以幫助我。(按鈕的文字是西班牙語)。
這是 main.dart:
import 'package:flutter/material.dart';
import 'package:go_out/screens/screens.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
initialRoute: 'home',
routes: {
'home': (_) => const HomeScreen(),
'signInEmail': (_) => const SignInEmail(),
},
);
}
}
這是 home_screen.dart:
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import '../widgets/widgets.dart';
class HomeScreen extends StatelessWidget {
const HomeScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const Scaffold(
backgroundColor: Colors.black,
body: Padding(
padding: EdgeInsets.only(left: 15, right: 15),
child: Center(
child: _SignInPageDesign(),
),
),
);
}
}
class _SignInPageDesign extends StatelessWidget {
const _SignInPageDesign({
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(left: 10, right: 10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.asset(
'images/logo.png',
height: 200,
fit: BoxFit.cover,
),
const SizedBox(
height: 30,
),
CustomElevatedButtonWithoutIcon(
text: 'Crear Cuenta',
onPressed: () => Navigator.pushNamed(context, 'signInEmail'),
),
const SizedBox(height: 12),
CustomElevatedButtonWithIcon(
text: 'Continuar con Facebook',
onPressed: () {},
icon: FontAwesomeIcons.facebookSquare,
),
const SizedBox(height: 12),
CustomElevatedButtonWithIcon(
text: 'Continuar con Google',
onPressed: () {},
icon: FontAwesomeIcons.google,
),
const SizedBox(height: 12),
CustomElevatedButtonWithIcon(
text: 'Continuar con Apple',
onPressed: () {},
icon: FontAwesomeIcons.apple,
),
const SizedBox(height: 12),
const CustomTextButton(
text: 'Iniciar sesión',
),
const SizedBox(height: 12),
CustomElevatedButtonWithoutIcon(
text: 'Registrar comercio',
onPressed: () {},
),
],
),
);
}
}
這是 home_screen.dart 的問題,它不回傳任何值。
CustomElevatedButtonWithoutIcon(
text: 'Crear Cuenta',
onPressed: () => Navigator.pushNamed(context, 'signInEmail'),
),
這是我要轉到 sign_in_email.dart 的第二個螢屏:
import 'package:flutter/material.dart';
class SignInEmail extends StatelessWidget {
const SignInEmail({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: const Color.fromRGBO(254, 80, 0, 1),
),
);
}
}
這是 CustomElevatedButtonWithoutIcon 類:
import 'package:flutter/material.dart';
class CustomElevatedButtonWithoutIcon extends StatelessWidget {
const CustomElevatedButtonWithoutIcon({
Key? key,
required this.text,
required this.onPressed,
}) : super(key: key);
final String text;
final VoidCallback onPressed;
@override
Widget build(BuildContext context) {
return ElevatedButton(
child: Text(
text,
style:
const TextStyle(color: Colors.black, fontWeight: FontWeight.bold),
),
onPressed: () {},
style: ButtonStyle(
fixedSize: MaterialStateProperty.all<Size>(const Size(0, 50)),
backgroundColor: MaterialStateProperty.all<Color>(
const Color.fromRGBO(254, 80, 0, 1)),
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0),
),
),
),
);
}
}
uj5u.com熱心網友回復:
你沒有傳遞任何 onPressed 函式,你將它保持為空,所以沒有任何作業,只需在 ElevatedButton 中傳遞你的 onPressed 函式,如下所示
import 'package:flutter/material.dart';
class CustomElevatedButtonWithoutIcon extends StatelessWidget {
const CustomElevatedButtonWithoutIcon({
Key? key,
required this.text,
required this.onPressed,
}) : super(key: key);
final String text;
final VoidCallback onPressed;
@override
Widget build(BuildContext context) {
return ElevatedButton(
child: Text(
text,
style:
const TextStyle(color: Colors.black, fontWeight: FontWeight.bold),
),
onPressed: onPressed,
style: ButtonStyle(
fixedSize: MaterialStateProperty.all<Size>(const Size(0, 50)),
backgroundColor: MaterialStateProperty.all<Color>(
const Color.fromRGBO(254, 80, 0, 1)),
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0),
),
),
),
);
}
}
最好還是使用 RouteGenerator,使用下面的代碼創建一個 route_generator.dart 檔案
import 'package:flutter/material.dart';
class RouterCustom {
static Route<dynamic> generateRoute(RouteSettings settings) {
final args = settings.arguments;
switch (settings.name) {
case "home":
return MaterialPageRoute(
builder: (_) => const HomeScreen(),
settings: const RouteSettings(name: "home"),
);
case "signInEmail":
return MaterialPageRoute(
builder: (_) => const SignInEmail(),
settings: const RouteSettings(name: "signInEmail"),
);
default:
return MaterialPageRoute(
builder: (_) => Scaffold(
body: Center(
child: Text('No route defined for ${settings.name}'),
),
),
settings: const RouteSettings(name: "404"),
);
}
}
}
然后將其傳遞給您的主要方法的路由器引數
MaterialApp(
initialRoute: "home",
onGenerateRoute: RouterCustom.generateRoute,
)
uj5u.com熱心網友回復:
CustomElevatedButtonWithoutIcon 在按下時實際上并沒有做任何事情,onPressed: () {},應該是onPressed: onPressed,.
檢測這一點的一種簡單方法是在導航之前添加列印陳述句,或添加斷點。如果兩者都未觸發,則不會呼叫該函式,并且您的問題不在導航中。
uj5u.com熱心網友回復:
您的小部件CustomElevatedButtonWithoutIcon在被按下后沒有呼叫任何函式
你有這個
onPressed: () {},
改變它
onPressed: onPressed
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/444172.html
上一篇:即使在功能->位置->自定義位置中設定自定義位置后,ios模擬器上的“位置權限被拒絕”
下一篇:NativeScript8.2:找不到名稱CLLocationManager、CLLocationManagerDelegate
