ControllerView with subpage either places content in center or gets “bottom overflowed” error when keyboard is active

I have a register view that goes through several views as steps to fill out registration information. In this setup I have trouble ensuring the content of each step is placed at the top of the screen while avoiding a “bottom overflowed” error when keyboard is active and shown.

Here is my code:

class RegisterView extends StatefulWidget {
  const RegisterView({super.key});

  @override
  State<RegisterView> createState() => _RegisterViewState();
}

class _RegisterViewState extends State<RegisterView> {
  String? _firstName;
  String? _lastName;
  String? _password;
  String? _email;

  void _onNameNext(String firstName, String lastName) {
    setState(() {
      _firstName = firstName;
      _lastName = lastName;
    });
  }

  void _onPasswordNext(String password) {
    setState(() {
      _password = password;
    });
  }

  void _onAccountCreated() {
    Navigator.of(context).pushNamedAndRemoveUntil(
      verifyEmailRoute,
      (route) => false,
    );
  }

  void _onEmailEntered(String email) {
    setState(() {
      _email = email;
    });
  }

  String _getTitle() {
    if (_firstName == null || _lastName == null) {
      return 'Hvad hedder du?';
    } else if (_password == null) {
      return 'Opret en adgangskode';
    } else {
      return 'Hvad er din email?';
    }
  }

  void _goBack() {
    if (_password != null) {
      setState(() {
        _password = null;
      });
    } else if (_firstName != null && _lastName != null) {
      setState(() {
        _firstName = null;
        _lastName = null;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(_getTitle()),
        leading: (_firstName != null || _lastName != null)
            ? IconButton(
                icon: const Icon(Icons.arrow_back),
                onPressed: _goBack,
              )
            : null,
      ),
      body: SingleChildScrollView(
        child: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              if (_firstName == null || _lastName == null)
                RegisterNameStep(
                  onNext: _onNameNext,
                  initialFirstName: _firstName,
                  initialLastName: _lastName,
                )
              else if (_password == null)
                RegisterPasswordStep(
                  onNext: _onPasswordNext,
                  initialPassword: _password,
                )
              else
                RegisterEmailStep(
                  firstName: _firstName!,
                  lastName: _lastName!,
                  password: _password!,
                  initialEmail: _email,
                  onAccountCreated: _onAccountCreated,
                ),
              const SizedBox(height: 16),
            ],
          ),
        ),
      ),
    );
  }
}

And this is the code for one of the steps. The password and email steps are structured similarly.

class RegisterNameStep extends StatefulWidget {
  final Function(String, String) onNext;
  final String? initialFirstName;
  final String? initialLastName;

  const RegisterNameStep({
    required this.onNext,
    this.initialFirstName,
    this.initialLastName,
    super.key,
  });

  @override
  State<RegisterNameStep> createState() => _RegisterNameStepState();
}

class _RegisterNameStepState extends State<RegisterNameStep> {
  late final TextEditingController _firstNameController;
  late final TextEditingController _lastNameController;
  late final FocusNode _firstNameFocusNode;
  late final FocusNode _lastNameFocusNode;
  bool _isFirstNameValid = false;
  bool _isLastNameValid = false;
  bool _isFirstNameFocused = false;
  bool _isLastNameFocused = false;

  @override
  void initState() {
    _firstNameController = TextEditingController(text: widget.initialFirstName);
    _lastNameController = TextEditingController(text: widget.initialLastName);
    _firstNameFocusNode = FocusNode();
    _lastNameFocusNode = FocusNode();
    _firstNameFocusNode.addListener(() {
      setState(() {
        _isFirstNameFocused = _firstNameFocusNode.hasFocus;
      });
    });
    _lastNameFocusNode.addListener(() {
      setState(() {
        _isLastNameFocused = _lastNameFocusNode.hasFocus;
      });
    });
    _isFirstNameValid = _firstNameController.text.isNotEmpty;
    _isLastNameValid = _lastNameController.text.isNotEmpty;
    super.initState();
  }

  @override
  void dispose() {
    _firstNameController.dispose();
    _lastNameController.dispose();
    _firstNameFocusNode.dispose();
    _lastNameFocusNode.dispose();
    super.dispose();
  }

  void _validateFirstName() {
    if (!_firstNameFocusNode.hasFocus) {
      setState(() {
        _isFirstNameValid = _firstNameController.text.isNotEmpty;
      });
    } else if (_firstNameController.text.isEmpty) {
      setState(() {
        _isFirstNameValid = false;
      });
    }
  }

