swipe_plus 0.8.0
swipe_plus: ^0.8.0 copied to clipboard
Simple way to implement horizontal drag interaction.
import 'dart:math';
import 'package:faker/faker.dart';
import 'package:flutter/material.dart';
import 'package:swipe_plus/swipe_plus.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
useMaterial3: true,
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Drag And Swipe'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: const Color(0xffFFFBFE),
elevation: 10,
title: Text(widget.title),
),
body: ListView.builder(
padding: const EdgeInsets.symmetric(vertical: 16),
itemBuilder: (context, index) => MessageWidget(
textMessage: faker.lorem.sentence(),
),
),
);
}
}
class MessageWidget extends StatelessWidget {
const MessageWidget({
super.key,
required this.textMessage,
});
final String textMessage;
void showSnackBar(BuildContext context, String message) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('Reply to $textMessage'),
behavior: SnackBarBehavior.floating,
));
}
@override
Widget build(BuildContext context) {
final size = MediaQuery.of(context).size;
final x = Random().nextBool();
return SwipePlus(
onDragComplete: () => showSnackBar(context, textMessage),
maxTranslation: .3,
minThreshold: .50,
alignment: x ? Alignment.centerRight : Alignment.centerLeft,
child: ConstrainedBox(
constraints: BoxConstraints(
maxWidth: size.width * .60,
),
child: Container(
margin: const EdgeInsets.symmetric(vertical: 5, horizontal: 10),
padding: const EdgeInsets.all(10),
decoration: BoxDecoration(
color: const Color(0xffFFD8E4),
borderRadius: BorderRadius.circular(12),
),
child: Text(
textMessage,
style: const TextStyle(color: Color(0xff31111D)),
),
),
),
);
}
}