dart - 通过滑动更改路线
问题描述
我想通过简单的向左滑动来改变一个窗口,我必须有 2 个窗口,当用户向右滑动时,我想改变我的路线。
我正在使用命名路由。
void main() => runApp(new HeatMapApp());
class HeatMapApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'HeatMap',
initialRoute: '/',
routes: {
'/': (context) => new Main(),
'/home': (context) => new Home()
},
theme: new ThemeData(
primaryColor: Colors.black
)
);
}
}
这是我在我的应用程序中的代码,主文件现在没有太多数据,我想知道滑动事件重定向到“主”路径。
主要.dart
class Main extends StatelessWidget {
final bool _isLoggedIn = true;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: _isLoggedIn ? AppBar (
title: Text('Logged In')
) : null,
body: Center(
child: Text('Hello World!')
)
);
}
}
解决方案
我认为Dismissible
Widget 非常适合您的要求
class Main extends StatelessWidget {
final bool _isLoggedIn = true;
_nextPage(BuildContext context) async {
Navigator.of(context).pushReplacementNamed("/home");
}
@override
Widget build(BuildContext context) {
return Dismissible(
key: new ValueKey("dismiss_key"),
direction: DismissDirection.endToStart,
child: Scaffold(
appBar: _isLoggedIn ? AppBar(title: Text('Logged In')) : null,
body: Center(child: Text('Hello World!'))),
onDismissed: (direction) {
if (direction == DismissDirection.endToStart) {
_nextPage(context);
}
});
}
}
推荐阅读
- javascript - JQuery 按多个值对数组进行排序(加权)
- mongodb - 在 JAVA 中更新 MongoDB 文档,无需显式转换
- sql-server - 到 SQL Server 的本地数据网关,数据库名称有句点
- javascript - RXJS 调用每 30 秒更新一次的 JSON 文件
- javascript - 使用 vanilla javascript 按钮而不是 html href 触发 href
- sql - Oracle/SQL - 按月划分的销售额
- corda - 在 Corda 中,节点是否检查它们收到的消息是否来自网络映射中列出的对方节点的 IP 地址?
- python - 我怎样才能找到virtualenv python的路径
- typescript - Cypress.io + TypeScript。测试开始时的断言调用
- react-native - 对话框显示时反应本机 - 持久键盘