【问题标题】:Prevent background from scrolling when using keyboard in Flutter在 Flutter 中使用键盘时防止背景滚动
【发布时间】:2021-12-22 11:43:56
【问题描述】:

我希望背景滚动以聚焦文本字段,但让背景图像保持其位置/纵横比。

我尝试过的:

  • 用一个包含背景图片的 BoxDecoration 将我的 Scaffold 包装在一个容器中
  • 用堆栈包裹我的脚手架,其中包括作为背景的图像
  • 在我的 Scaffold 中禁用了 resizeToAvoidBottomInset

使用列出的第一个和第三个选项:

class LoginPage extends BasePage {
  LoginPage({Key? key}) : super(key, configTypes: [ConfigurationType.login]);
  
  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends BaseState<LoginPage> with BasicPage {
  @override
  Widget body() {
    return Container(
      decoration: const BoxDecoration(
        image: DecorationImage(
          image: ExactAssetImage(Assets.bgLogin), // Assets is constant to filepath
          fit: BoxFit.cover,
        ),
      ),
      child: Scaffold(
        resizeToAvoidBottomInset: false,
        backgroundColor: Colors.transparent, // To see background image
        body: SafeArea(
          child: ... // Contains textfield/other UI elements
        ),
      ),
    );
  }
}

下面是背景滚动的演示:

【问题讨论】:

    标签: flutter dart keyboard textfield


    【解决方案1】:

    我认为您应该将装有容器的 Scaffold 退回。 您正在阻止图像在 Scaffold 上滚动,但不能阻止父 Countainer 的滚动。

    class _LoginPageState extends BaseState<LoginPage> with BasicPage {
      @override
      Widget body() {
        return Scaffold(
            resizeToAvoidBottomInset: false,
            backgroundColor: Colors.transparent, // To see background image
            body: SafeArea(
              child: Container(
            decoration: const BoxDecoration(
            image: DecorationImage(
              image: ExactAssetImage(Assets.bgLogin), // Assets is constant to filepath
              fit: BoxFit.cover,
            ),
          ),
          child: ... // Contains textfield/other UI elements
            ),
          ),
        );
      }
    

    【讨论】:

    • 很遗憾,这并没有解决我的问题。
    • 首页的代码(使用LoginPage的地方)是什么?
    • LoginPage 是整个页面,我使用 nav 2.0 导航到它。减去脚手架的子主体、应用栏和图像文件,所有内容都包含在内。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-19
    • 1970-01-01
    • 2018-06-03
    • 2018-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多