Как я могу сделать каркас для этого экрана входа? - PullRequest
1 голос
/ 19 марта 2020

Я пробовал много разных вещей, чтобы заставить виджеты выстраиваться в линию, но я совсем новичок, чтобы трепетать и не могу с легкостью создавать строительные леса. Я хочу, чтобы мой экран выглядел следующим образом. enter image description here Я действительно борюсь за то, чтобы выровнять вещи относительно других, как, например, поместить 2 текстовых представления под иконкой и выровнять вещи относительно страницы. Мой флаттерный код тоже ужасен, поэтому, наверное, нет смысла его помещать. Спасибо за любую помощь, спасибо!

Ответы [ 3 ]

3 голосов
/ 19 марта 2020

Я сделал один здесь. Вы можете проверить код в этом дротике: https://dartpad.dev/ab2b2531e943d0006164711a5e345be7 и изображение: enter image description here

Я попытался сделать его максимально отзывчивым, и я положить его в контейнер для имитации устройства телефона. Вы можете настроить ширину и высоту непосредственно в коде, чтобы увидеть его на разных размерах телефона. (Тем не менее, в финальной версии вы бы хотели использовать MediaQuery для получения этих данных)

Это нужно реорганизовать, но я хотел сохранить его в одном файле, чтобы поместить его в dartpad.

Поначалу я знаю, что делать эти макеты немного сложно, поэтому всегда лучше go пошагово. Вот как я подхожу к большинству макетов: 1. определить меньшие области: я определил их в своем коде и поместил их в стек: шестеренка, гребень, блок заголовка, блок ввода и кнопка входа в систему 2. Выяснение какие виджеты вы можете использовать для каждого ... и быть в поиске строк внутри столбцов и столбцов внутри строк. 3. Расположите виджеты. Это может быть либо путем точного позиционирования в стеке, который я использовал в этом коде, либо путем размещения отступов вокруг виджетов

Вот полный код:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}

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

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

var width = 350.0; // <-- play with these numbers
var height = 700.0; // <-- to see it on different sized devices

