State<WelcomePage>createState() => _WelcomePageState(); }

问题描述 投票:0回答:1

我对编码/flutter/dart 很陌生,我试图在前进的过程中学习一些东西。我正在尝试为应用程序构建欢迎页面。一切都很顺利,直到我尝试向显示密码字段添加功能。错误是名称冲突。我如何解决这个问题,任何帮助将不胜感激。

    import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Welcome Page',
      home: WelcomePage(),
    );
  }
}

class WelcomePage extends StatefulWidget {
  @override
  State<WelcomePage> createState() => _WelcomePageState();
}

class _WelcomePageState extends State<WelcomePage> {
  bool _passwordVisible = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.red[900], // Dark red     background
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            // ... other widgets ...

            // Password field
            SizedBox(
              width: MediaQuery.of(context).size.width * 0.8,     // Narrower width
              child: TextField(
                obscureText: !_passwordVisible, // Toggle     visibility
                decoration: InputDecoration(
                  labelText: 'Password',
                  border: OutlineInputBorder(),
                  fillColor: Colors.white,
                  filled: true,
                  suffixIcon: IconButton(
                    icon: Icon(
                      // Change the icon based on the     visibility state
                      _passwordVisible ? Icons.visibility :     Icons.visibility_off,
                    ),
                    onPressed: () {
                      setState(() {
                        _passwordVisible = !_passwordVisible;
                      });
                    },
                  ),
                 ),
              ),
            ),

            // ... other widgets ...
          ],
        ),
      ),
    );
  }
}

类“WelcomePage”无法定义方法“createState”并且具有同名字段“StatefulWidget.createState”。 尝试将方法转换为 getter,或者将方法重命名为不冲突的名称。dart(conflicting_method_and_field)

这是欢迎页面的完整代码,上面是错误。任何人都可以确定为什么会发生这种情况,以及如果这种事情再次发生,本页和以后的页面的补救措施是什么。

flutter
1个回答
0
投票

删除此页面并只需输入 stful 创建一个新页面。

之后在终端上运行 flutter pub clean 并等待完成该过程,然后 flutter pub get

你们都还好:)

© www.soinside.com 2019 - 2024. All rights reserved.