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

Sending new widget to the second screen #9

Open
EbramTawfik opened this issue Jul 13, 2021 · 7 comments
Open

Sending new widget to the second screen #9

EbramTawfik opened this issue Jul 13, 2021 · 7 comments

Comments

@EbramTawfik
Copy link
Contributor

I modified the example and added a button new screen to update the value of current widget. I created a provider service which I wanted to use to pass the widget between the two screen but for some reason whenever I get the value of current widget from the service in the second screen it's always null. I think the second screen is a different process

here is the code:

import 'package:flutter/material.dart';
import 'package:presentation_displays/displays_manager.dart';
import 'package:presentation_displays/display.dart';
import 'package:presentation_displays/secondary_display.dart';
import 'package:provider/provider.dart';
import 'package:provider/single_child_widget.dart';

class PresenterService {
  Widget current;
}

Route<dynamic> generateRoute(RouteSettings settings) {
  switch (settings.name) {
    case '/':
      return MaterialPageRoute(builder: (_) => DisplayManagerScreen());
    case 'presentation':
      return MaterialPageRoute(builder: (_) => SecondaryScreen());
    default:
      return MaterialPageRoute(
          builder: (_) => Scaffold(
                body: Center(
                    child: Text('No route defined for ${settings.name}')),
              ));
  }
}

class ProviderInjector {
  static List<SingleChildWidget> providers = [
    Provider.value(value: PresenterService()),
  ];
}

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: ProviderInjector.providers,
      child: MaterialApp(
        onGenerateRoute: generateRoute,
        initialRoute: '/',
      ),
    );
  }
}

class Button extends StatelessWidget {
  Button(this.title, this.function);

  final String title;
  final VoidCallback function;

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.all(4.0),
      child: RaisedButton(
        onPressed: function,
        child: Text(
          title,
          style: TextStyle(fontSize: 40),
        ),
      ),
    );
  }
}

/// Main Screen
class DisplayManagerScreen extends StatefulWidget {
  @override
  _DisplayManagerScreenState createState() => _DisplayManagerScreenState();
}

class _DisplayManagerScreenState extends State<DisplayManagerScreen> {
  DisplayManager displayManager = DisplayManager();
  List<Display> displays = [];
  int num = 0;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Plugin example app'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Wrap(
              alignment: WrapAlignment.center,
              children: <Widget>[
                Button("Get Displays", () async {
                  final values = await displayManager.getDisplays();
                  print(values);
                  displays.clear();
                  displays.addAll(values);
                  print(displays);
                }),
                Button("Show presentation", () async {
                  displayManager.showSecondaryDisplay(
                      displayId: displays[1]?.displayId ?? -1,
                      routerName: "presentation");
                }),
                Button("New Secreen", () async {
                  PresenterService service =
                      Provider.of(context, listen: false);
                  service.current = Text("Current " + (num++).toString());
                  await displayManager.transferDataToPresentation("NEW_SCREEN");
                }),
                Button("NameByDisplayId", () async {
                  final value = await displayManager
                      .getNameByDisplayId(displays[1]?.displayId ?? -1);
                  print(value);
                }),
                Button("NameByIndex", () async {
                  final value = await displayManager.getNameByIndex(1);
                  print(value);
                }),
                Button("TransferData", () async {
                  final value = await displayManager
                      .transferDataToPresentation("test transfer data");
                  print(value);
                })
              ],
            ),
          ],
        ),
      ),
    );
  }
}

/// UI of Presentation display
class SecondaryScreen extends StatefulWidget {
  @override
  _SecondaryScreenState createState() => _SecondaryScreenState();
}

class _SecondaryScreenState extends State<SecondaryScreen> {
  Widget _current;
  @override
  Widget build(BuildContext context) {
    PresenterService service = Provider.of(context, listen: false);
    return SecondaryDisplay(
      callback: (message) {
        switch (message) {
          case "NEW_SCREEN":
            setState(() {
              _current = service.current;
            });
            break;
          default:
        }
      },
      child: _current ??
          Container(
              color: Theme.of(context).scaffoldBackgroundColor,
              child: Center(
                child: Text("Loading .."),
              )),
    );
  }
}
@VNAPNIC
Copy link
Owner

VNAPNIC commented Jul 14, 2021

when u use the SecondaryDisplay then the class SecondaryScreen is a new application, so Provider not working,
If you want to receive data at the SecondaryScreen then you must use

displayManager.transferDataToPresentation("value");

"value" -> can u change value to json.

corresponding to data transfer

and

 SecondaryDisplay(
      callback: (message) {
      //TODO something
});

to listening for data

@shakirpa
Copy link

shakirpa commented Oct 4, 2021

@VNAPNIC @EbramTawfik @harrynguyen-vmo

I tried Three Packages presentation_displays: ^0.1.9, presentation_displays: ^0.2.0 and presentation_displays: ^0.2.1 in rk3288 POS

with these two packages I have same two issues :

  1. Once I press Show Presentation, in secondary screen I get "Init" message **but after that my whole screen FREEZES **, means to say none of the other buttons including the screen buttons are not able to Press (FROZEN)
  2. TransferData is not working for me. Nothing happens once i press the button. my both screens looks same.

