【问题标题】:Flutter Alert Dialog Not CenteredFlutter 警报对话框未居中
【发布时间】:2020-10-01 23:42:46
【问题描述】:

我正在创建一个 Flutter 移动应用程序。我有一个带抽屉的脚手架,它在应用程序中居中,如下所示:

居中抽屉:https://ibb.co/s5BMdNM

当用户点击第二个 ListTile 即“过滤字符/选择类别”时,会弹出一个 AlertDialog。问题是警报对话框在横向中既不是垂直居中也不是水平居中,在纵向中也不是垂直居中。

横向警报对话框:https://ibb.co/GtdJ2jZ

人像警报对话框:https://ibb.co/HH6vQbx

我尝试计算错位的百分比并将其添加为右侧的填充,当设备方向为 LandscapeRight 时它工作,但当方向变为 LandscapeLeft 时再次错位。

右调整:https://ibb.co/h9K0YB3

左错位:https://ibb.co/JykZ67x

我已经搜索过,但目前在获取设备的真实方向时出现问题。

那么谁能帮我将 AlertDialog 居中,或者告诉我为什么它没有居中?提前致谢。

总结抽屉代码:

Scaffold(
key: scaffoldKey,
backgroundColor: Colors.grey[100],
body: _getScaffoldBody(homePageState, context),
drawer: Center(
child: Container(
  alignment: Alignment.center,
  width: MediaQuery.of(context).size.width * 0.9 > 375
      ? 375
      : MediaQuery.of(context).size.width * 0.9,
  height: MediaQuery.of(context).size.height * 0.9 > 58.0 * 6
      ? 58.0 * 6 // 58 * Number of List Tiles
      : MediaQuery.of(context).size.height * 0.9,
  decoration: new BoxDecoration(
    color: Colors.white,
    borderRadius: new BorderRadius.all(Radius.circular(5)),
  ),
  child: ListView(
    shrinkWrap: true,
    padding: EdgeInsets.zero,
    physics: BouncingScrollPhysics(),
    children: <Widget>[
      Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          ListTile(
            title: Text('Filter Characters / Choose Categories'),
            trailing: Icon(
              Icons.search,
              color: Colors.blue,
            ),
            onTap: () {
              showCategoriesDialg(homePageState, context);
            },
          ),
        ],
      ),
    ),
  ),
),

显示 AlertDialog 的代码:

Future showCategoriesDialg(State<HomePage> homePageState, BuildContext context) async {
  return showDialog<void>(
    context: context,
    barrierDismissible: false, // user must tap a button!
    builder: (BuildContext context) {
      return AlertDialog(
        scrollable: true,
        content: Container(
          width: MediaQuery.of(context).size.width,
          height: MediaQuery.of(context).size.height - 200,
          child: ListView.builder(
            itemCount: categories.length,
            itemBuilder: (context, index) {
              return CheckboxListTile(
                title: Text(categories.values.elementAt(index).titleEn),
                value: categories.values.elementAt(index).checked,
                onChanged: (newValue) {
                  homePageState.setState(() {
                    categories.values.elementAt(index).checked = newValue;
                  });
                },
                controlAffinity: ListTileControlAffinity.trailing,
              );
            },
          ),
        ),
      );
    },
  );
}

【问题讨论】:

  • @pskink 我尝试用这个替换容器来测试:content: ListBody( children: &lt;Widget&gt;[ Text('This is a demo alert dialog.'), Text('Would you like to approve of this message?'), ], ), 但它仍然没有居中。
  • 我认为这与 2 个弹出窗口(抽屉和 AlerDialog)在彼此面前有关?
  • @pskink 不幸的是没有 =(

标签: flutter flutter-alertdialog


【解决方案1】:

这是获取设备方向的方法

 void main() => runApp(MyApp());

 class MyApp extends StatelessWidget {

   Widget _portraitView(){

     // Return Your Widget View Here Which you want to Load on Portrait Orientation.
     return Container(
    width: 300.00,
     color: Colors.green,
     padding: EdgeInsets.fromLTRB(10, 10, 10, 10),
     child: Text(' Portrait View Detected. ',
             textAlign: TextAlign.center,  
             style: TextStyle(fontSize: 24, color: Colors.white)));
   } 

    Widget _landscapeView(){

     // // Return Your Widget View Here Which you want to Load on Landscape      Orientation.
     return Container(
     width: 300.00,
     color: Colors.pink,
     padding: EdgeInsets.fromLTRB(10, 10, 10, 10),
     child: Text(' Landscape View Detected.',
             textAlign: TextAlign.center,  
             style: TextStyle(fontSize: 24, color: Colors.white)));
   }

   @override
   Widget build(BuildContext context) {
     return MaterialApp(
         home: Scaffold(
         appBar: AppBar(
         title: Text('Detect Device Screen Orientation')),
         body: OrientationBuilder(
           builder: (context, orientation) {
           return Center(

             child: orientation == Orientation.portrait 
             ? _portraitView() 
             : _landscapeView() 

                 );
            }
           )
          )
       );
   }
 }

获得正确的设备方向后,您可以相应地放置小部件。 并且不要使用固定值作为高度和宽度 总是使用 MediaQuery.of(context).size.height * percentYouWant MediaQuery.of(context).size.height * 0.5

等等

希望能帮到你

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-21
    • 2017-10-31
    • 1970-01-01
    • 1970-01-01
    • 2021-01-17
    • 2023-03-13
    • 2020-12-30
    • 1970-01-01
    相关资源
    最近更新 更多