0

我只在上一堂课时才尝试drawer在课堂上做一个。为此,我创建了一个变量 String并将该变量传递给 class 。但是,我很难编写 if-else 语句来仅当 String等于.DetailedPageCameraPagepreviousScreen = 'CAMERA'DetailedPagedrawerpreviousScreen'CAMERA'

我遵循了此页面上的建议,但它没有工作,因为我想使用drawer: MyDrawer()。(drawer:似乎它有一个指定的地方,例如在bewteen Scaffoldand CustomScrollView, to work)。有什么方法可以使用 if-else 语句drawer: MyDrawer()吗?

我的全部DetailedPage在这里:(我试图削减一些不必要的代码,所以括号的数量可能不对,但除了最后的 if-else 语句之外,它都有效。)

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,
    );
  }
}
4

2 回答 2

2

您应该使用以下内容:

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

drawerdrawer需要一个小部件,因此您可以使用三元运算符编写根据条件将小部件分配给属性。

于 2020-08-15T17:30:37.270 回答
1

我认为这有效:

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

如果我回答了你的问题,请告诉我。

于 2020-08-15T17:29:41.253 回答