我真的是用flutter新建的,我试图用带有登录和注册标签的标签栏构建登录页面。在具有使用2个小部件作为子项的标签栏im的LoginPage中,问题是当我尝试仅使用Login小部件登录时它完全正常且可以正常工作,但是当我在标签条中使用im时我总是会收到错误消息,登录窗口小部件中的文本字段始终返回null值,发生什么情况
im使用md2_tab_indicator版本1.0.2
这是登录页面:
class AuthScreen extends StatefulWidget {
@override
_AuthScreenState createState() => _AuthScreenState();
}
class _AuthScreenState extends State<AuthScreen> {
@override
Widget build(BuildContext context) {
UIHelper.init(context);
return Scaffold(
body: SafeArea(
child: DefaultTabController(
length: 2,
child: Container(
color: Colors.orange[50],
child: ListView(
shrinkWrap: true,
children: <Widget>[
Container(
padding: EdgeInsets.only(
right: 20.0, left: 20.0, top: 100.0, bottom: 20.0),
child: Image(
image: AssetImage('images/fare.gi.png'),
height: 150.0,
),
),
Center(
child: TabBar(
labelStyle: TextStyle(
//up to your taste
fontSize: 18,
fontWeight: FontWeight.w700),
indicatorSize:
TabBarIndicatorSize.label,
labelColor: Colors.red[400],
unselectedLabelColor: Color(0xff5f6368),
isScrollable: true, //up to your taste
indicator: MD2Indicator(
//it begins here
indicatorHeight: 3,
indicatorColor: Colors.red[400],
indicatorSize: MD2IndicatorSize
.full //3 different modes tiny-normal-full
),
tabs: <Widget>[
Tab(
text: "Login",
),
Tab(
text: "Register",
),
],
),
),
SizedBox(
height: 40,
),
Container(
height: MediaQuery.of(context).size.height,
child: TabBarView(
children: <Widget>[LoginScreen(), RegisterScreen()],
),
)
],
),
)),
),
);
}
}
这是登录小部件
class LoginScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
bool _isLoading = false;
Api api = Api();
String email;
String password;
return Material(
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Colors.orange[50], Colors.red[400]],
begin: Alignment.topCenter,
end: Alignment.bottomCenter),
),
child: _isLoading
? Center(child: CircularProgressIndicator())
: ListView(
children: <Widget>[
Container(
width: MediaQuery.of(context).size.width,
padding: EdgeInsets.symmetric(horizontal: 60.0),
child: Column(
children: <Widget>[
FloatingInputBox(
'Email',
onChanged: (value) => email = value,
),
SizedBox(
height: 20.0,
),
FloatingInputBox(
'Password',
onChanged: (value) => password = value,
obscureText: true,
),
],
),
),
ButtonSection(
label: 'Login',
onPressed: () async {
print('username: $email');
print('password: $password');
Result result = await api.login(email, password);
if (result.resultType == ResultType.Success)
Navigator.push(context,
MaterialPageRoute(builder: (_) => Home()));
}),
],
),
),
);
}
}
答案 0 :(得分:0)
原因是您将变量放在Widget build
下,当Widget
build
时,所有变量都将重置
来自
Widget build(BuildContext context) {
bool _isLoading = false;
Api api = Api();
String email;
String password;
到
bool _isLoading = false;
Api api = Api();
String email;
String password;
Widget build(BuildContext context) {