简体   繁体   中英

Freely moveable Flutter Widget

I need a special Widget.

Hey, I need the name pros.

Is there a widget that can be moved freely. Like how you can just move on with maps?

So basically scrollable in all directions.

You can check InteractiveViewer Her is a basic demo:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: _title,
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  late Offset _offset;

  @override
  void initState() {
    _offset = const Offset(0, 0);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(_offset.toString())),
      body: SizedBox.expand(
        child: InteractiveViewer(
          onInteractionUpdate: (details) => setState(() {
            _offset = details.focalPoint;
          }),
          boundaryMargin: const EdgeInsets.all(1000.0),
          minScale: 0.1,
          maxScale: 3,
          child: Center(
            child: TextButton(
              child: const Text('Drag me'),
              onPressed: () {
              },
            ),
          ),
        ),
      ),
    );
  }
}

You can use draggable widget for that simply wrap your widget like this

Draggable(
              data: 'Flutter',
              child: FlutterLogo(
                size: 100.0,
              ),
              feedback: FlutterLogo(
                size: 100.0,
              ),
              childWhenDragging: Container(),
            )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM