【问题标题】:Flutter: How to write if-else statement to call drawer?Flutter:如何编写 if-else 语句来调用抽屉?
【发布时间】:2020-12-05 07:05:10
【问题描述】:

我试图在DetailedPage 类中创建一个drawer,仅当上一课是CameraPage 时。为此,我创建了一个变量 String previousScreen = 'CAMERA' 并将该变量传递给类 DetailedPage。但是,仅当字符串 previousScreen 等于 'CAMERA' 时,我很难编写 if-else 语句来生成 drawer

我按照this page 上的建议进行操作,但它不起作用,因为我想使用drawer: MyDrawer()。 (drawer:好像有一个指定的地方,比如在ScaffoldCustomScrollView之间工作)。 drawer: MyDrawer() 有没有办法使用 if-else 语句?

我的整个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,
    );
  }
}

【问题讨论】:

    标签: flutter if-statement dart flutter-drawer


    【解决方案1】:

    我认为这可行:

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

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

    【讨论】:

      【解决方案2】:

      您应该使用以下内容:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-30
        • 1970-01-01
        • 1970-01-01
        • 2023-03-24
        • 1970-01-01
        • 1970-01-01
        • 2021-12-02
        • 1970-01-01
        相关资源
        最近更新 更多