[英]Flutter 2.0 appbar back button disappeared if contains endDrawer
我剛剛將 flutter 更新到 2.0,我意識到如果 appbar 還包含 endDrawer,所有后退按鈕都會消失
我試圖擺脫endDrawer,返回按鈕出現,只是沒有與endDrawer一起,更新前不是這樣,有人知道如何解決這個問題嗎?
我的代碼:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Page1(),
);
}
}
class Page1 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: Center(
child: TextButton(
child: Text(
'Page 1',
style: TextStyle(fontSize: 30.0, fontWeight: FontWeight.bold),
),
onPressed: () {
Navigator.push(
context, MaterialPageRoute(builder: (context) => Page2()));
},
)),
),
);
}
}
class Page2 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Demo'),
),
body: Container(
child: Center(
child: TextButton(
child: Text(
'Page 2',
style: TextStyle(fontSize: 30.0, fontWeight: FontWeight.bold),
),
onPressed: () {
Navigator.pop(context);
},
),
),
),
endDrawer: Drawer(),
);
}
}
只需將此添加到您的 AppBar/SliverAppBar
leading: (ModalRoute.of(context)?.canPop ?? false) ? BackButton() : null,
這是 2.0 版中的當前行為, if
條件還檢查!hasEndDrawer
1.17 版
if (canPop)
leading = useCloseButton ? const CloseButton() : const BackButton();
2.0版
if (!hasEndDrawer && canPop)
leading = useCloseButton ? const CloseButton() : const BackButton();
您可以在leading
中添加自己的邏輯
代碼片段
appBar: AppBar(
leading: Builder(
builder: (BuildContext context) {
final ScaffoldState scaffold = Scaffold.maybeOf(context);
final ModalRoute<dynamic> parentRoute = ModalRoute.of(context);
final bool hasEndDrawer = scaffold?.hasEndDrawer ?? false;
final bool canPop = parentRoute?.canPop ?? false;
if (hasEndDrawer && canPop) {
return BackButton();
} else {
return SizedBox.shrink();
}
},
),
title: Text('Page 2'),
),
工作演示
完整代碼
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Page1(),
);
}
}
class Page1 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: Center(
child: TextButton(
child: Text(
'Page 1',
style: TextStyle(fontSize: 30.0, fontWeight: FontWeight.bold),
),
onPressed: () {
Navigator.push(
context, MaterialPageRoute(builder: (context) => Page2()));
},
)),
),
);
}
}
class Page2 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: Builder(
builder: (BuildContext context) {
final ScaffoldState scaffold = Scaffold.maybeOf(context);
final ModalRoute<dynamic> parentRoute = ModalRoute.of(context);
final bool hasEndDrawer = scaffold?.hasEndDrawer ?? false;
final bool canPop = parentRoute?.canPop ?? false;
if (hasEndDrawer && canPop) {
return BackButton();
} else {
return SizedBox.shrink();
}
},
),
title: Text('Page 2'),
),
body: Container(
child: Center(
child: TextButton(
child: Text(
'Page 2',
style: TextStyle(fontSize: 30.0, fontWeight: FontWeight.bold),
),
onPressed: () {
Navigator.pop(context);
},
),
),
),
endDrawer: Drawer(),
);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.