【问题标题】:How to add responsiveness to Tablets through Positioned and padding widget?如何通过定位和填充小部件为平板电脑添加响应能力?
【发布时间】:2020-08-21 05:10:56
【问题描述】:

我正在尝试在 LayoutBuilder 中使用 MediaQuery,并且对于所有 Positioned - Material 按钮,我需要为平板电脑、小型手机和大型手机添加不同的逻辑,并且我想通过 mediaQuery 为这些添加 if 和 else 语句

当我将这个 if 语句放在定位时,它显示错误。

@override
  Widget build(BuildContext context) {
    var deviceData = MediaQuery.of(context);
    double _width = MediaQuery.of(context).size.width;
    double _height = MediaQuery.of(context).size.height;
    var screenSize = MediaQuery.of(context).size;
    return LayoutBuilder(builder: (context, constraints) {
      return OrientationBuilder(builder: (context, orientation) {
      Size size = MediaQuery.of(context).size;
double width = size.width > size.height ? size.height : size.width;      
        if (MediaQuery.of(context).size.width > 600) {
          isLargeScreen = true;
        } else {
          isLargeScreen = false;
        }
        SizeConfig().init(constraints, orientation);


  Positioned (
                                // height: context.heightPct(.99),
                                // width: context.widthPct(.99),
                                // Container(
                                // alignment: Alignment(,-.3),
                                //  child:   FractionallySizedBox(
                                // widthFactor: 30,                      
                               if (isLargeScreen){
                                width: 213 * SizeConfig.imageSizeMultiplier,
                                height: 92 * SizeConfig.heightMultiplier,
                                  }else{
                                height: 92 * SizeConfig.heightMultiplier,
                                width: 213 * SizeConfig.imageSizeMultiplier,

 child: Opacity(
                                  opacity: 0.9,
                                  // left: width*0.5,
                                  // top: 900,
                                  // top: 100,
                                  child: _visible
                                      ? MaterialButton(
                                          // minWidth:screenSize.width*0.1,
                                          // 0.1 = 10% of the screen , 0.5 = 50% of the screen
                                          // ios sizes usually are 620...
                                          // height: screenSize.height*0.1,
                                          onPressed: () => changeTextArabic1(),
                                          onLongPress: () =>
                                              changeTextEnglish1(),
                                          // shape: new RoundedRectangleBorder(borderRadius: new BorderRadius.circular(60.0)
                                          // child: Text(labels[i]),
                                          // ),
                                          child: Ink(
                                            child: Text(surah0),
                                            //  disabledTextColor: Colors.transparent,
//                       color: Colors.cyan[300],
//                       // color: Colors.purple[300],
//                       highlightColor: Colors.blue,
//                       // shape: new RoundedRectangleBorder(borderRadius: new BorderRadius.circular(60.0)
//                       // ),
//                       textColor: Colors.white,
                                            // color: Colors.cyan[400],

                                            padding: EdgeInsets.all(21.0),
                                            // padding: EdgeInsets.only(
                                            //     left: 28,
                                            //     top: 25,
                                            //     right: 28,
                                            //     bottom: 25),
                                            decoration: BoxDecoration(
                                              gradient: LinearGradient(
                                                colors: [
// all together looks COOL royal Blue:
                                                  Colors.red,
                                                  Colors.blue,
                                                  // Color(0xff374ABE),
                                                  // Color(0xff64B6FF),
                                                  // Color(0xff0E0077),
                                                  // Color(0xff1B00EE)
                                                ],
                                                // begin: Alignment.centerLeft,
                                                // end: Alignment.centerRight,
                                              ),
                                            ),
                                          ),
                                          // color: Colors.blue[300],
                                          // highlightColor: Colors.blue,
                                          // ),
                                          textColor: Colors.white,
                                        )
                                      : Container(),
                                  // ),

                                  //  ):Container()
                                ),
      
                                  }),
                            

【问题讨论】:

  • 使用isLargeScreen? someThing : someThingElse

标签: flutter dart flutter-layout flutter-dependencies


【解决方案1】:

你可以使用:

isLargeScreen 
? Positioned()
: Positioned()

或:

Positioned(
  height: isLargeScreen ? something : something,
  width: isLargeScreen ? something : something
)

【讨论】:

  • 谢谢,我试过了,但我得到了错误:// Iphone x width: 213 * SizeConfig.imageSizeMultiplier, height: 92 * SizeConfig.heightMultiplier, // Ipad AIr height: isLargeScreen? 92 * SizeConfig.heightMultiplier,宽度:isLargeScreen?第213章
  • 嗨,我的问题是我如何才能为两种不同的手机尺寸添加这个......使用相同的定位小部件......?
  • 所以您可以使用我的第二段代码,只需将“某物”替换为您选择的屏幕尺寸
  • 嗨,对不起,我试过了,我得到了 iPad 和 Iphone 的最高号码,但最低号码什么也没做:高度:isLargeScreen? 92 * SizeConfig.heightMultiplier // Iphone 上方:213 * SizeConfig.heightMultiplier, // IPad 上方宽度:isLargeScreen ? 130 * SizeConfig.imageSizeMultiplier // Iphone 以上 : 230 * SizeConfig.imageSizeMultiplier,
  • 我什至试过: Positioned( height: isLargeScreen ? 95 * SizeConfig.heightMultiplier: isSmallScreen? 95 * SizeConfig.heightMultiplier, width: isLargeScreen ? 125 * SizeConfig.imageSizeMultiplier: isSmallScreen? 125 * SizeConfig.imageSizeMultiplier no运气....请帮忙
猜你喜欢
  • 2015-05-30
  • 1970-01-01
  • 2013-06-29
  • 2019-08-03
  • 1970-01-01
  • 2012-08-05
  • 2016-06-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多