【发布时间】:2020-12-05 07:05:10
【问题描述】:
我试图在DetailedPage 类中创建一个drawer,仅当上一课是CameraPage 时。为此,我创建了一个变量 String previousScreen = 'CAMERA' 并将该变量传递给类 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,
);
}
}
【问题讨论】:
标签: flutter if-statement dart flutter-drawer