TextFormField未出现在模拟器中

时间:2019-11-17 08:02:14

标签: flutter dart

我一直试图使TextFormField('email:')出现在模拟器中,但没有出现在模拟器中,如下所示:

enter image description here

但是,当我对其他任何东西进行更改时,它也会更改,因此模拟器不是问题。

这是我的login_page.dart文件:

import 'package:flutter/material.dart';

class LoginPage extends StatefulWidget{
  @override  
  State createState() => new LoginPageState();
}

class LoginPageState extends State<LoginPage>{
  @override 
  Widget build(BuildContext context){
    return new Scaffold(
      appBar: AppBar(
        title: new Text("SMART ID", textAlign: TextAlign.center, style: TextStyle(fontFamily: 'Open Sans', fontWeight: FontWeight.bold)),
        leading: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Image.asset(
            "assets/arrowPNG.png",
            scale: 8.0,
          )
        )
      ),
      backgroundColor: Colors.transparent,
      body: Stack(
        children: <Widget>[
          Container(
            alignment: Alignment.center,
            decoration: BoxDecoration(
              image: DecorationImage(
                image: AssetImage('assets/background.png'),
                fit: BoxFit.cover,
              ),
            ),
          ),
          Positioned(
            width: MediaQuery.of(context).size.width,
            top: MediaQuery.of(context).size.width * 0.30,
            child: Container(
              margin: EdgeInsets.all(16.0),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Image.asset('assets/arrowPNG.png', scale: 2.5),
                  SizedBox(height: 20,),
                  Text("SMARTID", style: TextStyle(
                    fontSize: 30, color: Colors.black, fontFamily: 'Open Sans', fontWeight: FontWeight.bold,       
                  ),
                  ),
                  Text("Attendance & Wallet Monitoring", style: TextStyle(
                    fontSize: 16, color: Colors.black, fontFamily: 'Open Sans', fontWeight: FontWeight.normal,
                  )
                  )
                ],
              ),
            ),
          ),
          Positioned(
            width: MediaQuery.of(context).size.width,
            top: MediaQuery.of(context).size.width * 0.85,
            child: Container(
              margin: EdgeInsets.all(16.0),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Text("Version 1.0", style: TextStyle(
                    fontSize: 16, color: Colors.black, fontFamily: 'Open Sans', fontWeight: FontWeight.normal,
                  )
                  )
                ],
              )
            )
          )
        ],
      ),
    );
  }
}

class LoginForm extends StatefulWidget{
  @override 
  LoginFormState createState(){
    return LoginFormState();
  }
}

class LoginFormState extends State<LoginForm>{
  final formKey = GlobalKey<FormState>();
  String _username, _password;

  @override 
  Widget build(BuildContext context){
    body: Card(
      child: Padding(
        padding: EdgeInsets.all(8.0),
        child: Form(
          key: formKey,
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              TextFormField(
                decoration: InputDecoration(
                  labelText: 'Username:'
                ),
                validator: (input) => !input.contains('0') ? 'Username cannot be blank' : null,
                onSaved: (input) => _username = input,
              ),
            ],
          ),
        ),
      ),
    );
  }
}

我该如何解决?预先感谢!

1 个答案:

答案 0 :(得分:0)

您是对的,模拟器不是问题。问题出在您的代码中。您已经正确创建了LoginPage。要更正LoginForm小部件,请在LoingFormState的构建函数中替换:

body: Card(
    // your usual code that you have above
)

return Card(
// your usual code that you have above
)

您还没有在LoginPage中使用LoginForm窗口小部件,这就是表单不可见的原因。简而言之,创建窗口小部件在任何屏幕上都不包括该窗口小部件,您必须使用该窗口小部件。

您可以通过在LoginPageState的build函数的body部分内添加小部件来实现。要使用小部件类型

body: Stack(
    children: <Widget>[
           // your other widgets
           LoginForm(),
    ]
)

就像处理其他任何小部件一样。

这应该可以解决您的问题