Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change route with Swipe

Tags:

flutter

dart

I want to change a window with a simple swipe to the left, I have to 2 windows and when user swipes to the right side, I want to change my route.

I'm working with Named Routes.

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

This is my code in my App, the Main file doesn't have too much data now, I want to know the swipe event to redirect to 'home' path.

Main.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!')
        )
    );
  }

}

like image 200
Joseph Arriaza Avatar asked Oct 16 '22 14:10

Joseph Arriaza


1 Answers

I think Dismissible Widget fits perfect on your requirement

  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);
            }
          });
    }
  }
like image 94
diegoveloper Avatar answered Oct 21 '22 02:10

diegoveloper