Flutter:如何编写 if-else 语句来调用抽屉?
Flutter: How to write if-else statement to call drawer?
我试图在 class DetailedPage
中创建一个 drawer
,仅当前一个 class 是 CameraPage
时。为此,我创建了一个变量 String previousScreen = 'CAMERA'
并将该变量传递给 class DetailedPage
。但是,仅当字符串 previousScreen
等于 'CAMERA'
.
时,我很难编写一个 if-else 语句来生成 drawer
我听从了 this page 上的建议,但它没有用,因为我想使用 drawer: MyDrawer()
。 (drawer:
好像有指定的地方,比如在Scaffold
和CustomScrollView
之间上班)。 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,
);
}
}
我认为这可行:
drawer: (previousScreen == 'CAMERA')? MyDrawer() : null,
如果我回答了你的问题请告诉我。
您应该使用以下内容:
drawer: previousScreen == 'CAMERA'? MyDrawer() : null,
drawer
取一个widget,所以可以用三元运算符,根据条件写给属性drawer
分配一个widget。
我试图在 class DetailedPage
中创建一个 drawer
,仅当前一个 class 是 CameraPage
时。为此,我创建了一个变量 String previousScreen = 'CAMERA'
并将该变量传递给 class DetailedPage
。但是,仅当字符串 previousScreen
等于 'CAMERA'
.
drawer
我听从了 this page 上的建议,但它没有用,因为我想使用 drawer: MyDrawer()
。 (drawer:
好像有指定的地方,比如在Scaffold
和CustomScrollView
之间上班)。 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,
);
}
}
我认为这可行:
drawer: (previousScreen == 'CAMERA')? MyDrawer() : null,
如果我回答了你的问题请告诉我。
您应该使用以下内容:
drawer: previousScreen == 'CAMERA'? MyDrawer() : null,
drawer
取一个widget,所以可以用三元运算符,根据条件写给属性drawer
分配一个widget。