我读了很多关于此的主题,但没有任何效果,而且与我的背景确实相同。
我正在尝试在原生Android项目中添加抖动视图,我有一个原生Activity会启动抖动片段。该颤动片段包含2个视图(firstScreen和secondScreen)。按下按钮,FirstScreen重定向到SecondScreen,按下按钮,secondScreen弹出回到第一屏幕。到现在为止一切正常。
但是,当我进入第二个屏幕时,我想回到firstScreen上的设备后退按钮上,但是颤动的Fragment关闭了,我回到了本机Activity。
当我单独运行flutter模块时,后退过程看起来很好,因为它可以正常工作。
以下是一些屏幕截图:
这是一些代码:
在本地MainActivity中
override fun startFlutterModule() {
val newFlutterFragment: FlutterFragment = FlutterFragment.createDefault()
supportFragmentManager.beginTransaction()
.replace(
fragmentContainerId,
newFlutterFragment,
TAG_FLUTTER_FRAGMENT
)
.addToBackStack(TAG_FLUTTER_FRAGMENT)
.commit()
}
基础Flutter应用
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
routes: {
'/first': (context) => FirstScreen(),
'/second': (context) => SecondScreen(),
},
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: FirstScreen(),
);
}
}
第一个颤动屏幕
class FirstScreen extends StatefulWidget {
@override
_FirstScreenState createState() => _FirstScreenState();
}
class _FirstScreenState extends State<FirstScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
"First Screen",
style: TextStyle(
fontSize: 30.0,
fontWeight: FontWeight.w600,
),
),
Padding(
padding: EdgeInsets.all(10.0),
child: RaisedButton(
padding: EdgeInsets.symmetric(
horizontal: 20.0,
vertical: 10.0,
),
onPressed: (){
Navigator.pushNamed(context, '/second');
},
child: Text(
"Screen 2",
style: TextStyle(
color: Colors.white,
fontSize: 20.0,
),
),
),
)
],
),
),
);
}
}
第二个颤振屏幕
class SecondScreen extends StatefulWidget {
@override
_SecondScreenState createState() => _SecondScreenState();
}
class _SecondScreenState extends State<SecondScreen> {
@override
Widget build(BuildContext context) {
return new WillPopScope(
onWillPop: () async {
Navigator.pop(context);
return Future.value(false);
},
child: Scaffold(
backgroundColor: Colors.white,
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
"Second Screen",
style: TextStyle(
fontSize: 30.0,
fontWeight: FontWeight.w600,
),
),
Padding(
padding: EdgeInsets.all(10.0),
child: RaisedButton(
padding: EdgeInsets.symmetric(
horizontal: 20.0,
vertical: 10.0,
),
onPressed: () {
Navigator.pop(context);
},
child: Text(
"Screen 1",
style: TextStyle(
color: Colors.white,
fontSize: 20.0,
),
),
),
)
],
),
),
)
);
}
}