首页 > 解决方案 > 通过滑动更改路线

问题描述

我想通过简单的向左滑动来改变一个窗口,我必须有 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!')
        )
    );
  }

}

标签: dartflutter

解决方案


我认为DismissibleWidget 非常适合您的要求

  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);
            }
          });
    }
  }

推荐阅读