0

I am working on a app which has multiple screens. Camera image capturing screen is never directly called by main screen instead of sub screens. Though My camera picker code works perfectly as an individual flutter project when I put it in main file and execute. But when I do the same by using a stateful widget class and calling it via Navigator push, It just doesn't respond. The camera opening part.

Code for the screen that invokes camera screen

import 'package:carryon/luggageupdatepage.dart';
import 'package:carryon/scannerscreen.dart';
import 'package:flutter/material.dart';
import 'allvalues.dart';
import 'login_screen.dart';
import 'welcome_screen.dart';
import 'auth.dart';
class DetailsPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Detailspage();
  }
}


class Detailspage extends StatefulWidget {
  @override
  _DetailspageState createState() => _DetailspageState();
}

class _DetailspageState extends State<Detailspage>
    with TickerProviderStateMixin{
  final AuthServices _auth=AuthServices();
  void _showDialog() {
    // flutter defined function
    showDialog(
      context: context,
      builder: (BuildContext context) {
        // return object of type Dialog
        return AlertDialog(
          title: Text("Invalid PNR",
            style: TextStyle(fontSize: 14),),
          content: Text(
            "Please neter a valid PNR number.",
            style: TextStyle(fontSize: 12,),),
          actions: <Widget>[
            // usually buttons at the bottom of the dialog
            new FlatButton(
              child: new Text("Close"),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
          ],
        );
      },
    );
  }
  AnimationController controller;
  Animation<double> animation;
  @override
  initState() {
    super.initState();
    controller = AnimationController(
        duration: const Duration(seconds: 3), vsync: this);
    animation = CurvedAnimation(parent: controller, curve: Curves.easeIn);
    controller.forward();
  }
  @override
  Widget build(BuildContext context) {
    return FadeTransition(
      opacity: animation,
      child: Scaffold(
        resizeToAvoidBottomInset: false,
        backgroundColor: Background_Color,
        appBar: AppBar(
          actions: <Widget>[
            FlatButton.icon(onPressed: ()async{
              await _auth.signOut();
              Navigator.push(context,
                MaterialPageRoute(
                    builder: (context)=>CarryOn()),);
            },
                icon: Icon(Icons.perm_identity), label: Text('Logout'))
          ],
          title: Text(
            'Flight CheckIn',
            style: LoginScreenTextStyle.copyWith(fontSize: 20),
          ),

        ),
        body: Column(
          children: <Widget>[
            SizedBox(height: 30,),
            Container(
              width: double.infinity,
              color: Colors.green,
              child: Text(
                'Your Flight ${userdetails.FlightName} from ${userdetails.Departure_Destination} to ${userdetails.Arrival_Destination} on ${userdetails.Departure_Date} is on time. Kindly report one hour prior to boarding time.',
                style: TextStyle(
                  fontSize: 15,
                  color: Colors.black,
                ),
              ),
            ),
            SizedBox(height: 100,),
            Row(
              children: <Widget>[
                SizedBox(width: 60,),
                Image.asset('assets/images/flighticon.png',height: 100,width: 100,),
              SizedBox(width:20),
                Container(
                  height: 100,
                  child: Column(
                    children: <Widget>[
                      Container(
                        height: 20,
                        color: Colors.white,
                        child: Text(
                          '${userdetails.Departure_Destination}',
                          style: TextStyle(color: Colors.black),
                        ),
                      ),
                      SizedBox(height: 20,),
                      Container(
                        height: 20,
                        color: Background_Color,
                        child: Text(
                          '${userdetails.Departure_Time}hrs-${userdetails.Arrival_Time}hrs',
                          style: TextStyle(color: Colors.grey),
                        ),
                      ),
                      SizedBox(height: 20,),
                      Container(
                        height: 20,
                        color: Colors.white,
                        child: Text(
                          '${userdetails.Arrival_Destination}',
                          style: TextStyle(color: Colors.black),
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
            SizedBox(height: 80,),
            Container(
              height: 50,width:250,
              child: DecoratedBox(
                decoration:ShapeDecoration(shape:roundedrectangle, color:Button_Color),
                child: OutlineButton(
                  color:Button_Color,
                  borderSide: BorderSide(color:Colors.white,),
                  shape: roundedrectangle,

                  child: Center(child: Text('My Luggage Status')),
                  onPressed: ()
                  {
                    Navigator.push(context,
                      MaterialPageRoute(
                          builder: (context)=>LuggageUpdates()),);

                  },
                ),

              ),
            ),
            SizedBox(height: 30,),
            Container(
              height: 50,width:250,
              child: DecoratedBox(
                decoration:ShapeDecoration(shape:roundedrectangle, color:Button_Color),
                child: OutlineButton(
                  color:Button_Color,
                  borderSide: BorderSide(color:Colors.white,),
                  shape: roundedrectangle,

                  child: Center(child: Text('Repeat CheckIn')),
                  onPressed: ()
                  {
                    Navigator.push(context,
                      MaterialPageRoute(
                          builder: (context)=>MyApp()),);

                  },
                ),

              ),
            ),

          ],
        ),
      ),
    );
  }
}

code I used to execute for the camera screen:

import 'package:flutter/material.dart';
import 'dart:io';
import 'package:image_picker/image_picker.dart';


class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  File _image;
  Future getImage(bool isCamera)
  async{
    File image;
    if(isCamera)
    {
      image=await ImagePicker.pickImage(source: ImageSource.camera);
      print('camera chale to sahi');

    }
    else
      image=await ImagePicker.pickImage(source: ImageSource.gallery);
    setState(() {
      _image=image;
    });
  }
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Image Pick Demo',),
        ),
        body: Column(
          children: <Widget>[
            IconButton(
              icon: Icon(Icons.insert_drive_file),
              onPressed:(){
                getImage(false);
              },
            ),
            SizedBox(height: 10,),
            IconButton(
              icon: Icon(Icons.camera_alt),
              onPressed: (){getImage(true);},
            ),
            _image==null?Container():Image.file(_image,height: 300,width: 300,),
          ],
        ),
      ),
    );
  }
}

The exact same code works when run directly by main.dart, so there's some blunder I am doing in invoking the camera_screen.dart file.

James Z
  • 12,209
  • 10
  • 24
  • 44
Shivam Sahil
  • 4,055
  • 3
  • 31
  • 62
  • what does your console log say? x( – OMi Shah Nov 14 '19 at 17:11
  • 1
    In a normal flutter project your code just works fine when run on real device. Are you trying it in `flutter-web` by any chance..? I also see the comment `'camera chale to sahi'` :-).. My stateful widget is composed of a **Scaffold-->body: Container, floatingActionButton: FloatingActionButton** where when the fab is pressed a navigation happens as yours `Navigator.push(context, MaterialPageRoute(builder: (context) => MyApp()));`. – Abhilash Chandran Nov 16 '19 at 20:14
  • Hi Omi, this is the error I am getting. – Shivam Sahil Nov 18 '19 at 02:24
  • `W/Binder (21887): Caught a RuntimeException from the binder stub implementation. W/Binder (21887): java.lang.RuntimeException: Methods marked with @UiThread must be executed on the main thread. Current thread: Binder:21887_3 ` – Shivam Sahil Nov 18 '19 at 02:26
  • Hi Abhilash, yes I am using flutter-web. Is there another method to access the camera in that case? And Yeah, I have been making so many of the modifications, but camera is still not working so, I was just debugging at which stage it stops working by adding that comment. – Shivam Sahil Nov 18 '19 at 02:27

0 Answers0