如何解决不同分辨率设备上的Flutter AlertDialog小部件大小问题
https://i.stack.imgur.com/oUxNk.jpg
我想要这样。我在RadioListTile
中使用过ALertDialog
。这会导致在不同的android设备中出现大小不同的对话框。因此导致像素错误溢出或只是黑屏。
这是我的代码
@override Widget build(BuildContext context) { Size size = MediaQuery.of(context).size;
return AlertDialog(
insetPadding: EdgeInsets.symmetric(
horizontal: SizeConfig.safeBlockHorizontal * 16,vertical: SizeConfig.safeBlockHorizontal * 40),title: Text("Methods"),content: ListView(children: <Widget>[
Container(
decoration: Boxdecoration(
shape: BoxShape.rectangle,border: Border.all(
color: Theme.of(context).hintColor,width: 1.0)),child: RadioListTile(
value: 1,groupValue: _crtIndex,title: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,children: [
Text(
"12 months",style: TextStyle(
fontWeight: FontWeight.bold,fontSize: 13),),Text(
"Rs.4,375",],SizedBox(width: size.width * 0.005),Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,children: [
Text(
"50% off",style: TextStyle(
fontWeight: FontWeight.normal,fontSize: 13,Text(
"9,876",style: TextStyle(
fontWeight: FontWeight.normal,decoration: Textdecoration.lineThrough),activeColor: Colors.teal,onChanged: (val) {
setState(() {
_crtIndex = val;
});
}),SizedBox(height: size.height * 0.01),new FlatButton(
color: Colors.red,child: new Text('BUY Now AND SAVE'),onpressed: () {
Navigator.of(context).pop();
},]));
我还尝试将AlertDialog
放在SizedBox
内以及Container
内,并尝试设置高度和宽度。
我已经使用Size size = MediaQuery.of(context).size;
来获取高度和宽度。
我也尝试过insetPadding: EdgeInsets.symmetric(horizontal: *value,vertical: *value),
什么都没有。
我是新手。请帮忙。预先谢谢你
解决方法
看看我做的这个例子:
import 'package:flutter/material.dart';
import 'package:json_parsing_example/offerScreen.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Login(),debugShowCheckedModeBanner: false,);
}
}
class Login extends StatefulWidget {
@override
_LoginState createState() => _LoginState();
}
class _LoginState extends State<Login> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Center(
child: Column(
children: <Widget>[
RaisedButton(
child: Text('Tap'),onPressed: () async {
var response = await Navigator.push(
context,MaterialPageRoute(
builder: (context) => OfferPage(),));
if (response !=
null) // This is when the user presses the back button
{
// if user inputs anything then this is where the output will come
print(response);
}
//this response is you value that you selected
// you can do processing on this page or the selection
//page
},),RaisedButton(
child: Text('Tap2'),onPressed: () {
showModalBottomSheet(
isScrollControlled: true,context: context,shape: RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(
top: Radius.circular(20),clipBehavior: Clip.antiAliasWithSaveLayer,builder: (context) => Container(
height: MediaQuery.of(context).size.height * 0.95,child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.end,children: <Widget>[
Padding(
padding: const EdgeInsets.only(right: 12,top: 15),child: GestureDetector(
onTap: () {
Navigator.of(context).pop(null);
},child: Icon(
Icons.close,size: 30,],Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[
Container(
margin: EdgeInsets.symmetric(horizontal: 10),decoration: BoxDecoration(
shape: BoxShape.rectangle,border: Border.all(
color: Theme.of(context).hintColor,width: 0.5)),child: Padding(
padding: const EdgeInsets.all(8.0),child: RadioListTile(
value: 1,groupValue: 1,title: Column(
children: [
Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,children: [
Text(
"12 months",style: TextStyle(
fontWeight: FontWeight.bold,fontSize: 13),Text(
"Rs.4,375",SizedBox(
width: MediaQuery.of(context)
.size
.width *
0.005),Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,children: [
Text(
"50% off",style: TextStyle(
fontWeight: FontWeight.normal,fontSize: 13,Text(
"9,876",style: TextStyle(
fontWeight: FontWeight.normal,decoration: TextDecoration
.lineThrough),activeColor: Colors.teal,onChanged: (val) {
setState(() {});
}),SizedBox(
height: 10,Container(
margin: EdgeInsets.symmetric(horizontal: 10),width: 1.0)),SizedBox(
height:
MediaQuery.of(context).size.height * 0.01),Container(
width: MediaQuery.of(context).size.width,height: 60,child: Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 10),child: new FlatButton(
color: Colors.red,child: new Text(
'BUY NOW AND SAVE',style: TextStyle(
color: Colors.white,onPressed: () {
Navigator.of(context).pop();
},);
},)));
}
}
这是要约页面:
import 'package:flutter/material.dart';
class OfferPage extends StatefulWidget {
@override
_OfferPageState createState() => _OfferPageState();
}
class _OfferPageState extends State<OfferPage> {
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return Scaffold(
backgroundColor: Colors.white,body: SafeArea(
child: Container(
height: MediaQuery.of(context).size.height,width: MediaQuery.of(context).size.width,child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.end,children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),child: GestureDetector(
onTap: () {
Navigator.of(context).pop(null);
},child: Icon(
Icons.close,Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[
Container(
margin: EdgeInsets.symmetric(horizontal: 10),decoration: BoxDecoration(
shape: BoxShape.rectangle,border: Border.all(
color: Theme.of(context).hintColor,child: Padding(
padding: const EdgeInsets.all(8.0),child: RadioListTile(
value: 1,title: Column(
children: [
Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,children: [
Text(
"12 months",style: TextStyle(
fontWeight: FontWeight.bold,Text(
"Rs.4,SizedBox(width: size.width * 0.005),Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,children: [
Text(
"50% off",style: TextStyle(
fontWeight: FontWeight.normal,Text(
"9,style: TextStyle(
fontWeight: FontWeight.normal,decoration:
TextDecoration.lineThrough),onChanged: (val) {
setState(() {});
}),SizedBox(
height: 10,Container(
margin: EdgeInsets.symmetric(horizontal: 10),SizedBox(height: size.height * 0.01),Container(
width: MediaQuery.of(context).size.width,child: Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 10),child: new FlatButton(
color: Colors.red,child: new Text(
'BUY NOW AND SAVE',style: TextStyle(
color: Colors.white,onPressed: () {
Navigator.of(context).pop("your selected value");
},);
}
}
检查一下,让我知道它是否有效。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。