flutter - 如何在颤动中保持自定义小部件在多个屏幕上通用
问题描述
我正在制作一个颤振应用程序,其中我需要两个放置一个在多个屏幕上常见的小部件。这是屏幕一的构建方法的代码
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: app_color,
body: _isLoading == false ? Stack(
new Container(//used for whole screen ),
Positioned(
left: 0,
right: 0,
bottom: 0,
//a bottom tab like which is common across screens
),
],
)
:
//code to show progress indicator while loading data in background
Stack(
children: <Widget>[
Center(
child:Container(
height: 50,
width: 50,
child: CircularProgressIndicator(),
)
),
Positioned(
left: 0,
right: 0,
bottom: 0,
//keep this to show bottom tab while loading
),
],
)
);
}
上面的代码在屏幕底部有一个定位小部件,我想在多个屏幕上保持通用?我怎样才能实现它?我了解android,在这种情况下,我可以使用片段事务来实现,但在这里我必须在所有屏幕上保持底部定位的小部件,问题是在更改屏幕底部位置后,小部件会消失一段时间,但我想要那个底部小部件保持静态并仅更改屏幕而不是底部定位的小部件
解决方案
实现此目的的一种方法是在 MaterialApp 小部件中自定义构建器。
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Test',
home: TestPage(),
initialRoute: "/test",
builder: (context, child) {
return Scaffold(
body: Stack(
children: [
child!,
Positioned(
left: 0,
right: 0,
bottom: 0,
),
],
),
);
},
routes: routes(context),
);
}
}
这将在所有屏幕中呈现小部件,并且屏幕之间的过渡动画不会弄乱它,并且您可以像往常一样在页面中自由使用其他 Scaffolds。
推荐阅读
- google-api - 人员 API - 超出 QUOTA_EXCEEDED / FBS 配额限制
- javascript - 如何为 React 中的无限嵌套导航正确设置样式和应用悬停?
- php - PHP文件没有更新?
- javascript - 为什么我的 JavaScript 日期显示错误的日期?
- javascript - IndexedDB - 新版本的数据库
- java - 部署到测试环境 linux tomcat 时,Jsp 更改未反映。在本地它工作正常
- c# - 定义 IQueryable 的正确语法
.OrderBy() 类方法 - python - 线性回归打印预测值
- python-3.x - 在这个问题中我必须使用什么样的功能?
- python - 是否有用于查看文档字符串的 vim 插件