flutterif-statementdartflutter-drawer

Flutter: How to write if-else statement to call drawer?


I am trying to make a drawer in the class DetailedPage only when the previous class was CameraPage. To achieve this, I create a variable String previousScreen = 'CAMERA' and passed the variable to the class DetailedPage. However, I am having difficulty to write an if-else statement to make the drawer only when String previousScreen is equal to 'CAMERA'.

I followed the advice on this page, but it did not work because I want to use drawer: MyDrawer(). (drawer: seems that it has a designated place, such as in bewteen Scaffold and CustomScrollView, to work). Is there any way to use if-else statement for drawer: MyDrawer()?

My entire DetailedPage is here: (I tried to cut some unnecessary codes, so the number of parenthesis may not be right, but it works except for the if-else statement in the very end.)

class DetailScreen extends StatelessWidget {
  final Recyclable recyclable;
  final String previousScreen;
  DetailScreen(
      {Key key, @required this.recyclable, @required this.previousScreen})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    //set variables
    String imagename = recyclable.title;
    String recycle = recyclable.recycle;
    String instruction = recyclable.instruction;
    String why = recyclable.why;
    String donate = recyclable.donate;

    return Scaffold(
      body: CustomScrollView(
        physics: const BouncingScrollPhysics(),
        slivers: <Widget>[
          SliverAppBar(
            expandedHeight: 200.0,
            pinned: false,
            floating: false,
            backgroundColor: Colors.transparent,
            onStretchTrigger: () {
              // Function callback for stretch
              return;
            },
            flexibleSpace: FlexibleSpaceBar(
              centerTitle: true,
              title: Text('$imagename',
                  style: TextStyle(
                      fontSize: 55.0,
                      fontFamily: 'Rajdhani',
                      fontWeight: FontWeight.w700,
                      color: Colors.white)),
              background: Container(
                decoration: MyBoxDecoration(imagename),
              ),
            ),
            actions: <Widget>[
              IconButton(
                icon: Icon(
                  Icons.widgets,
                  color: Colors.white,
                ),
                tooltip: 'Home Page',
                //show side pages
                onPressed: () => Navigator.of(context).pushNamed('/HOME'),
              ),
            ],
          ),
          SliverPadding(
            padding: EdgeInsets.all(20.0),
            sliver: SliverList(
              delegate: SliverChildListDelegate([
                Row(
                  children: [
                    Text('This item is ',
                        style: LightThemeGrey().textTheme.bodyText1),
                    Text('$recycle',
                        style: LightThemeGrey().textTheme.subtitle2),
                    Text('.', style: LightThemeGrey().textTheme.bodyText1),
                  ],
                ),            
              ]),
            ),
          ),
        ],
      );
      //TODO: If previous Screen == CAMERA, make MyDrawer()
      previousScreen == 'CAMERA'? drawer: MyDrawer() : null,
    );
  }
}

Solution

  • You should use the following:

    drawer:  previousScreen == 'CAMERA'? MyDrawer() : null,
    

    drawer takes a widget, therefore you can use the ternary operator to written assign a widget to the property drawer according to the condition.