0

i want to build an app swiping container and gives random word. im using gesturedetector to swipe but i cant make it work properly, i want to swipe and take random word but swiping must be swipe(when touched to screen gives random word).

my idea, take starting position and ending position of swiping if value more than 5 (or -5 direction doesnt matter) give random word

for testing only gives random numbers

onPanUpdate: (details) {
    setState(() {

      if(swipingLength>5||swipingLenght<-5){
      int randomNumber = random.nextInt(100);
      num= randomNumber;
      }
      
    });
B. Mercan
  • 51
  • 1
  • 9
  • Please try to use proper grammar and syntax to make your question easier to comprehend. This link might help you to find your answer: https://stackoverflow.com/a/55050804/15117201 – Jahn E. Nov 29 '21 at 21:17

1 Answers1

0

Here the code for displayWord call at user swipe :

import 'package:flutter/material.dart';
import 'dart:async';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        body: SafeArea(
          child: MyHomePage(),
        ),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage>
    with SingleTickerProviderStateMixin {
 String txt ='';
  @override
  void initState() {
    super.initState();
  }


  void displayWord() {
   
      
        setState(() {
          txt = 'random txt';
        }); 
        print('User swiped');
      
      
    }
  

  @override
  Widget build(BuildContext context) {
    return Stack(children: [
      SwipeZone(displayWord: displayWord),
      Container(child: Text('$txt'))]);
  }
}

class SwipeZone extends StatefulWidget {
  const SwipeZone({Key? key, required this.displayWord})
      : super(key: key);

  final void Function()displayWord;
  
  @override
  _SwipeZoneState createState() => _SwipeZoneState();
}

class _SwipeZoneState extends State<SwipeZone> {
  double startPointX = 0;
 
  void onPanStart(DragStartDetails details) {
    startPointX = details.globalPosition.dx;
   
  }

  @override
bool _enabled = true ;
  void onPanUpdate(DragUpdateDetails details) {
    var x = details.globalPosition.dx;
    var endPointX = x - startPointX;
   if(endPointX>100){
    widget.displayWord();
     setState(() {
       _enabled = false;
     }); 
      
      Timer(Duration(seconds: 1), ()
          {setState(() {
             _enabled = true;
          });}); /*Make the Swipe Stop, user can swipe every 1 second*/
   }
  }

  Widget build(BuildContext context) {
    return GestureDetector(
      onPanStart: onPanStart,
      onPanUpdate: _enabled ? onPanUpdate: null,
      child: Container(color: Colors.grey.withOpacity(0.5)),
    );
  }
}