繁体   English   中英

在 flutter 中,需要在我的 main.dart 文件中使用“Login()”。 检查我的 dart 代码如下:

[英]in flutter, Need to use 'Login()' in my main.dart file. Check my dart code below:

这是一小段代码

class Login extends StatefulWidget {
  final double screenHeight, screenWidth;

  const Login({Key key, this.screenHeight, this.screenWidth}) : super(key: key);

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

class _LoginState extends State<Login> {
  var _formkey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: PopBlue,
      body: Stack(
        children: <Widget>[
          CustomPaint(
            painter: MyCustomPainter(),
            child: Container(
              height: widget.screenHeight * 0.65,
            ),
          ),
...

登录class 在第一行。
我必须在我的main.dart文件中使用这个登录class 作为home: Login()
i am not able to use this login class in my main code which is at main.dart file help me finding a way to use this class in my main.dart file.

好的,我玩了一会儿,想出了这个应该可以帮助你入门的方法,(测试并在 Chrome 上工作)

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) => MaterialApp(home: Login());
}

class Login extends StatefulWidget {
  @override
  _LoginState createState() => _LoginState();
}

class _LoginState extends State<Login> {
  var _formkey = GlobalKey<FormState>();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.blue, //PopBlue,
      body: Stack(
        children: <Widget>[
          Container(
            height: MediaQuery.of(context).size.height * 0.65, //widget.screenHeight * 0.65,
            width: MediaQuery.of(context).size.width * 0.65,
            color: Colors.red,
          ),
          CustomPaint(
            size: Size(300, 300),
            painter: MyCustomPainter(),
            // child: Container(
            //   height: MediaQuery.of(context).size.height * 0.65,     //widget.screenHeight * 0.65,
            //   width: MediaQuery.of(context).size.width * 0.65,
            //   color: Colors.red,                // ),             ),
        ],
      ),
    );
  }
}

class MyCustomPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    final left = 50.0;
    final top = 100.0;
    final right = 250.0;
    final bottom = 200.0;
    final rect = Rect.fromLTRB(left, top, right, bottom);
    final paint = Paint()
      ..color = Colors.black
      ..style = PaintingStyle.stroke
      ..strokeWidth = 4;
    canvas.drawRect(rect, paint);
  }

  @override
  bool shouldRepaint(CustomPainter old) => false;
}

您的 main.dart 将如下所示

import 'package:flutter/material.dart';
import 'path-of-your-login-page';//import './login.dart';

void main() => runApp(MyApp());
class MyApp extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Login();
    );
  }
}

“我无法在我的主代码中使用此登录 class”是什么意思?

IDE 是否报告了问题? 如果是这样,它说明了什么?

还是在运行时失败? 如果是这样,错误信息是什么?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM