我刚刚将 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(),
);
}
}
Run Code Online (Sandbox Code Playgroud)
只需将此添加到您的 AppBar/SliverAppBar
leading: (ModalRoute.of(context)?.canPop ?? false) ? BackButton() : null,
Run Code Online (Sandbox Code Playgroud)
这是 2.0 版的当前行为,if条件还检查!hasEndDrawer
1.17 版
if (canPop)
leading = useCloseButton ? const CloseButton() : const BackButton();
Run Code Online (Sandbox Code Playgroud)
2.0版
if (!hasEndDrawer && canPop)
leading = useCloseButton ? const CloseButton() : const BackButton();
Run Code Online (Sandbox Code Playgroud)
您可以在其中添加自己的逻辑 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'),
),
Run Code Online (Sandbox Code Playgroud)
工作演示
完整代码
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(),
);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
536 次 |
| 最近记录: |