首页 > 解决方案 > 应用程序因 obsureText !=null 而崩溃。我该如何解决这个问题?

问题描述

我正在构建一个登录 ui 屏幕,一切正常,但是因为我应该使用多个TextField ,所以我创建了一个新的TextField类,代码如下

import 'package:gradient_widgets/gradient_widgets.dart';
import 'package:notepad/authentication/textfield.dart';

class LogIn extends StatefulWidget {
  LogIn({Key key}) : super(key: key);

  @override
  _LogInState createState() => _LogInState();
}

class _LogInState extends State<LogIn> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomPadding: false,
      body: Column(
        crossAxisAlignment:
            CrossAxisAlignment.start, //TODO: ESSE DACK NA EK BAR
        children: [
          Container(
            padding: EdgeInsets.fromLTRB(20.0, 150.0, 20.0, 0.0),
            child: Stack(
              children: [
                Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      'Welcome,',
                      style: TextStyle(
                        fontWeight: FontWeight.bold,
                        fontSize: 35.0,
                      ),
                    ),
                    Text(
                      'Log In to continue!',
                      style: TextStyle(
                        fontWeight: FontWeight.bold,
                        fontSize: 20.0,
                        color: Colors.grey,
                      ),
                    ),
                    SizedBox(height: 70),
                    buildTextField(
                      labelText: 'Email',
                    ),
                    SizedBox(height: 30),
                    buildTextField(
                      labelText: 'Password',
                       obscureText: true, //TODO: password ko thich krna hy!!!
                    ),
                    SizedBox(height: 10),
                    Container(
                      alignment: Alignment(1.0, 0.0),
                      child: InkWell(
                        onTap: () {
                          //TODO: navigate to forgot password page
                        },
                        child: Text(
                          'Forgot Password?',
                          style: TextStyle(
                            fontWeight: FontWeight.bold,
                            color: Colors.deepOrange,
                            fontSize: 10.0,
                          ),
                        ),
                      ),
                    ),
                    SizedBox(height: 70),
                    Container(
                      child: GradientButton(
                        increaseHeightBy: 10.0,
                        increaseWidthBy: 200.0,
                        child: Text(
                          'Log In',
                          style: TextStyle(fontWeight: FontWeight.bold),
                        ),
                        callback: () {},
                        //TODO: LOGIN KA CALLBACK JAYEGA YAHA PR
                        gradient: LinearGradient(
                          colors: [
                            Color(0xFFFD7F2C),
                            Color(0xFFFF6200),
                            Color(0xFFFD9346),
                          ],
                        ),
                        shadowColor: Gradients.backToFuture.colors.last
                            .withOpacity(0.25),
                      ),
                    ),
                    SizedBox(
                      height: 10.0,
                    ),
                    Container(
                      child: GradientButton(
                        increaseHeightBy: 10.0,
                        increaseWidthBy: 200.0,
                        child: Text(
                          'Log In With Google',
                          style: TextStyle(
                              fontWeight: FontWeight.bold, color: Colors.black),
                        ),
                        callback: () {},
                        //TODO: LOGIN KA CALLBACK JAYEGA YAHA PR
                        gradient: LinearGradient(
                            colors: [Colors.white, Colors.white]),
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

文本字段类的代码


TextField buildTextField({
  @required String labelText,
  bool obscureText,
}) {
  return TextField(
    decoration: InputDecoration(
      enabledBorder: OutlineInputBorder(
        borderRadius: BorderRadius.circular(25),
        borderSide: BorderSide(color: Colors.white),
      ),
      focusedBorder: OutlineInputBorder(
        borderRadius: BorderRadius.circular(25),
        borderSide: BorderSide(color: Colors.deepOrange),
      ),
      labelText: labelText,
      labelStyle:
          TextStyle(fontWeight: FontWeight.bold, color: Colors.deepOrange),
    ),
    keyboardType: TextInputType.emailAddress,
      obscureText: obscureText,
  );
}

我在登录页面中将 obscureText传递为 true 时,我的应用程序崩溃说obsureText !=null在此处输入图像描述

我不知道我能解决这个问题,因为我是新手,我会很感激有人帮助我解决这个问题

标签: flutterdart

解决方案


您收到该错误是因为您没有将值传递给

bool obscureText,

确保从调用 buildTextField 的任何地方传递 obscureText 的值,或者像这样为 obscureText 设置默认值 -

bool obscureText = false,

请参阅下面的工作代码:

import 'package:flutter/material.dart';

final Color darkBlue = const Color.fromARGB(255, 18, 32, 47);

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
      debugShowCheckedModeBanner: false,
      home: const Scaffold(
        body: Center(
          child: LogIn(),
        ),
      ),
    );
  }
}

class LogIn extends StatefulWidget {
  const LogIn({Key key}) : super(key: key);

  @override
  _LogInState createState() => _LogInState();
}

class _LogInState extends State<LogIn> {
  TextField buildTextField({
    @required String labelText,
    bool obscureText = false,
  }) {
    return TextField(
      decoration: InputDecoration(
        enabledBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(25),
          borderSide: const BorderSide(color: Colors.white),
        ),
        focusedBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(25),
          borderSide: const BorderSide(color: Colors.deepOrange),
        ),
        labelText: labelText,
        labelStyle: const TextStyle(
            fontWeight: FontWeight.bold, color: Colors.deepOrange),
      ),
      keyboardType: TextInputType.emailAddress,
      obscureText: obscureText,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomPadding: false,
      body: SingleChildScrollView(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Container(
              padding: const EdgeInsets.fromLTRB(20.0, 150.0, 20.0, 0.0),
              child: Stack(
                children: [
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      const Text(
                        'Welcome,',
                        style: TextStyle(
                          fontWeight: FontWeight.bold,
                          fontSize: 35.0,
                        ),
                      ),
                      const Text(
                        'Log In to continue!',
                        style: TextStyle(
                          fontWeight: FontWeight.bold,
                          fontSize: 20.0,
                          color: Colors.grey,
                        ),
                      ),
                      const SizedBox(height: 70),
                      buildTextField(
                        labelText: 'Email',
                      ),
                      const SizedBox(height: 30),
                      buildTextField(
                        labelText: 'Password',
                        obscureText: true,
                      ),
                      const SizedBox(height: 10),
                      Container(
                        alignment: const Alignment(1.0, 0.0),
                        child: InkWell(
                          onTap: () {},
                          child: const Text(
                            'Forgot Password?',
                            style: TextStyle(
                              fontWeight: FontWeight.bold,
                              color: Colors.deepOrange,
                              fontSize: 10.0,
                            ),
                          ),
                        ),
                      ),
                      const SizedBox(height: 70),
                      Container(
                        child: RaisedButton(
                          child: const Text(
                            'Log In',
                            style: TextStyle(fontWeight: FontWeight.bold),
                          ),
                          onPressed: () {},
                        ),
                      ),
                      const SizedBox(
                        height: 10.0,
                      ),
                      Container(
                        child: RaisedButton(
                          child: const Text(
                            'Log In With Google',
                            style: TextStyle(
                                fontWeight: FontWeight.bold,
                                color: Colors.black),
                          ),
                          onPressed: () {},
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

推荐阅读