0

I mounted a trial code using a ListView widget to show three cards: Result(), ImputField(callbackFunction: callback) and VideoStreamer() item.

Is it possible that the callbackFunction only rebuilds the Result() widget, so that a running video is not reloaded every time a new value is submitted form the InputFiled?

Thanks Martin

import 'package:flutter/material.dart';
import 'package:mustoappmodel/reusable.dart';

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

  @override
  State<Home> createState() => _HomeState();
}

String value = '';

class _HomeState extends State<Home> {
  void callback(String result) {
    value = result;
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('test module')),
      body: ListView(
        children: [
          Result(), // Shows whats is put in InputField
          ImputField(callbackFunction: callback),
          VideoStreamer(), // a steram should not be interrupted
        ],
      ),
    );
  }
}

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

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

  @override
  State<Result> createState() => _ResultState();
}

class _ResultState extends State<Result> {
  @override
  Widget build(BuildContext context) {
    return Container(
      height: 150,
      margin: const EdgeInsets.all(10),
      alignment: Alignment.centerLeft,
      decoration: BoxDecoration(
        color: Colors.grey,
        borderRadius: const BorderRadius.all(Radius.circular(20)),
      ),
      child: Padding(
        padding: const EdgeInsets.all(15.0),
        child: Row(children: [Text(value, textAlign: TextAlign.center)]),
      ),
    );
  }
}

class ImputField extends StatelessWidget {
  ImputField({super.key, required this.callbackFunction});
  final Function callbackFunction;

  final TextEditingController _txtController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 150,
      margin: const EdgeInsets.all(10),
      alignment: Alignment.centerLeft,
      decoration: BoxDecoration(
        color: Colors.grey,
        borderRadius: const BorderRadius.all(Radius.circular(20)),
      ),
      child: Padding(
        padding: const EdgeInsets.all(15.0),
        child: TextField(
          textAlign: TextAlign.center,
          keyboardType: TextInputType.number,
          controller: _txtController,
          onSubmitted: (value) {
            print('here: $value');
            callbackFunction(value);
            _txtController.clear();
          },
        ),
      ),
    );
  }
}

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

  @override
  State<VideoStreamer> createState() => _VideoStreamerState();
}

class _VideoStreamerState extends State<VideoStreamer> {
  @override
  Widget build(BuildContext context) {
    return Container(
      height: 150,
      margin: const EdgeInsets.all(10),
      alignment: Alignment.centerLeft,
      decoration: BoxDecoration(
        color: Colors.grey,
        borderRadius: const BorderRadius.all(Radius.circular(20)),
      ),
    );
  }
}
7
  • What package are you using for video streaming? Your code is also missing in the minimal code. Commented Aug 31 at 10:09
  • I checked your code and I think it should not have any problem. You should update your question with more information and codes in your video streamer class for us to be able to try and solve it. Commented Aug 31 at 10:48
  • Yes, it's totally possible – and actually a good idea – to update only the Result() widget without restarting or reloading the video in VideoStreamer(). Commented Aug 31 at 11:12
  • The issue you're running into is that you're calling setState() in the Home widget, which causes the whole widget tree to rebuild. That includes your video, so it gets interrupted every time there's a new input. Commented Aug 31 at 11:13
  • I also tested the code with youtube_player_flutter package and there is not restart of the video on submitting the result! Commented Aug 31 at 11:39

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.