mylisabox / flutter_mjpeg

Flutter widget to show mjpeg stream from URL
BSD 2-Clause "Simplified" License
32 stars 24 forks source link

Stream value update #19

Closed Ahsanmughal closed 2 years ago

Ahsanmughal commented 2 years ago

Hi @jaumard

is there any way to update or change the stream on button click.

jaumard commented 2 years ago

Hello :)

Yes just change it :) it is easy as that ^^

Ahsanmughal commented 2 years ago

hope it is as easy as you mentioned, i'm new in flutter, please check the example code. ` import 'package:flutter/material.dart'; import 'package:flutter_hooks/flutter_hooks.dart'; import 'package:flutter_mjpeg/flutter_mjpeg.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: MyHomePage(), ); } }

class MyHomePage extends HookWidget { @override Widget build(BuildContext context) { final isRunning = useState(true); String statevalue = '5552'; return Scaffold( appBar: AppBar( title: Text('Flutter Demo Home Page'), ), body: Column( children: [ Expanded( child: Center( child: Mjpeg( isLive: isRunning.value, error: (context, error, stack) { return Text(error.toString(), style: TextStyle(color: Colors.red)); }, stream: 'http://forexample.com/mobile_stream?port=' + statevalue, ), ), ), Row( children: [ RaisedButton( onPressed: () { statevalue = '5551'; }, child: Text('Toggle'), ), RaisedButton( onPressed: () { Navigator.of(context).push(MaterialPageRoute( builder: (context) => Scaffold( appBar: AppBar(), ))); }, child: Text('Push new route'), ), ], ), ], ), ); } } `

jaumard commented 2 years ago

I suggest that you read/learn how to update state in flutter :)
Here you update your variable but the widget is not rebuilt so it didn't really change. You can use useState('5552'); instead and it will fix your issue because it will trigger a rebuild.