Nitesh Sharma
Nitesh Sharma

Reputation: 23

How can I achieve the property swipe right and swipe left in flutter

I am seeking for whenever a user swipes right, then it would be gone on other page and in the same page if I again the swipe left it would be return me on a home page (from where I started to swipe right).

For a proper idea I am asking about like Instagram whenever I swipe right it will goes to you message section and in the same page if swipe left it will be return me on the main screen

Upvotes: 0

Views: 1474

Answers (2)

lenz
lenz

Reputation: 2425

Try Gestures widget. Look at ‘onHorizontalDrag’

Upvotes: 0

Ali Qanbari
Ali Qanbari

Reputation: 3121

There is 2 ways that I can think of to do that:

1.The first and easier way is to use a PageView:

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp>{
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView(
        children: [
          FirstPage(),
          SecondPage(),
          ThirdPage(),
        ],
      ),
    );
  }
}
  1. The second way is to use a TabBarView:
class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> with SingleTickerProviderStateMixin{
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: TabBarView(
        controller: TabController(length: 3, vsync: this),
        children: [
          FirstPage(),
          SecondPage(),
          ThirdPage(),
        ],
      ),
    );
  }
}

If you have a tab bar use TabBarView otherwise PageView is better in every other way.

Upvotes: 1

Related Questions