简体   繁体   English

如何在 Flutter 中设置 A Button 或 Text Widget 在屏幕底部

[英]How to set A Button or Text Widget in Flutter At the bottom of the screen

I'm trying to make a LoginPage in Flutter.我正在尝试在 Flutter 中创建一个 LoginPage。 I Just want to place "Terms of Service. Privacy Policy."我只想放置“服务条款。隐私政策”。 Flatbutton At the end of the screen. Flatbutton 在屏幕的末尾。 I tried many things but nothing seems to work!我尝试了很多东西,但似乎没有任何效果!

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Expense Tracker',
      home: LoginPage(),
    );
  }
}

class LoginPage extends StatefulWidget {
  // String titleInput;
  // String amountInput;
  @override
  _LoginPage createState() => _LoginPage();
}

class _LoginPage extends State<LoginPage> {
  final userName = TextEditingController();
  final password = TextEditingController();
  String textDisplay = "";

  void onSubmit() {
    setState(() {
      if (userName == null) {
        textDisplay = "Invalid Username";
      } else if (password == null && userName != null) {
        textDisplay = "Invalid Password";
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('LOGIN'),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [
            Padding(
              padding: EdgeInsets.all(10),
              child: Image.asset("assets/images/nike.png"),
            ),
            Container(
              padding: EdgeInsets.all(10),
              child: Column(
                children: [
                  TextField(
                    controller: userName,
                    decoration: InputDecoration(
                      labelText: "Username",
                      labelStyle: TextStyle(
                        fontWeight: FontWeight.bold,
                        color: Colors.black,
                      ),
                    ),
                    keyboardType: TextInputType.emailAddress,
                    onSubmitted: (_) => onSubmit(),
                  ),
                  TextField(
                    controller: password,
                    decoration: InputDecoration(
                      labelText: "Password",
                      labelStyle: TextStyle(
                        fontWeight: FontWeight.bold,
                        color: Colors.black,
                      ),
                    ),
                    keyboardType: TextInputType.visiblePassword,
                    onSubmitted: (_) => onSubmit(),
                  ),
                  Text(
                    textDisplay,
                    style: TextStyle(
                      color: Colors.redAccent[700],
                    ),
                  ),
                  Column(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: [
                      FlatButton(
                        child: Text(
                          "Not a User. Sign Up Here.",
                          style: TextStyle(
                            color: Colors.black,
                            fontSize: 15,
                          ),
                        ),
                        onPressed: () {},
                      ),
                      FlatButton(
                        child: Text(
                          "Terms of Service. Privacy Policy.",
                          style: TextStyle(
                            color: Colors.grey[700],
                            fontSize: 13,
                          ),
                        ),
                        onPressed: () {},
                      ),
                    ],
                  )
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}

SetState(() {}) Also doen't work. SetState(() {})也不行。 Please explain the answer too as i'm still learning Flutter.请解释答案,因为我还在学习 Flutter。

If there are any good changes in my code to make my app better please suggest them.如果我的代码中有任何好的更改以使我的应用程序更好,请提出建议。

If you separate the contents of the column into two child elements and use mainAxisAlignment: MainAxisAlignment.spaceBetween , you will get the desired result, for the setState part TextEditingController() has a property of text.如果将列的内容分成两个子元素并使用mainAxisAlignment: MainAxisAlignment.spaceBetween ,您将获得所需的结果,因为 setState 部分TextEditingController()具有文本属性。 Also you need to exchange SingleChildScrollView() with CustomScrollView, as per this answer: https://stackoverflow.com/a/62097942/13315601您还需要根据以下答案将SingleChildScrollView()与 CustomScrollView 交换: https://stackoverflow.com/a/62097942/13315601

See the reworked code below:请参阅下面的重新编写的代码:

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Expense Tracker',
      home: LoginPage(),
    );
  }
}

class LoginPage extends StatefulWidget {
  // String titleInput;
  // String amountInput;
  @override
  _LoginPage createState() => _LoginPage();
}

class _LoginPage extends State<LoginPage> {
  final userName = TextEditingController();
  final password = TextEditingController();
  String textDisplay = "";

  void onSubmit() {
    setState(() {
      print(userName.text);
      if (userName.text.length == 0) {
        textDisplay = "Invalid Username";
      } else if (password.text.length == 0 && userName.text.length > 0) {
        textDisplay = "Invalid Password";
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('LOGIN'),
      ),
      body: CustomScrollView(slivers: [
        SliverFillRemaining(
          hasScrollBody: false,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Padding(
                padding: EdgeInsets.all(10),
                child: Column(
                  children: [
                    Image.asset("assets/images/nike.png"),
                    TextField(
                      controller: userName,
                      decoration: InputDecoration(
                        labelText: "Username",
                        labelStyle: TextStyle(
                          fontWeight: FontWeight.bold,
                          color: Colors.black,
                        ),
                      ),
                      keyboardType: TextInputType.emailAddress,
                      onSubmitted: (_) => onSubmit(),
                    ),
                    TextField(
                      controller: password,
                      decoration: InputDecoration(
                        labelText: "Password",
                        labelStyle: TextStyle(
                          fontWeight: FontWeight.bold,
                          color: Colors.black,
                        ),
                      ),
                      keyboardType: TextInputType.visiblePassword,
                      onSubmitted: (_) => onSubmit(),
                    ),
                    Text(
                      textDisplay,
                      style: TextStyle(
                        color: Colors.redAccent[700],
                      ),
                    ),
                  ],
                ),
              ),
              Column(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  FlatButton(
                    child: Text(
                      "Not a User. Sign Up Here.",
                      style: TextStyle(
                        color: Colors.black,
                        fontSize: 15,
                      ),
                    ),
                    onPressed: () {},
                  ),
                  FlatButton(
                    child: Text(
                      "Terms of Service. Privacy Policy.",
                      style: TextStyle(
                        color: Colors.grey[700],
                        fontSize: 13,
                      ),
                    ),
                    onPressed: () {},
                  ),
                ],
              )
            ],
          ),
        )
      ]),
    );
  }
}

If you want to properly validate the username and password you should use form validation, like: https://flutter.dev/docs/cookbook/forms/validation如果您想正确验证用户名和密码,您应该使用表单验证,例如: https://flutter.dev/docs/cookbook/forms/validation

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

相关问题 如何在android中将一组按钮对齐到屏幕底部 - how to align a set of button to bottom of the screen in android 如何在屏幕底部设置包含xml文件的包含小部件的内容 - How to set the content of include widget containing xml file at the bottom of screen $ 登录文本小部件,如何在颤振中显示特殊字符? 如何在flutter中为文本小部件设置特殊字符 - $ sign in a Text widget , how to show special characters in flutter? how to set special character to text widget in flutter 当父级是滚动视图时,如何在屏幕底部设置按钮? - how to set a button at the bottom of a screen when the parent is a scroll view? 如何使用 Flutter 构建一个可以拖动到全屏的底部表单小部件? - How to use Flutter to build a bottom sheet widget which is able to drag up to full screen? 从屏幕底部弹出一个小窗口使用什么 Widget Flutter - What Widget to use for a small Pop Up from the bottom of the screen Flutter 如何在屏幕底部显示小部件 - how to display widget at bottom of screen android 如何将按钮对准屏幕底部 - How to align a button to bottom of screen 如何将 Text/ListTile 小部件(在抽屉内)放置在手机屏幕的最底部 - How to place a Text/ListTile widget (inside Drawer) at the very bottom of the phone screen 如何在屏幕底部设置TextView - How to set a TextView at the bottom of the screen
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM