FMorschel / floating_overlay

A widget wrapper that allows a floating widget be dragged and rescaled
MIT License
7 stars 3 forks source link

[Need HELP] How to update the content of floatingChild #8

Open cddqssc opened 6 months ago

cddqssc commented 6 months ago

Thank you for providing such a useful plugin. I want to know how to update the content of floatingChild: Such as:

                  floatingChild: SizedBox(
                    width: width - 1,
                    height: height / 2.5,
                    child: Container(
                      decoration: BoxDecoration(
                        color: Theme.of(context).primaryColor,
                        borderRadius: BorderRadius.circular(10),
                        border: Border.all(
                          color: Theme.of(context).colorScheme.primary,
                          width: 1.0,
                        ),
                      ),
                      **_child: Text("${str}"),_**
                      //CustomPaint(painter: SpecPainter(context: context)),
                    ),
                  ),

When str has changed and setstate has been called, the str in FloatingOverlay has not changed. The str display is updated only when resize or position is moved.

FMorschel commented 6 months ago

Yes, this is probably not actually in the same scope as the setState you're using. For this, you can look into using any state management solution you'd like. I'd suggest you try to use StatefulBuilder (which gives you its own setState function), ValueListenableBuilder with a ValueNotifier (my preferred option) or even Streams for Dart/Flutter built-in support.

Little example:

import 'package:flutter/material.dart';

void main() => runApp(
      const MaterialApp(
        home: MyHomePage(),
      ),
    );

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

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  ValueNotifier<int> _counter = ValueNotifier<int>(0);

  void _incrementCounter() {
    _counter.value++;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter Demo Home Page'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const Text(
              'You have pushed the button this many times:',
            ),
            ValueListenableBuilder(
              valueListenable: _counter,
              builder: (context, value, _) {
                return Text(
                  '$value',
                  style: Theme.of(context).textTheme.headlineMedium,
                );
              }
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}
FMorschel commented 6 months ago

Please let me know if you were able to solve your problem!