dart_pad_widget icon indicating copy to clipboard operation
dart_pad_widget copied to clipboard

updating text in dart pad not working.

Open cmedamine opened this issue 5 years ago • 2 comments

at this time i'm working a project and i'm tryping to update the "code" from dart pad widget using setState method but it won't update i dont know what's the issue here so this is my code below:

import 'package:flutter/material.dart';
import 'package:dart_pad_widget/dart_pad_widget.dart';
import 'package:flutter/foundation.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  final keyValue = Key('codeOne');

  TextEditingController promptController = new TextEditingController();
  TextEditingController tokenController = new TextEditingController();

  String response;
  int token = 50;

  @override
  void initState() {
    super.initState();
    promptController = TextEditingController();
    tokenController = TextEditingController();
  }

  @override
  void dispose() {
    super.dispose();
    promptController.dispose();
    tokenController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Container(
                  margin: EdgeInsets.only(top: 30),
                  width: 600,
                  child: TextField(
                    controller: promptController,
                    keyboardType: TextInputType.multiline,
                    maxLines: null,
                    decoration: InputDecoration(
                      hintText: 'Enter your request.',
                      border: OutlineInputBorder(),
                    ),
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(top: 10, bottom: 10),
                  width: 600,
                  child: TextField(
                    controller: tokenController,
                    decoration: InputDecoration(
                      hintText: 'Enter max tokens (default is 50)',
                      border: OutlineInputBorder(),
                    ),
                    onChanged: (tokenValue) {
                      token = int.parse(tokenValue);
                    },
                  ),
                ),
                Container(
                  width: 600,
                  child: FlatButton(
                    key: keyValue,
                    child: Text('DONE'),
                    textColor: Colors.white,
                    color: Colors.blue,
                    onPressed: () {
                      setState(() {
                        response = 'Hello'; //
                      });
                    },
                  ),
                ),
                SizedBox(height: 20),
                DartPad(
                  //dart output not showing after set state.
                  key: keyValue,
                  flutter: true,
                  width: 1024,
                  height: 600,
                  code: response,
                ),
                SizedBox(height: 15),
              ],
            ),
          ],
        ),
      ),
    );
  }
}

cmedamine avatar Dec 17 '20 20:12 cmedamine

This has to do with how the dartpad works from the html / javascript standpoint. For now you can change the key as well as the code. If you want to look into a better fix, such as maybe using a hash of the code as the default key. I'd happily review and accept a PR.

However, I'm busy with other things at the moment.

TimWhiting avatar Dec 17 '20 22:12 TimWhiting

oh ok tanx, i'm a flutter and dart beginner hahaha, so this confused me little bit.

cmedamine avatar Dec 18 '20 09:12 cmedamine