我想避免在有人按下android后退按钮时起作用。我的目标是只有屏幕顶部的“后退”按钮可以工作。
我尝试用WillPopScope修复它,但是该按钮仍在工作...
我希望当我按下android的后退按钮时,什么都不会发生。有人可以帮助我解决这个问题吗?
import 'dart:async';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:carousel_slider/carousel_slider.dart';
import 'package:rating_bar/rating_bar.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:cruke_app/ui/map.dart';
import 'package:flutter/services.dart' show rootBundle;
class ChosenOption extends StatefulWidget {
final String option;
final DocumentSnapshot document;
ChosenOption({Key key, @required this.document, this.option}) : super(key: key);
@override
_ChosenOptionState createState() => _ChosenOptionState();
}
class _ChosenOptionState extends State<ChosenOption> {
Completer<GoogleMapController> _controller = Completer();
GoogleMapController mapController;
String _mapStyle;
static const LatLng _center = const LatLng(45.521563, -122.677433);
@override
void initState() {
super.initState();
rootBundle.loadString('assets/map_style.txt').then((string) {
_mapStyle = string;
});
}
void _onMapCreated(GoogleMapController controller) {
mapController = controller;
mapController.setMapStyle(_mapStyle);
_controller.complete(controller);
}
Future<bool> _willPopCallback() async{
return false;
}
@override
Widget build(BuildContext context) {
double _rating = 0;
return WillPopScope(
onWillPop: _willPopCallback,
child: Scaffold(
appBar: AppBar(leading: Icon(Icons.arrow_back),),
body: SingleChildScrollView(
child: Column(
children: <Widget>[
Stack(
children: <Widget>[
CarouselSlider(
height: 200.0,
items: [1,2,3,4,5].map((i) {
return Builder(
builder: (BuildContext context) {
return Container(
width: MediaQuery.of(context).size.width,
margin: EdgeInsets.symmetric(horizontal: 1.0),
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(widget.document["image"]),
fit: BoxFit.cover,
),
),
child: Text('$i', style: TextStyle(fontSize: 16.0),)
);
},
);
}).toList(),
),
IconButton(
icon: Icon(Icons.arrow_back, color: Colors.red, size: 35,),
onPressed: (){
Navigator.pop(context, false);
},
),
],
),
Container(
margin: EdgeInsets.only(top: 20.0, left: 13.0),
child: Row(
children: <Widget>[
Container(
width: MediaQuery.of(context).size.width * 0.8,
child: Text(widget.document["title"],
style: TextStyle(color: Colors.black,
fontSize: 20.0, fontWeight: FontWeight.bold),),
),
Container(
width: MediaQuery.of(context).size.width * 0.15,
child: RawMaterialButton(
onPressed: () {},
child: Icon(
Icons.favorite,
color: Colors.white,
size: 20.0,
),
shape: CircleBorder(),
elevation: 2.0,
fillColor: Colors.red,
padding: EdgeInsets.all(8.0),
),
),
],
),
),
Row(
children: <Widget>[
Container(
margin: EdgeInsets.only(left: 13.0),
child: Icon(Icons.star, size: 18.0, color: Colors.black),
),
Container(
margin: EdgeInsets.only(left: 5.0),
child: RatingBar(
onRatingChanged: (rating) => setState(() => widget.document["rating"] == rating),
filledIcon: Icons.star,
emptyIcon: Icons.star_border,
halfFilledIcon: Icons.star_half,
isHalfAllowed: true,
filledColor: Colors.yellow,
emptyColor: Colors.grey,
halfFilledColor: Colors.yellow,
size: 18,
),
),
Container(
margin: EdgeInsets.only(left: 5.0),
child: Text(widget.document["avaliations"].toString()+" | ", style: TextStyle(color: Colors.black, fontSize: 10.0),),
),
Text(widget.document["city"], style: TextStyle(color: Colors.black, fontSize: 10.0),),
],
),
Container(
margin: EdgeInsets.all(13.0),
child: Text(widget.document["text"]),
),
Divider(),
Text("Avaliações", style: TextStyle(fontSize: 20,),),
buildEvaluation(_rating, "images/restaurant.jpg", "Leandro", "Minas Gerais - MG", "6 dias atrás"),
buildEvaluation(_rating, "images/restaurant.jpg", "Leandro", "Minas Gerais - MG", "10 dias atrás"),
buildEvaluation(_rating, "images/restaurant.jpg", "Leandro", "MInas Gerais - MG", "16 dias atrás"),
Stack(
children: <Widget>[
Column(
children: <Widget>[
Container(
margin: EdgeInsets.only(top: 20.0),
child: Text("Avaliar este Restaurante", style: TextStyle(fontSize: 18),),
),
Container(
alignment: Alignment.center,
margin: EdgeInsets.only(top: 10.0),
child: RatingBar(
onRatingChanged: (rating) => setState(() => _rating = rating),
filledIcon: Icons.star,
emptyIcon: Icons.star_border,
halfFilledIcon: Icons.star_half,
isHalfAllowed: true,
filledColor: Colors.yellow,
emptyColor: Colors.black,
halfFilledColor: Colors.yellow,
size: 20,
),
),
Container(
width: 300.0,
margin: EdgeInsets.only(bottom: 50.0),
child: TextField(
decoration: InputDecoration(
suffixIcon: Padding(
padding: EdgeInsetsDirectional.only(end: 12.0),
child: Icon(Icons.send, color: Colors.black),
),
hintText: "Comentar...",
hintStyle: TextStyle(color: Colors.grey, fontSize: 15.0),
fillColor: Colors.white,
),
textAlign: TextAlign.end,
),
),
Container(
margin: EdgeInsets.only(right: 220.0, bottom: 5.0),
child: Text("Local no mapa",
style: TextStyle(fontWeight: FontWeight.bold, color: Colors.black),
),
),
Container(
width: 340.0,
height: 200.0,
margin: EdgeInsets.only(bottom: 10.0),
decoration: BoxDecoration(
color: Colors.white,
boxShadow: <BoxShadow>[
BoxShadow(
color: Colors.grey,
offset: Offset(1.0, 1.0),
blurRadius: 10.0,
),
],
borderRadius: BorderRadius.circular(10.0),
),
child: GoogleMap(
mapType: MapType.normal,
onMapCreated: _onMapCreated,
initialCameraPosition: CameraPosition(
target: _center,
zoom: 18.0,
),
),
),
RaisedButton.icon(
onPressed: (){
Navigator.push(
context,
MaterialPageRoute(builder: (context) => CrukeMap()),
);
},
label: Text(
'TRAÇAR ROTA',
style: TextStyle(fontSize: 13, color: Colors.white, fontWeight: FontWeight.bold),
),
color: Colors.red,
icon: Icon(Icons.directions, color: Colors.white),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(30.0)),
),
Row(
children: <Widget>[
Container(
margin: EdgeInsets.only(left: 20.0, top: 40.0),
child: Icon(Icons.place, color: Colors.red, size: 18,),
),
Container(
margin: EdgeInsets.only(left: 10.0, top: 40.0),
child: Text("R. Sete de Setembro, Porto Seguro - BA, 45810-000", style: TextStyle(fontSize: 12.0),),
),
],
),
Row(
children: <Widget>[
Container(
margin: EdgeInsets.only(left: 20.0, top: 10.0),
child: Icon(Icons.local_phone, color: Colors.red, size: 18,),
),
Container(
margin: EdgeInsets.only(left: 10.0, top: 10.0),
child: Text("(73)99906-3724", style: TextStyle(fontSize: 12.0),),
),
],
),
Row(
children: <Widget>[
Container(
margin: EdgeInsets.only(left: 20.0, top: 10.0, bottom: 50.0),
child: Icon(Icons.timer, color: Colors.red, size: 18,),
),
Container(
margin: EdgeInsets.only(left: 10.0, top: 10.0, bottom: 50.0),
child: Text("Horário de Funcionamento: 12:00 às 20:00", style: TextStyle(fontSize: 12.0),),
),
],
),
],
),
],
),
],
),
),
),
);
}
Widget buildEvaluation(double _rating, String image, String name, String location, String time){
return Stack(
children: <Widget>[
Positioned(
child: Row(
children: <Widget>[
Padding(
padding: EdgeInsets.only(left: 20.0, right: 20.0, top: 30.0),
child: Container(
height: 80.0,
width: 80.0,
decoration: BoxDecoration(
color: Colors.blue,
shape: BoxShape.circle,
image: DecorationImage(
fit: BoxFit.fill,
image: ExactAssetImage(image),
),
)),
),
Container(
margin: EdgeInsets.only(left: 20.0),
child: RatingBar(
onRatingChanged: (rating) => setState(() => _rating = rating),
filledIcon: Icons.star,
emptyIcon: Icons.star_border,
halfFilledIcon: Icons.star_half,
isHalfAllowed: true,
filledColor: Colors.yellow,
emptyColor: Colors.grey,
halfFilledColor: Colors.yellow,
size: 20,
),
),
Container(
margin: EdgeInsets.only(left: 10.0),
child: Text(time, style: TextStyle(color: Colors.black, fontSize: 10.0),),
),
],
),
),
Column(
children: <Widget>[
Container(
padding: EdgeInsets.only(top: 110.0, left: 5.0),
child: Text(name),
),
Container(
padding: EdgeInsets.only(top: 5.0, left: 10.0),
child: Text(location),
),
],
),
Container(
margin: EdgeInsets.only(left: 140.0, top: 70.0, bottom: 10.0),
width: 200.0,
child: Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt" +
"ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat." ),
),
],
);
}
}
答案 0 :(得分:0)
这不是应该的工作
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: Scaffold(
body: MyHomePage()
),
routes: {
'home': (context) => MyHomePage(),
'page1': (context) => Page1(),
}
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Container(
child: Card(
child: Center(
child: Column(
children: <Widget>[
IconButton(
icon: Icon(Icons.transit_enterexit),
onPressed: () { Navigator.pushNamed(context, 'page1');}
)
],
),
)
)
);
}
}
class Page1 extends StatefulWidget {
Page1({Key key}) : super(key: key);
_Page1State createState() => _Page1State();
}
Future<bool> controlBackButton() async {
return false;
}
class _Page1State extends State<Page1> {
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () {return controlBackButton();},
child: Container(
child: Card(
child: ListTile(
title: Text('Hello'),
subtitle: Text('Click home to go back'),
trailing: IconButton(
icon: Icon(Icons.home),
onPressed: () { Navigator.pop(context); }
),
),
)
),
);
}
}