我有一个flutter应用程序,其中包含一个导航抽屉菜单。当您单击抽屉时,可以导航到其他页面。但是,当您导航到其他页面时,工具栏与包含导航抽屉的上一页相同。我有一个主页,其中包含工具栏上的菜单。但是,我只想要主页上的菜单。如果我转到另一个页面,我希望菜单消失。
这是我的代码
import 'package:flutter/material.dart';
import 'package:finsec/widget/drawer_widget.dart';
import 'package:finsec/widget/inherited_month_year.dart';
import 'package:finsec/screens/income/second_fragment.dart';
import 'package:finsec/screens/home/dashboards.dart';
import 'package:finsec/screens/transaction/transaction_list.dart';
import 'package:finsec/utils/colors.dart';
import 'package:finsec/utils/strings.dart';
import 'package:month_picker_dialog/month_picker_dialog.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:finsec/utils/localizations.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:finsec/model/month_year.dart';
class HomeScreen extends StatefulWidget {
final String title = home;
final Widget child;
HomeScreen({Key key, this.child}) : super(key: key);
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
DateTime selectedDate, initialDate, prevSelectedDate;
String monthName1;
int monthNumber, year;
MonthYear monthYear;
getDrawerItemWidget(int pos, String title) {
switch (pos) {
case 0:
return new Dashboards ();
case 1:
return new SecondFragment();
case 2:
return new TransactionList(
outstanding: totalOutstanding,
received_or_paid:
totalReceived, transactionType: title,);
case 3:
return new TransactionList(
outstanding: totalOutstanding,
received_or_paid: totalPaid,
transactionType: title,);
default:
return new Text("Error");
}
}
String titleAppBar = home;
int tabIndex = 0;
double elevation = 0;
@override
void initState() {
setState(() {
titleAppBar = widget.title;
});
super.initState();
initialDate = DateTime.now();
selectedDate = initialDate;
}
void getMonthYear() {
if(selectedDate != null) {
monthName1 = monthNamesAbbrv[selectedDate.month];
monthNumber = selectedDate.month;
year = selectedDate.year;
prevSelectedDate = selectedDate;
}
else {
monthName1 = monthNamesAbbrv[prevSelectedDate.month];
monthNumber = prevSelectedDate.month;
year = prevSelectedDate.year;
}
monthYear = new MonthYear(monthNumber: monthNumber, year: year) ;
}
Widget homeScreen(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(titleAppBar),
elevation: elevation,
backgroundColor: colorPrimary,
actions: <Widget>[
Builder(
builder: (context) => FlatButton.icon(
icon: Icon(Icons.calendar_today, color: white,), //`Icon` to display
label: Text(monthName1 + ' $year' , style: TextStyle(color: white, fontSize: 18)), //`Text` to display
onPressed: () {
// DateTime date = new DateTime.now();
// showMonthPicker(context: context, initialDate: date);
showMonthPicker(
context: context,
firstDate: DateTime( DateTime.now().year - 1 , 5),
lastDate: DateTime( DateTime.now().year + 1, 9 ),
initialDate: selectedDate ?? initialDate
)
.then((date) => setState(() {
selectedDate = date ;//!= null ? date : prevSelectedDate;
}));
},
),
)
],
),
drawer: DrawerWidget((title, index) {
setState(() {
titleAppBar = title;
tabIndex = index;
});
}, tabIndex),
body: getDrawerItemWidget(tabIndex, titleAppBar),
);
}
@override
Widget build(BuildContext context) {
getMonthYear();
return
new MaterialApp(
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: [
Locale('en'),
Locale('zh'),
Locale('fr'),
Locale('es'),
Locale('de'),
Locale('ru'),
Locale('ja'),
Locale('ar'),
Locale('fa'),
],
title: widget.title,
home: homeScreen(context)
);
}
}
如果您在上面查看我的代码并检查homeScreen功能,您会注意到我在actions: <Widget>[
命令下有一个菜单小部件。
这是出现在工具栏上的菜单。当我使用导航菜单移至另一页时,将显示相同的菜单。我想在转到另一页时删除菜单。有没有办法告诉Flutter仅在首页上显示菜单?也许if操作中的if语句:homeScreen函数的一部分。
2019年10月菜单也会出现在收入预算屏幕的工具栏上。我不要那里的菜单。仅在首页屏幕上
答案 0 :(得分:0)
如果tabIndex不为0,则可以有条件地告诉构建器销毁日期,如果为0,则可以再次创建日期
Builder(
builder: (context) => tabIndex==0 ? //here we check
FlatButton.icon(
icon: Icon(Icons.calendar_today, color: white,), //`Icon` to display
label: Text(monthName1 + ' $year' , style: TextStyle(color: white, fontSize: 18)), //`Text` to display
onPressed: () {
// DateTime date = new DateTime.now();
// showMonthPicker(context: context, initialDate: date);
showMonthPicker(
context: context,
firstDate: DateTime( DateTime.now().year - 1 , 5),
lastDate: DateTime( DateTime.now().year + 1, 9 ),
initialDate: selectedDate ?? initialDate
)
.then((date) => setState(() {
selectedDate = date ;//!= null ? date : prevSelectedDate;
}));
},
):SizedBox() //if it's not 0 we return an empty widget
)