找不到材质小部件文本字段小部件需要材质小部件祖先

时间:2018-08-09 17:34:10

标签: android ios material-design flutter

您好,我试图在浮动状态下构建登录屏幕,但是打开它时出现错误提示。

  

未找到任何材料小部件,文本字段小部件需要材料小部件祖先

 import 'package:flutter/material.dart';

 Widget LoginPage() {
    return new Container(
      height: MediaQuery.of(context).size.height,
      decoration: BoxDecoration(
        color: Colors.white,
        image: DecorationImage(
          colorFilter: new ColorFilter.mode(
              Colors.black.withOpacity(0.05), BlendMode.dstATop),
          image: AssetImage('assets/images/mountains.jpg'),
          fit: BoxFit.cover,
        ),
      ),
      child: new Column(
        children: <Widget>[
          Container(
            padding: EdgeInsets.all(120.0),
            child: Center(
              child: Icon(
                Icons.headset_mic,
                color: Colors.redAccent,
                size: 50.0,
              ),
            ),
          ),
          new Row(
            children: <Widget>[
              new Expanded(
                child: new Padding(
                  padding: const EdgeInsets.only(left: 40.0),
                  child: new Text(
                    "EMAIL",
                    style: TextStyle(
                      fontWeight: FontWeight.bold,
                      color: Colors.redAccent,
                      fontSize: 15.0,
                    ),
                  ),
                ),
              ),
            ],
          ),
          new Container(
            width: MediaQuery.of(context).size.width,
            margin: const EdgeInsets.only(left: 40.0, right: 40.0, top: 10.0),
            alignment: Alignment.center,
            decoration: BoxDecoration(
              border: Border(
                bottom: BorderSide(
                    color: Colors.redAccent,
                    width: 0.5,
                    style: BorderStyle.solid),
              ),
            ),
            padding: const EdgeInsets.only(left: 0.0, right: 10.0),
            child: new Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.start,
              children: <Widget>[
                new Expanded(
                  child: TextField(
                    obscureText: true,
                    textAlign: TextAlign.left,
                    decoration: InputDecoration(
                      border: InputBorder.none,
                      hintText: 'samarthagarwal@live.com',
                      hintStyle: TextStyle(color: Colors.grey),
                    ),
                  ),
                ),
              ],
            ),
          ),
          Divider(
            height: 24.0,
          ),
          new Row(
            children: <Widget>[
              new Expanded(
                child: new Padding(
                  padding: const EdgeInsets.only(left: 40.0),
                  child: new Text(
                    "PASSWORD",
                    style: TextStyle(
                      fontWeight: FontWeight.bold,
                      color: Colors.redAccent,
                      fontSize: 15.0,
                    ),
                  ),
                ),
              ),
            ],
          ),
          new Container(
            width: MediaQuery.of(context).size.width,
            margin: const EdgeInsets.only(left: 40.0, right: 40.0, top: 10.0),
            alignment: Alignment.center,
            decoration: BoxDecoration(
              border: Border(
                bottom: BorderSide(
                    color: Colors.redAccent,
                    width: 0.5,
                    style: BorderStyle.solid),
              ),
            ),
            padding: const EdgeInsets.only(left: 0.0, right: 10.0),
            child: new Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.start,
              children: <Widget>[
                new Expanded(
                  child: TextField(
                    obscureText: true,
                    textAlign: TextAlign.left,
                    decoration: InputDecoration(
                      border: InputBorder.none,
                      hintText: '*********',
                      hintStyle: TextStyle(color: Colors.grey),
                    ),
                  ),
                ),
              ],
            ),
          ),
          Divider(
            height: 24.0,
          ),
          new Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: <Widget>[
              Padding(
                padding: const EdgeInsets.only(right: 20.0),
                child: new FlatButton(
                  child: new Text(
                    "Forgot Password?",
                    style: TextStyle(
                      fontWeight: FontWeight.bold,
                      color: Colors.redAccent,
                      fontSize: 15.0,
                    ),
                    textAlign: TextAlign.end,
                  ),
                  onPressed: () => {},
                ),
              ),
            ],
          ),
          new Container(
            width: MediaQuery.of(context).size.width,
            margin: const EdgeInsets.only(left: 30.0, right: 30.0, top: 20.0),
            alignment: Alignment.center,
            child: new Row(
              children: <Widget>[
                new Expanded(
                  child: new FlatButton(
                    shape: new RoundedRectangleBorder(
                      borderRadius: new BorderRadius.circular(30.0),
                    ),
                    color: Colors.redAccent,
                    onPressed: () => {},
                    child: new Container(
                      padding: const EdgeInsets.symmetric(
                        vertical: 20.0,
                        horizontal: 20.0,
                      ),
                      child: new Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: <Widget>[
                          new Expanded(
                            child: Text(
                              "LOGIN",
                              textAlign: TextAlign.center,
                              style: TextStyle(
                                  color: Colors.white,
                                  fontWeight: FontWeight.bold),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
          new Container(
            width: MediaQuery.of(context).size.width,
            margin: const EdgeInsets.only(left: 30.0, right: 30.0, top: 20.0),
            alignment: Alignment.center,
            child: Row(
              children: <Widget>[
                new Expanded(
                  child: new Container(
                    margin: EdgeInsets.all(8.0),
                    decoration: BoxDecoration(border: Border.all(width: 0.25)),
                  ),
                ),
                Text(
                  "OR CONNECT WITH",
                  style: TextStyle(
                    color: Colors.grey,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                new Expanded(
                  child: new Container(
                    margin: EdgeInsets.all(8.0),
                    decoration: BoxDecoration(border: Border.all(width: 0.25)),
                  ),
                ),
              ],
            ),
          ),
          new Container(
            width: MediaQuery.of(context).size.width,
            margin: const EdgeInsets.only(left: 30.0, right: 30.0, top: 20.0),
            child: new Row(
              children: <Widget>[
                new Expanded(
                  child: new Container(
                    margin: EdgeInsets.only(right: 8.0),
                    alignment: Alignment.center,
                    child: new Row(
                      children: <Widget>[
                        new Expanded(
                          child: new FlatButton(
                            shape: new RoundedRectangleBorder(
                              borderRadius: new BorderRadius.circular(30.0),
                            ),
                            color: Color(0Xff3B5998),
                            onPressed: () => {},
                            child: new Container(
                              child: new Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: <Widget>[
                                  new Expanded(
                                    child: new FlatButton(
                                      padding: EdgeInsets.only(
                                        top: 20.0,
                                        bottom: 20.0,
                                      ),
                                      child: new Row(
                                        mainAxisAlignment:
                                        MainAxisAlignment.spaceEvenly,
                                        children: <Widget>[
                                          Icon(
                                            const IconData(0xea90,
                                                fontFamily: 'icomoon'),
                                            color: Colors.white,
                                            size: 15.0,
                                          ),
                                          Text(
                                            "FACEBOOK",
                                            textAlign: TextAlign.center,
                                            style: TextStyle(
                                                color: Colors.white,
                                                fontWeight: FontWeight.bold),
                                          ),
                                        ],
                                      ),
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
                new Expanded(
                  child: new Container(
                    margin: EdgeInsets.only(left: 8.0),
                    alignment: Alignment.center,
                    child: new Row(
                      children: <Widget>[
                        new Expanded(
                          child: new FlatButton(
                            shape: new RoundedRectangleBorder(
                              borderRadius: new BorderRadius.circular(30.0),
                            ),
                            color: Color(0Xffdb3236),
                            onPressed: () => {},
                            child: new Container(
                              child: new Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: <Widget>[
                                  new Expanded(
                                    child: new FlatButton(
                                      padding: EdgeInsets.only(
                                        top: 20.0,
                                        bottom: 20.0,
                                      ),
                                      child: new Row(
                                        mainAxisAlignment:
                                        MainAxisAlignment.spaceEvenly,
                                        children: <Widget>[
                                          Icon(
                                            const IconData(0xea88,
                                                fontFamily: 'icomoon'),
                                            color: Colors.white,
                                            size: 15.0,
                                          ),
                                          Text(
                                            "GOOGLE",
                                            textAlign: TextAlign.center,
                                            style: TextStyle(
                                                color: Colors.white,
                                                fontWeight: FontWeight.bold),
                                          ),
                                        ],
                                      ),
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            ),
          )
        ],
      ),
    );
  }

enter image description here

有人知道如何解决吗?

6 个答案:

答案 0 :(得分:7)

错误指出 TextField小部件需要Material小部件祖先。只需将整个loginWidget 包装Widget LoginPage() { return new Scaffold(body: *your whole code*) } 中即可解决该问题。

package WASD;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.*;

public class WASD extends JFrame implements KeyListener, ActionListener{
    private int playerX = 400;
    private int playerY = 400;
    int C = 1;
    private int RESTART = 0; // Three ENTER = Restart
    private Timer timer;
    JFrame j = new JFrame();
    private int previousX = -50;
    private int previousY = -50;
    public WASD() {
        JPanel panel = new JPanel();
        setTitle("Not supposed to be a painting program.");
        setSize(2000, 2000);

        timer = new Timer(8, this);

        this.getContentPane().add(panel);

        addKeyListener(this);
        setVisible(true);
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);

        setDefaultCloseOperation(EXIT_ON_CLOSE);

    }
    public static void main(String[] args){
        WASD W = new WASD();
    }
    public void paint(Graphics g) { //TODO paint is only working once, then failing forever.
        //////////////////////////
        g.setColor(Color.BLACK);
        g.drawRect(0, 0, 3000, 3000); // Background


        if(C == 1) g.setColor(Color.RED);

        else if (C == 2) g.setColor(Color.BLUE);
        else if (C == 3) g.setColor(Color.GREEN);
        else if (C == 4) g.setColor(Color.YELLOW);
        else if(C>4){
            g.setColor(Color.GRAY); 
            try {
                Thread.sleep(1000); //Gray pauses for a second I guess...
            } 
            catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            C = 1;
        }
        g.fillRect(playerX, playerY, 50, 50); // Player/
        g.setColor(Color.BLACK);
        g.fillRect(previousX, previousY, 50, 50);
        g.dispose(); //Last thing
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        timer.start();
        repaint();
    }
    @Override
    public void keyPressed(KeyEvent K) {
        // TODO Auto-generated method stub
        int e = K.getKeyCode();

        repaint();  //timer.start(); does not affect this. g.dispose() is unavailable.
    /*
    If this repaint is used, then the previous locations 
    that the player was at will be painted as well.
    Basically, instead of moving, all instances 
    made will not be deleted. #PaintingApplication

    However, if it is not used, then the CHARACTER will not move at all.

    It seems that the problem is not about 

    playerX, or playerY, 

    instead, it is about the program not refreshing.
    */
        if((e == KeyEvent.VK_RIGHT || e == KeyEvent.VK_D) && playerX<2000){
            playerX += 200;
        }
        else if((e == KeyEvent.VK_LEFT || e == KeyEvent.VK_A) && playerX>0){
            playerX -= 200;
        }
        else if((e == KeyEvent.VK_UP || e == KeyEvent.VK_W) && playerY>0){
            playerY -= 200;
        }
        else if(e == KeyEvent.VK_DOWN || e == KeyEvent.VK_S && playerY<2000){ // Easter egg bug 1
            playerY += 200;
        }
        else if (e == KeyEvent.VK_C){
            C++;
        }

        else if(e == KeyEvent.VK_ENTER){
            if(RESTART == 3){
                playerX = 400;
                playerY = 400;
                RESTART = 0; //TODO
                repaint();
            }
            else{
                RESTART++;
            }
        }
        else{
            RESTART = 0;
        }

    }

    @Override public void keyTyped(KeyEvent arg0) {}
    @Override public void keyReleased(KeyEvent arg0) {}
}

答案 1 :(得分:2)

用Scaffold或MaterialApp包装登录页面功能

` Widget LoginPage() {
   return new Scaffold(
   body: Container(
   *the rest of your code*
     )
    }`

` Widget LoginPage() {
 return new MaterialApp(
  home: Container(
  *the rest of your code*
   )
   }`

答案 2 :(得分:0)

我遇到了同样的问题。始终记住要输入Scaffold,要记住它必须作为创建页面的父项包含在其中,之后,您将插入所需的所有小部件,但是MaterialApp之后的Scaffold是创建新页面时必须添加的第二个组件。页面。

答案 3 :(得分:0)

只需使用以下材料将小部件包装起来:

@override
Widget build(BuildContext context) {
  return Material(
    child: YourAwesomeWidget(),
}

答案 4 :(得分:0)

由于大多数小部件都要求将材质小部件作为其父小部件

这是一种好的做法,您应该在小部件树顶部的上使用Material()Scaffold()小部件,然后继续执行代码。

@override
Widget build(BuildContext context) {
  return Material(
    child: body(),
}

OR

@override
Widget build(BuildContext context) {
  return Scaffold(
    child: body(),
}

答案 5 :(得分:-1)

用Scaffold将代码包装起来。它有效