I am getting these print messages, but nothing in the secondary screen

image

Get Displays works well

image

NameByDisplayId works well
image

NameByIndex works well
image

TransferData NOT WORKING
Show Presentation, as i described above It FREEZES the screen
image

Please see the code below

import 'package:flutter/material.dart';
import 'package:presentation_displays/displays_manager.dart';
import 'package:presentation_displays/display.dart';
import 'package:presentation_displays/secondary_display.dart';
import 'package:provider/provider.dart';
import 'package:provider/single_child_widget.dart';

class PresenterService {
late Widget current;
}

Route generateRoute(RouteSettings settings) {
switch (settings.name) {
case '/':
return MaterialPageRoute(builder: () => DisplayManagerScreen());
case 'presentation':
return MaterialPageRoute(builder: (
) => SecondaryScreen());
default:
return MaterialPageRoute(
builder: (_) => Scaffold(
body: Center(
child: Text('No route defined for ${settings.name}')),
));
}
}

class ProviderInjector {
static List providers = [
Provider.value(value: PresenterService()),
];
}

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@OverRide
Widget build(BuildContext context) {
return MultiProvider(
providers: ProviderInjector.providers,
child: MaterialApp(
onGenerateRoute: generateRoute,
initialRoute: '/',
),
);
}
}

class Button extends StatelessWidget {
Button(this.title, this.function);

final String title;
final VoidCallback function;

@OverRide
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.all(4.0),
child: ElevatedButton(
onPressed: function,
child: Text(
title,
style: TextStyle(fontSize: 40),
),
),
);
}
}

/// Main Screen
class DisplayManagerScreen extends StatefulWidget {
@OverRide
_DisplayManagerScreenState createState() => _DisplayManagerScreenState();
}

class _DisplayManagerScreenState extends State {
DisplayManager displayManager = DisplayManager();
List displays = [];
int num = 0;
@OverRide
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Wrap(
alignment: WrapAlignment.center,
children: [
Button("Get Displays", () async {
final values = await displayManager.getDisplays();
print(values);
displays.clear();
displays.addAll(values!);
print(
'Display Id0: ${displays[0].displayId} : ${displays[0].name}');
print(
'Display Id1: ${displays[1].displayId} : ${displays[1].name}');
}),
Button("Show presentation", () async {
print(
'Show presentation Display Id1: ${displays[1].displayId} : ${displays[1].name}');
displayManager.showSecondaryDisplay(
displayId: displays[1].displayId,
routerName: "presentation");
}),

            Button("NameByDisplayId", () async {
              final value = await displayManager
                  .getNameByDisplayId(displays[1].displayId);
              print(value);
            }),
            Button("NameByIndex", () async {
              final value = await displayManager.getNameByIndex(1);
              print("NameByIndex alue : $value");
            }),
            Button("TransferData", () async {
              final value = await displayManager
                  .transferDataToPresentation("test transfer data");
              print("TransferData Value : $value");
            })
          ],
        ),
      ],
    ),
  ),
);

}
}

/// UI of Presentation display
class SecondaryScreen extends StatefulWidget {
@OverRide
_SecondaryScreenState createState() => _SecondaryScreenState();
}

class _SecondaryScreenState extends State {
String value = "init";
@OverRide
Widget build(BuildContext context) {
print('message $value');
return SecondaryDisplay(
callback: (message) {
setState(() {
value = message;
print("message : $message");
});
},
child: Container(
color: Theme.of(context).scaffoldBackgroundColor,
child: Center(
child: Text(value),
)),
);
}
}

@shakirpa
Copy link

The issue was with Android image, I had to flush the android image and re flash with other build. Now its working

@shakirpa
Copy link

when u use the SecondaryDisplay then the class SecondaryScreen is a new application, so Provider not working, If you want to receive data at the SecondaryScreen then you must use

displayManager.transferDataToPresentation("value");

"value" -> can u change value to json.

corresponding to data transfer

and

 SecondaryDisplay(
      callback: (message) {
      //TODO something
});

to listening for data

if some one have issue to get it work, please use this way displayManager.transferDataToPresentation(json.encode(json));

@jinqugan
Copy link

jinqugan commented Jan 5, 2023

The issue was with Android image, I had to flush the android image and re flash with other build. Now its working

hello , i am new to this, can help provide more information about this ? how can i solve this issue, i cant get it what its mean by flush the android image? anyone can help on this?

@shakirpa
Copy link

shakirpa commented Jan 5, 2023

The issue was with Android image, I had to flush the android image and re flash with other build. Now its working

hello , i am new to this, can help provide more information about this ? how can i solve this issue, i cant get it what its mean by flush the android image? anyone can help on this?

Its better if you contact your device vendor, they will do it for you.

@jinqugan
Copy link

jinqugan commented Jan 6, 2023

The issue was with Android image, I had to flush the android image and re flash with other build. Now its working

hello , i am new to this, can help provide more information about this ? how can i solve this issue, i cant get it what its mean by flush the android image? anyone can help on this?

Its better if you contact your device vendor, they will do it for you.

this is related to hardware device? i will contact my device vendor, thanks

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

4 participants