  void _validateLastName() {
    if (!_lastNameFocusNode.hasFocus) {
      setState(() {
        _isLastNameValid = _lastNameController.text.isNotEmpty;
      });
    } else if (_lastNameController.text.isEmpty) {
      setState(() {
        _isLastNameValid = false;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        onTap: () => FocusScope.of(context).unfocus(),
        child: SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              const Padding(
                padding: EdgeInsets.only(bottom: 16.0),
                child: Align(
                  alignment: Alignment.centerLeft,
                  child: Text(
                    'Lots of text for why name is needed. yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada - approximately this long, but the length should be irrelevant and the view should be adaptable.',
                    style: TextStyle(fontSize: 12),
                  ),
                ),
              ),
              TextField(
                controller: _firstNameController,
                focusNode: _firstNameFocusNode,
                decoration: InputDecoration(
                  border: const OutlineInputBorder(
                    borderSide: BorderSide(color: Colors.grey),
                  ),
                  enabledBorder: const OutlineInputBorder(
                    borderSide: BorderSide(color: Colors.grey),
                  ),
                  focusedBorder: const OutlineInputBorder(
                    borderSide: BorderSide(color: Colors.grey),
                  ),
                  errorBorder: const OutlineInputBorder(
                    borderSide: BorderSide(color: Colors.grey),
                  ),
                  labelText: 'Fornavn',
                  suffixIcon: _firstNameController.text.isNotEmpty &&
                          _isFirstNameFocused
                      ? GestureDetector(
                          onTap: () {
                            _firstNameController.clear();
                            setState(() {
                              _isFirstNameValid = false;
                            });
                          },
                          child: const Icon(Icons.cancel,
                              color: Colors.grey, size: 20),
                        )
                      : null,
                ),
                onChanged: (_) {
                  setState(() {
                    _isFirstNameValid = _firstNameController.text.isNotEmpty;
                  });
                },
              ),
              const SizedBox(height: 16),
              TextField(
                controller: _lastNameController,
                focusNode: _lastNameFocusNode,
                decoration: InputDecoration(
                  border: const OutlineInputBorder(
                    borderSide: BorderSide(color: Colors.grey),
                  ),
                  enabledBorder: const OutlineInputBorder(
                    borderSide: BorderSide(color: Colors.grey),
                  ),
                  focusedBorder: const OutlineInputBorder(
                    borderSide: BorderSide(color: Colors.grey),
                  ),
                  errorBorder: const OutlineInputBorder(
                    borderSide: BorderSide(color: Colors.grey),
                  ),
                  labelText: 'Efternavn',
                  suffixIcon:
                      _lastNameController.text.isNotEmpty && _isLastNameFocused
                          ? GestureDetector(
                              onTap: () {
                                _lastNameController.clear();
                                setState(() {
                                  _isLastNameValid = false;
                                });
                              },
                              child: const Icon(Icons.cancel,
                                  color: Colors.grey, size: 20),
                            )
                          : null,
                ),
                onChanged: (_) {
                  setState(() {
                    _isLastNameValid = _lastNameController.text.isNotEmpty;
                  });
                },
              ),
              const SizedBox(height: 8),
              if (!_isFirstNameValid || !_isLastNameValid)
                const Padding(
                  padding: EdgeInsets.only(top: 8.0),
                  child: Align(
                    alignment: Alignment.centerLeft,
                    child: Text(
                      'Både fornavn og efternavn skal udfyldes',
                      style: TextStyle(color: Colors.grey, fontSize: 12),
                    ),
                  ),
                ),
              const SizedBox(height: 16),
              ElevatedButton(
                onPressed: _isFirstNameValid && _isLastNameValid
                    ? () => widget.onNext(
                        _firstNameController.text, _lastNameController.text)
                    : null,
                child: const Text('Næste'),
              ),
              TextButton(
                onPressed: () {
                  Navigator.of(context).pushNamedAndRemoveUntil(
                    loginRoute,
                    (route) => false,
                  );
                },
                child: const Text('Har du allerede en profil?'),
              ),
            ],
          ),
        ));
  }
}

How can I make sure the content is placed at the top of the page and avoid getting overflow errors? I am testing this on an old Android device (Samsung j7) where the screen is small enough and the entire content of the page can’t be shown together with the keyboard within the view frame.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật