gpt4 book ai didi

flutter - 为什么 build() 在后台的屏幕上被多次调用?

转载 作者:行者123 更新时间:2023-12-05 07:26:55 29 4
gpt4 key购买 nike

我创建了一个类似于 Navigate to a new screen and back 的示例项目例子。我在每个屏幕上都有 TextFieldautofocus: true 的唯一区别:

import 'package:flutter/material.dart';

void main() {
runApp(MaterialApp(
title: 'Navigation Basics',
home: FirstScreen(),
));
}

class FirstScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
print("FirstScreen build");
return Scaffold(
body: Column(
children: <Widget>[
RaisedButton(
child: Text('Next screen'),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondScreen()),
);
},
),
TextField(
decoration: InputDecoration(),
onEditingComplete: () {},
autofocus: true,
)
],
),
);
}
}

class SecondScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
print("SecondScreen build");
return Scaffold(
body: Column(
children: <Widget>[
RaisedButton(
child: Text('Next screen'),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => ThirdScreen()),
);
},
),
TextField(
decoration: InputDecoration(),
onEditingComplete: () {},
autofocus: true,
)
],
),
);
}
}

class ThirdScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
print("ThirdScreen build");
return Scaffold(
body: Column(
children: <Widget>[
RaisedButton(
child: Text('Next screen'),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => FourthScreen()),
);
},
),
TextField(
decoration: InputDecoration(),
onEditingComplete: () {},
autofocus: true,
)
],
),
);
}
}

class FourthScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
print("FourthScreen build");
return Scaffold(
body: Column(
children: <Widget>[
TextField(
decoration: InputDecoration(),
onEditingComplete: () {},
autofocus: true,
)
],
),
);
}
}

当我从 ThirdScreen 导航到 FourthScreen 时,我的日志如下所示:

I/flutter ( 7523): FourthScreen build
I/flutter ( 7523): SecondScreen build
I/flutter ( 7523): ThirdScreen build
I/flutter ( 7523): FourthScreen build
I/flutter ( 7523): SecondScreen build
I/flutter ( 7523): ThirdScreen build
I/flutter ( 7523): FourthScreen build
  1. 为什么第一次构建FourthScreen后又调用SecondScreen、ThirdScreen和FourthScreen构建?
  2. 为什么后台堆栈中的屏幕会在键盘弹出时重新绑定(bind)?
  3. 为什么不重建 FirstScreen?

最佳答案

我尝试运行您提供的重现,但无法重现您报告的问题。导航到下一个屏幕后不会重建以前的屏幕。

demo

flutter 医生

[✓] Flutter (Channel stable, 1.22.3, on Mac OS X 10.15.7 19H2, locale en)
• Flutter version 1.22.3
• Framework revision 8874f21e79 (5 days ago), 2020-10-29 14:14:35 -0700
• Engine revision a1440ca392
• Dart version 2.10.3

[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
• Platform android-29, build-tools 29.0.2
• Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
• All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 12.0.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 12.0.1, Build version 12A7300
• CocoaPods version 1.9.0

[!] Android Studio (version 4.1)
• Android Studio at /Applications/Android Studio.app/Contents
✗ Flutter plugin not installed; this adds Flutter specific functionality.
✗ Dart plugin not installed; this adds Dart specific functionality.
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)

[✓] VS Code (version 1.50.1)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.15.1

[✓] Connected device (2 available)
• AOSP on IA Emulator (mobile) • emulator-5554 • android-x86 • Android 9 (API 28) (emulator)
• iPhone 11 (mobile) • 216904B4-E876-4AC5-9BCF-F77CD19B9338 • ios • com.apple.CoreSimulator.SimRuntime.iOS-14-0 (simulator)

如果您仍然遇到问题,请提供最小的重现以及复制行为的步骤并在此处提交 http://github.com/flutter/flutter/issues/

关于flutter - 为什么 build() 在后台的屏幕上被多次调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54185734/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com