Created
May 24, 2025 18:06
-
-
Save fredgrott/2ed01efa700bfd4b9a04ec30594d3a91 to your computer and use it in GitHub Desktop.
flutter box transform example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:flutter_box_transform/flutter_box_transform.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Box Transform Demo', | |
debugShowCheckedModeBanner: false, | |
theme: ThemeData( | |
colorScheme: ColorScheme.fromSeed( | |
seedColor: Colors.deepPurple, | |
brightness: Brightness.dark, | |
), | |
useMaterial3: true, | |
), | |
home: const MyHomePage(), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
const MyHomePage({super.key}); | |
@override | |
State<MyHomePage> createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
late Rect rect = Rect.fromCenter( | |
center: MediaQuery.of(context).size.center(Offset.zero), | |
width: 400, | |
height: 300, | |
); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: Stack( | |
fit: StackFit.expand, | |
children: [ | |
TransformableBox( | |
rect: rect, | |
clampingRect: Offset.zero & MediaQuery.sizeOf(context), | |
onChanged: (result, event) { | |
setState(() { | |
rect = result.rect; | |
}); | |
}, | |
contentBuilder: (context, rect, flip) { | |
return DecoratedBox( | |
decoration: BoxDecoration( | |
border: Border.all( | |
color: Theme.of(context).colorScheme.primary, | |
), | |
image: const DecorationImage( | |
image: AssetImage('assets/image1.png'), | |
fit: BoxFit.fill, | |
), | |
), | |
); | |
}, | |
), | |
], | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment