ethanblake4 / flutter_eval

Code push for Flutter, powered by dart_eval
https://pub.dev/packages/flutter_eval
BSD 3-Clause "New" or "Revised" License
302 stars 28 forks source link

Use EvalWidget nested inside other widgets #81

Open kishan-character opened 4 months ago

kishan-character commented 4 months ago

I'm able to get the demo working, and was trying to modify it so that I could only render some widgets from the server side within the existing flutter app is this possible or is this a known limitation? Should I use hot swapping for that instead?

When I try and run the following code it just shows a white screen:

import 'package:flutter/material.dart';
import 'package:flutter_eval/flutter_eval.dart';

void main() {
  runApp(const Wrapper());
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'appp demo',
        home: Scaffold(
            appBar: AppBar(
              title: const Text("bruh"),
            ),
            body: const Center(
                child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                  Text("yooo", style: TextStyle(color: Colors.black)),
                  // EvalExample(),
                ]))));
  }
}

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

  @override
  Widget build(BuildContext context) {
    return const EvalWidget(
      packages: {
        'example': {
          'main.dart': '''
            import 'package:flutter/material.dart';

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

              @override
              IncrementDemoState createState() => IncrementDemoState();
            }

            class IncrementDemoState extends State<IncrementDemo> {
              int _counter = 0; // Initial counter value

              void _incrementCounter() {
                setState(() {
                  _counter += 4; // Increment counter by 4
                });
              }

              @override
              Widget build(BuildContext context) {
                return Center(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      const Text(
                        'You have pushed the button this many timessss:',
                      ),
                      Text(
                        '\$_counter', // Display the current value of _counter
                        style: Theme.of(context).textTheme.headlineMedium,
                      ),
                      const SizedBox(
                          height: 20), // Adds a space between the text and the button
                      ElevatedButton(
                        onPressed: _incrementCounter,
                        child: const Text('Increment by 4'),
                      ),
                    ],
                  ),
                );
              }
            }

            '''
        }
      },

      /// In debug mode, flutter_eval will continually re-generate a compiled
      /// EVC bytecode file for the given program, and save it to the specified
      /// assetPath. During runtime, it will instead load the compiled EVC file.
      /// To ensure this works, you must add the file path to the assets section of
      /// your pubspec.yaml file.
      assetPath: 'assets/program.evc',

      /// Specify which library (i.e. which file) to use as an entrypoint.
      library: 'package:example/main.dart',

      /// Specify which function to call as the entrypoint.
      /// To use a constructor, use "ClassName.constructorName" syntax. In
      /// this case we are calling a default constructor so the constructor
      /// name is blank.
      function: 'MyApp.',

      /// Specify the arguments to pass to the entrypoint. Generally these
      /// should be dart_eval [$Value] objects, but when invoking a static or
      /// top-level function or constructor, [int]s, [double]s, and [bool]s
      /// should be passed directly.
      args: [null],
    );
  }
}
wrbl606 commented 3 months ago

In the code you provided, the dynamic code declares a IncrementDemo class, but the function parameter on the EvalWidget makes it look for MyApp class, which doesn't exist.