class _MyHomePageState extends State<MyHomePage> {
  bool trueFalse;
  initState() {
    trueFalse = true;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
//  width = MediaQuery.of(context).size.width;  <-- this is where you would
//  height = MediaQuery.of(context).size.height; <-- get the real width/height
    print(trueFalse);
    return Center(
      child: Container(
        width: width,
        height: height,
        child: Scaffold(
          appBar: PreferredSize(
              preferredSize: Size.fromHeight(25.0),
              child: AppBar(backgroundColor: Colors.grey[850])),
          body: Stack(
            children: [
              Positioned(
                // THIS IS THE SETTINGS COG
                right: width * 0.02,
                top: height * 0.03,
                child: Icon(
                  Icons.settings,
                  color: Colors.grey,
                  size: width * 0.1,
                ),
              ),
              Positioned(
                  // THIS IS THE CREST AND BACKGROUND
                  left: width * 0.05,
                  top: height * 0.05,
                  child: Container(
                    child: Padding(
                        padding: EdgeInsets.all(10.0),
                        child: Image(
                            image: NetworkImage(
                                'http://highlandslatin.org/wp-content/uploads/2016/12/Monogram-white.png'))),
                    width: width * 0.20,
                    height: width * 0.20,
                    decoration: BoxDecoration(
                      color: Colors.blue,
                      borderRadius: BorderRadius.circular(25),
                    ),
                  )),
              Positioned(
                // THIS IS THE HIGHLANDS UPPER TEXT
                left: width * 0.05,
                top: height * 0.17,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text("Highlands Latin School",
                        style: TextStyle(
                            fontSize: 17, fontWeight: FontWeight.bold)),
                    Container(height: 8),
                    Text("Canvas Grades",
                        style: TextStyle(
                            fontSize: 13, fontWeight: FontWeight.bold))
                  ],
                ),
              ),
              Column(
                  // THIS IS THE LOGIN AREA
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Padding(
                        padding: EdgeInsets.symmetric(horizontal: width * 0.05),
                        child: TextField(
                            decoration: InputDecoration(
                                border: OutlineInputBorder(),
                                labelText: 'Email'))),
                    Container(height: 10),
                    Padding(
                        padding: EdgeInsets.symmetric(horizontal: width * 0.05),
                        child: TextField(
                            obscureText: true,
                            decoration: InputDecoration(
                                border: OutlineInputBorder(),
                                labelText: 'Password'))),
                    Container(height: 10),
                    Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Padding(
                              padding:
                                  EdgeInsets.fromLTRB(width * 0.01, 0, 0, 0),
                              child: Container(
                                child: Row(children: [
                                  Checkbox(
                                    value: trueFalse,
                                    onChanged: (bool value) {
                                      setState(() {
                                        trueFalse =
                                            value; // trueFalse?false:true;
                                      });
                                      print("check mark changed");
                                      print(value);
                                    },
                                  ),
                                  Padding(
                                      padding: EdgeInsets.fromLTRB(1, 0, 0, 0),
                                      child: Text("Save Login?")),
                                ]),
                              )),
                          Padding(
                              padding: EdgeInsets.symmetric(
                                  horizontal: width * 0.05),
                              child: Text("Forgot Password")),
                        ])
                  ]),
              Align(
                alignment: Alignment.bottomCenter,
                child: Padding(
                  padding: EdgeInsets.fromLTRB(0, 0, 0, width * 0.05),
                  child: Container(
                    width: width - 2 * width * 0.05,
                    child: FlatButton(
                      color: Colors.grey[850],
                      textColor: Colors.white,
                      padding: EdgeInsets.symmetric(
                          vertical: 11.0, horizontal: width * 0.05),
                      splashColor: Colors.blue,
                      onPressed: () {
                        print("button pressed");
                      },
                      child: Text(
                        "Login",
                        style: TextStyle(fontSize: 20.0),
                      ),
                    ),
                  ),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}
3 голосов
/ 19 марта 2020

Проверьте это


import 'package:flutter/material.dart';

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

class _LoginState extends State<Login> {
  bool _obscureText = true;
  bool _isChecked = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Padding(
          padding: EdgeInsets.all(20),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Container(
                        width: 85,
                        height: 85,
                        decoration: BoxDecoration(
                            color: Colors.blue[900],
                            borderRadius: BorderRadius.circular(30)),
                      ),
                      Icon(
                        Icons.settings,
                        color: Colors.grey,
                        size: 40,
                      )
                    ],
                  ),
                  SizedBox(
                    height: 10,
                  ),
                  Text(
                    "Highlands Latin School",
                    style: TextStyle(fontSize: 25, color: Colors.blue[900]),
                  ),
                  SizedBox(
                    height: 10,
                  ),
                  Text(
                    "Canvas Grades",
                    style: TextStyle(color: Colors.grey[700], fontSize: 18),
                  ),
                ],
              ),
              Column(
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  Column(
                    children: <Widget>[
                      TextField(
                        decoration: InputDecoration(
                            labelText: "Email",
                            border: OutlineInputBorder()
                        ),
                      ),
                      SizedBox(height: 10,),
                      TextField(
                        obscureText: _obscureText,
                        decoration: InputDecoration(
                          labelText: "Password",
                          border: OutlineInputBorder(),
                          suffixIcon: IconButton(
                            icon: Icon(_obscureText ? Icons.visibility_off: Icons.visibility),
                            onPressed: (){
                              setState(() {
                                _obscureText = !_obscureText;
                              });
                            },
                          )
                        ),
                      ),
                      SizedBox(height: 10,),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        children: <Widget>[
                          Row(
                            children: <Widget>[
                              Checkbox(
                                value: _isChecked,
                                onChanged: (value){
                                  setState(() {
                                    _isChecked = !_isChecked;
                                  });
                                },
                              ),
                              Text(
                                "SAVE LOGIN?"
                              )
                            ],
                          ),
                          Container(
                            height: 30,
                            width: 1,
                            color: Colors.grey,
                          ),
                          GestureDetector(
                            onTap: (){},
                            child: Text(
                              "FORGOT PASSWORD?"
                            ),
                          )
                        ],
                      )
                    ],
                  ),
                ],
              ),
              SizedBox(
                width: double.infinity,
                child: RaisedButton(
                  child: Text(
                    "Login",
                    style: TextStyle(
                        color: Colors.white,
                        fontSize: 18
                    ),
                  ),
                  padding: EdgeInsets.all(15),
                  color: Colors.blue[900],
                  onPressed: (){

                  },
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}

Выход: enter image description here

Надеюсь, это поможет вам.

1 голос
/ 20 марта 2020

Примечание. Этому очень помог ответ Уильяма Террилла и Жостева Адеканби

Вот мой окончательный код после компиляции из разных мест:

@override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Padding(
          padding: EdgeInsets.all(20),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Image.asset('assets/ic_launcher_round.png', fit: BoxFit.contain, width: 100, height: 100),
                      Builder(
                        builder: (BuildContext context) {
                        return
                          IconButton(
                              icon: Icon(MyFlutterApp.settings),
                              color: Colors.black,
                              onPressed: () {
                                  navigateToSettings(context);
                              },
                          );
                      }
                      )
                    ],
                  ),
                  SizedBox(
                    height: 10,
                  ),
                  Text(
                    "Highlands Latin School",
                    style: TextStyle(fontSize: 25, color: Color(0xFF012B5C), fontWeight: FontWeight.w700),
                  ),
                  SizedBox(
                    height: 10,
                  ),
                  Text(
                    "Canvas Grades",
                    style: TextStyle(color:  Color(0xFF827250), fontSize: 18,),
                  ),
                ],
              ),
              Column(
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  Column(
                    children: <Widget>[
                      TextField(
                        controller: myController1,
                        decoration: InputDecoration(
                            labelText: "Email",
                            border: OutlineInputBorder()
                        ),
                      ),
                      SizedBox(height: 10,),
                      Visibility(
                        visible: _isVisible,
                        child: TextField(
                          obscureText: _obscureText,
                          controller: myController2,
                          decoration: InputDecoration(
                              labelText: "Password",
                              border: OutlineInputBorder(),
                              suffixIcon: IconButton(
                                icon: Icon(_obscureText ? Icons.visibility_off: Icons.visibility),
                                onPressed: (){
                                  setState(() {
                                    _obscureText = !_obscureText;
                                  });
                                },
                              )
                          ),
                        ),
                      ),
                      SizedBox(height: 10,),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: <Widget>[
                          Row(
                            children: <Widget>[
                              Visibility(
                                visible: _isVisible,
                                maintainSize: true,
                                maintainState: true,
                                maintainAnimation: true,
                                child: Checkbox(
                                  value: _isChecked,
                                  activeColor: Color(0xFF827250),
                                  onChanged: (bool value){
                                    setState(() {
                                      _isChecked = value;
                                      if (_isChecked) {
                                        prefs.setString("email", Email);
                                        prefs.setString("pass", Password);
                                        prefs.setBool("remember", true);
                                      } else {
                                        prefs.setString("email", "");
                                        prefs.setString("pass", "");
                                        prefs.setBool("remember", false);
                                      }
                                    });
                                  },
                                ),
                              ),
                              Visibility(
                                  maintainSize: true,
                                  maintainState: true,
                                  maintainAnimation: true,
                                  visible: _isVisible,
                                  child: Text(
                                  "SAVE LOGIN?", style: TextStyle(color:  Color(0xFF827250)),
                              )
                              ),
                            ],
                          ),
                          GestureDetector(
                            onTap: (){
                              setState(() {
                                if (_forgotPassText == "FORGOT PASSWORD?") {
                                  _forgotPassText = "Back to Login";
                                  _loginText = "Request Password";
                                  _isVisible = false;
                                } else {
                                  _forgotPassText = "FORGOT PASSWORD?";
                                  _loginText = "Login";
                                  _isVisible = true;
                                }});
                            },
                            child: Text(_forgotPassText, style: TextStyle(color:  Color(0xFF827250))
                            ),
                          ),
                        ],
                      )
                    ],
                  ),
                ],
              ),
              Column(
                children: <Widget>[
                ProgressButton(
                  child: Text(
                    _loginText,
                    style: TextStyle(
                        color: Colors.white,
                        fontSize: 18
                    ),
                  ),
                  backgroundColor: Color(0xFF012B5C),
                  buttonState: _buttonState,
                  progressColor: Colors.white,
                  onPressed: () async {
                          //custom login method, edit _buttonState to make the button become spinner
                    }
                  },
                ),
                SizedBox(height: 10),
                Padding(
                  padding: const EdgeInsets.all(15.0),
                  child: Text(error, style: TextStyle(color: _errorColor),
                  ),
                ),
                ]
              )
            ],
          ),
        ),
      ),
    );
  }

Я добавил Visibility и переменные в setStates, чтобы меняйте цвета, тексты и т. д. c на лету разными способами. Кроме того, я поместил текст в самом низу под кнопкой, которая не была показана на исходном рисунке.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...