Flutter/Dart Scrolling textfield above keyboard

后端 未结 3 1342
伪装坚强ぢ
伪装坚强ぢ 2021-01-06 11:44

I am using textfields, how can I make it so it scrolls up a bit when entering email and password?

Here is a preview of the code

https://pastebin.com/4EngQDV

3条回答
  •  萌比男神i
    2021-01-06 12:05

    Ok first, the code you pasted is incomplete, so I'm guessing you are having those textfields insides a Column. You have two options:

    1st) In your Scaffold you can set this property to false like resizeToAvoidBottomInset: false,
    2o) You can use a SingleChildScrollView that will move up your textfield when the keyboard appears eg.:

    import 'package:flutter/material.dart';
    import 'package:font_awesome_flutter/font_awesome_flutter.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: Scaffold(
            body: SingleChildScrollView(
              child: MyLoginPage(title: 'Flutter Demo Home Page'),
            ),
          ),
        );
      }
    }
    
    class MyLoginPage extends StatefulWidget {
      MyLoginPage({Key key, this.title}) : super(key: key);
    
      final String title;
    
      @override
      _MyLoginPageState createState() => _MyLoginPageState();
    }
    
    class _MyLoginPageState extends State {
      String _email;
      String _password;
      TextStyle style = TextStyle(fontSize: 25.0);
    
      @override
      Widget build(BuildContext context) {
        final emailField = TextField(
          obscureText: false,
          style: style,
          decoration: InputDecoration(
              contentPadding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
              prefixIcon: Icon(FontAwesomeIcons.solidEnvelope),
              hintText: "Email",
              focusedBorder: OutlineInputBorder(
                  borderSide: BorderSide(color: Colors.red[300], width: 32.0),
                  borderRadius: BorderRadius.circular(97.0))),
          onChanged: (value) {
            setState(() {
              _email = value;
            });
          },
        );
        final passwordField = TextField(
          obscureText: true,
          style: style,
          decoration: InputDecoration(
              contentPadding: EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
              prefixIcon: Icon(FontAwesomeIcons.key),
              hintText: "Password",
              focusedBorder: OutlineInputBorder(
                  borderSide: BorderSide(color: Colors.red[300], width: 32.0),
                  borderRadius: BorderRadius.circular(25.0))),
          onChanged: (value) {
            setState(() {
              _password = value;
            });
          },
        );
    
        return Center(
          child: Column(
            children: [
              Container(
                color: Colors.yellow[300],
                height: 300.0,
              ),
              emailField,
              passwordField
            ],
          ),
        );
      }
    }
    

    Just copy and paste the code, and see if it's what you want.
    Hope this help.

提交回复
热议问题