如何解决Flutter:如何编写if-else语句来调用抽屉?
仅在上一类为drawer
时,我才尝试在DetailedPage
类中创建CameraPage
。为此,我创建了一个变量String previousScreen = 'CAMERA'
,并将该变量传递给类DetailedPage
。但是,仅在字符串drawer
等于previousScreen
时,我很难编写if-else语句来使'CAMERA'
。
我遵循了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('.',]),);
//TODO: If previous Screen == CAMERA,make MyDrawer()
previousScreen == 'CAMERA'? drawer: MyDrawer() : null,);
}
}
解决方法
我认为这可行:
drawer: (previousScreen == 'CAMERA')? MyDrawer() : null,
让我知道我是否回答了您的问题。
,您应该使用以下内容:
drawer: previousScreen == 'CAMERA'? MyDrawer() : null,
drawer
带有一个窗口小部件,因此您可以使用三元运算符根据条件将一个窗口小部件写入属性drawer
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。