Android 颤振:-我需要在Navigator.push()方法之后从方法返回一个小部件
我已经使用Flutter和BLOC架构制作了一个登录应用程序。如果登录成功,我需要将一个小部件返回到scaffold,还需要使用Navigator.push导航到下一个屏幕 我尝试过以下方法:- 方法1:- 但它会导致一个错误,即当切换到下一个屏幕时,无法设置小部件 方法2:- 但它会导致未来的代码每1.2秒运行一次 3.方法3:-Android 颤振:-我需要在Navigator.push()方法之后从方法返回一个小部件,android,flutter,dart,Android,Flutter,Dart,我已经使用Flutter和BLOC架构制作了一个登录应用程序。如果登录成功,我需要将一个小部件返回到scaffold,还需要使用Navigator.push导航到下一个屏幕 我尝试过以下方法:- 方法1:- 但它会导致一个错误,即当切换到下一个屏幕时,无法设置小部件 方法2:- 但它会导致未来的代码每1.2秒运行一次 3.方法3:- var cancellableCompleter = CancelableCompleter(onCancel:(){print("onCancel");});
var cancellableCompleter = CancelableCompleter(onCancel:(){print("onCancel");});
cancellableCompleter.complete(
//Future.value("future result"));
Future.delayed(
Duration(days: 1200),
() => {
print("i am here"),
Navigator.push(context,
MaterialPageRoute(builder: (context) => DashboardPage())),
}));
cancellableCompleter.operation.value.then((value)=>print(value));
cancellableCompleter.operation.value.whenComplete(()=>{print("Completed")});
return ButtonWidget();
但是它没有实例化Navigator.push方法。即使是我在这里也不会被打印出来
我也尝试过CancelableOperation,但它给出了与方法2相同的错误
完整代码
首先在登录正文上设置小部件,然后设置状态调用,然后是Navigator.push。第二种解决方案用于StreamBuilder进行不带设置状态的更新设计。首先在登录正文上设置小部件,然后设置状态调用,然后是Navigator.push。第二种解决方案用于StreamBuilder进行不带设置状态的更新设计。使用布洛克斯腾纳
return BlocListener<LoginBloc, LoginState>(
listener: (context, state) {
if (state is loginSuccess) {
Navigator.push(context,MaterialPageRoute(builder: (context) => DashboardPage()));
}
},
child: ButtonWidget(),
)
使用BlocListener
return BlocListener<LoginBloc, LoginState>(
listener: (context, state) {
if (state is loginSuccess) {
Navigator.push(context,MaterialPageRoute(builder: (context) => DashboardPage()));
}
},
child: ButtonWidget(),
)
我需要将一个小部件返回到脚手架上——你这到底是什么意思?您想调用push/pushNamed并转到其他屏幕,那么return在这里是什么意思呢?实际上,我正在使用flatter_bloc来实现bloc架构,因此我有3个状态和3个相应的方法:-1_buildInitialState:-其中该方法仅返回一个登录按钮小部件。2. _buildLoadingState:-其中该方法返回一个CircularProgressingIndicator 3_buildLoadedState:-在这里我必须转到另一个屏幕,然后我必须再次返回一个按钮。我只是发布整个代码,以防您不理解。我需要将一个小部件返回脚手架-您真正的意思是什么?您想调用push/pushNamed并转到其他屏幕,那么return在这里是什么意思呢?实际上,我正在使用flatter_bloc来实现bloc架构,因此我有3个状态和3个相应的方法:-1_buildInitialState:-其中该方法仅返回一个登录按钮小部件。2. _buildLoadingState:-其中该方法返回一个CircularProgressingIndicator 3_buildLoadedState:-我必须转到另一个屏幕,然后我必须再次返回一个按钮。我只是发布整个代码,以防您不理解。我以为您没有理解我的问题。我已附上完整的代码。请再看一遍。我以为你没有明白我的问题。我已附上完整的代码。请检查一下。
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:sales_app/bloc/bloc.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:async/async.dart';
import 'dashboard_page.dart';
class LoginPage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _LoginPageState();
}
}
class _LoginPageState extends State<LoginPage> {
String _email;
String _password;
LoginBloc bloc = LoginBloc();
final blue_color = Color(0xff4490E8);
final formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return Scaffold(
body: BlocProvider(
builder: (context) => bloc,
child: Container(
decoration: BoxDecoration(color: blue_color),
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Form(
key: formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(
"Log In",
style: TextStyle(color: Colors.white, fontSize: 30),
),
SizedBox(
height: 50,
),
TextFormField(
autovalidate: true,
style: TextStyle(color: blue_color),
onSaved: (email) => {this._email = email},
keyboardType: TextInputType.emailAddress,
initialValue: "saurav.vidyarthi@connexrm.com",
validator: (val) =>
RegExp(r"^[a-zA-Z0-9.]+@[a-zA-Z0-9]+\.[a-zA-Z]+")
.hasMatch(val)
? null
: "Invalid Email",
decoration: InputDecoration(
filled: true,
helperStyle: TextStyle(color: blue_color),
labelStyle: TextStyle(color: blue_color),
labelText: "Email Id",
hintText: "e.g. joe@gmail.com",
hintStyle: TextStyle(color: blue_color),
prefixIcon: Icon(Icons.email, color: blue_color),
fillColor: Colors.white,
/*border: OutlineInputBorder(
borderSide:
BorderSide(color: const Color(0xffffffff)))),*/
)),
SizedBox(height: 15.0),
TextFormField(
style: TextStyle(color: blue_color),
obscureText: true,
autovalidate: true,
initialValue: "Saurav@1",
onSaved: (password) => {_password = password},
validator: (val) => val.length < 6
? "Password must be of at least 6 char."
: null,
decoration: InputDecoration(
filled: true,
labelStyle: TextStyle(color: blue_color),
hintStyle: TextStyle(color: blue_color),
prefixIcon: Icon(Icons.lock, color: blue_color),
fillColor: Colors.white,
/* border: OutlineInputBorder(
borderSide:
BorderSide(color: const Color(0xffffffff))),*/
labelText: "Password",
hintText: "*******")),
SizedBox(height: 25.0),
BlocBuilder(
bloc: bloc,
builder:
(BuildContext buildContext, LoginState loginState) {
print(loginState.toString());
if (loginState is InitialLoginState)
return _buildInitialState();
else if (loginState is LoggingInState)
return _buildLoadingState();
else if (loginState is LoginSuccessState) {
LoginSuccessState state = loginState;
return _buildSuccessState(state.user.StatusText);
} else {
LoginFailedState state = loginState;
return _buildFailureState(state.user.StatusText);
}
},
)
],
),
),
),
),
),
);
}
void _onButtonPressed() {
if (formKey.currentState.validate()) {
formKey.currentState.save();
bloc.dispatch(GetUser(_email, _password));
} else {
Fluttertoast.showToast(msg: "Please enter a valid email and password");
}
}
Widget _buildInitialState() {
return RaisedButton(
onPressed: _onButtonPressed,
color: Colors.white,
child: Text(
"Log In",
style: TextStyle(color: blue_color),
));
}
Widget _buildLoadingState() {
return CircularProgressIndicator(backgroundColor: Colors.white);
}
Widget _buildFailureState(message) {
Fluttertoast.showToast(msg: message);
return RaisedButton(
onPressed: _onButtonPressed,
color: Colors.white,
child: Text(
"Log In",
style: TextStyle(color: blue_color),
));
}
Widget _buildSuccessState(message) {
Fluttertoast.showToast(msg: message);
_setAuthState();
var cancellableCompleter = CancelableCompleter(onCancel:(){print("onCancel");});
cancellableCompleter.complete(
//Future.value("future result"));
Future.delayed(
Duration(days: 1200),
() => {
print("i am here"),
Navigator.push(context,
MaterialPageRoute(builder: (context) => DashboardPage())),
}));
cancellableCompleter.operation.value.then((value)=>print(value));
cancellableCompleter.operation.value.whenComplete(()=>{print("Completed")});
return RaisedButton(
onPressed: _onButtonPressed,
color: Colors.white,
child: Text(
"Log In",
style: TextStyle(color: blue_color),
),
);
}
_setAuthState() async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
await sharedPreferences.setBool("isLoggedIn", true);
}
@override
void dispose() {
super.dispose();
bloc.dispose();
}
}
return BlocListener<LoginBloc, LoginState>(
listener: (context, state) {
if (state is loginSuccess) {
Navigator.push(context,MaterialPageRoute(builder: (context) => DashboardPage()));
}
},
child: ButtonWidget(),
)