Flutter: 如何编写if-else语句以调用抽屉?

12 浏览
0 Comments

Flutter: 如何编写if-else语句以调用抽屉?

我正在尝试仅在前一个类为CameraPage时,在类DetailedPage中创建一个drawer。为了实现这一点,我创建了一个变量StringpreviousScreen =\'CAMERA\',并将变量传递给类DetailedPage。然而,我在编写if-else语句时遇到了困难,只有当StringpreviousScreen等于\'CAMERA\'时才能制作drawer

我按照此页面的建议去做,但它没有起作用,因为我想使用drawer:MyDrawer()。(drawer:似乎有一个指定的位置,如在ScaffoldCustomScrollView之间,才能起作用)。有没有办法为drawer:MyDrawer()编写if-else语句?

我整个的DetailedPage在这里:

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

admin 更改状态以发布 2023年5月21日
0
0 Comments

我认为这样可以:

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

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

0
0 Comments

您应该使用以下内容:

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

drawer 接受一个小部件,因此您可以使用三目运算符,根据条件将小部件写入属性 drawer

0