Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
cddqssc opened this issue May 6, 2024 · 2 comments
Open

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

cddqssc opened this issue May 6, 2024 · 2 comments

Comments

@cddqssc
Copy link

cddqssc commented May 6, 2024

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
Copy link
Owner

FMorschel commented May 6, 2024

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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants