Flutter Expanded 弹性、自适应、充满剩余

参数详解

属性说明
flex弹性
child元素

 代码示例

class MyBody extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Text('后自适应'),
        Row(
          children: <Widget>[
            MyImage('https://raw.githubusercontent.com/think-ing/flutter_demo/master/images/a.jpg'),
            Expanded(
              child: MyImage('https://raw.githubusercontent.com/think-ing/flutter_demo/master/images/b.jpg'),
            )
          ]
        ),

        Text('前自适应'),
        Row(
          children: <Widget>[
            Expanded(
              child: MyImage('https://raw.githubusercontent.com/think-ing/flutter_demo/master/images/b.jpg'),
            ),
            MyImage('https://raw.githubusercontent.com/think-ing/flutter_demo/master/images/a.jpg'),
          ]
        ),
        
        Text('1:2 比例'),
        Row(
          children: <Widget>[
            Expanded(
              flex: 1,
              child: MyImage('https://raw.githubusercontent.com/think-ing/flutter_demo/master/images/a.jpg'),
            ),
            
            Expanded(
              flex: 2,
              child: MyImage('https://raw.githubusercontent.com/think-ing/flutter_demo/master/images/b.jpg'),
            )
          ]
        ),


        Text('2:1 比例'),
        Row(
          children: <Widget>[
            Expanded(
              flex: 2,
              child: MyImage('https://raw.githubusercontent.com/think-ing/flutter_demo/master/images/a.jpg'),
            ),
            
            Expanded(
              flex: 1,
              child: MyImage('https://raw.githubusercontent.com/think-ing/flutter_demo/master/images/b.jpg'),
            )
          ]
        ),

        Text('1:2:1 比例'),
        Row(
          children: <Widget>[
            Expanded(
              flex: 1,
              child: MyImage('https://raw.githubusercontent.com/think-ing/flutter_demo/master/images/a.jpg'),
            ),
            
            Expanded(
              flex: 2,
              child: MyImage('https://raw.githubusercontent.com/think-ing/flutter_demo/master/images/b.jpg'),
            ),
            Expanded(
              flex: 1,
              child: MyImage('https://raw.githubusercontent.com/think-ing/flutter_demo/master/images/c.jpg'),
            )
          ]
        ),


        Text('2:1:2 比例'),
        Row(
          children: <Widget>[
            Expanded(
              flex: 2,
              child: MyImage('https://raw.githubusercontent.com/think-ing/flutter_demo/master/images/a.jpg'),
            ),
            
            Expanded(
              flex: 1,
              child: MyImage('https://raw.githubusercontent.com/think-ing/flutter_demo/master/images/b.jpg'),
            ),
            Expanded(
              flex: 2,
              child: MyImage('https://raw.githubusercontent.com/think-ing/flutter_demo/master/images/c.jpg'),
            )
          ]
        ),

      ],
    );
  }
}



//定义一个 公共类
class MyImage extends StatelessWidget {
  String imgUrl;
  MyImage(this.imgUrl);
  @override
  Widget build(BuildContext context) {
    return Container(
      height: 100,
      width: 100,
      child: Image.network(this.imgUrl,fit: BoxFit.cover,),
    );
  }
}

 水平布局可以自适应,垂直布局也是一样

class MyBody extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
            MyImage('https://raw.githubusercontent.com/think-ing/flutter_demo/master/images/a.jpg'),
            Expanded(
              child: MyImage('https://raw.githubusercontent.com/think-ing/flutter_demo/master/images/b.jpg'),
            )
          ]
    );
  }
}

 效果图

                     水平自适应                                                          垂直自适应

       

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要实现 Flutter WebView 页面自适应高度,可以使用 flutter_webview_plugin 插件。该插件提供了一个 onStateChanged 回调函数,可以监听 WebView 页面的状态改变。当页面加载完成后,可以获取 WebView 页面的高度,并将其应用到 WebView 上,从而实现自适应高度。 以下是一个简单的示例代码,演示如何实现 Flutter WebView 页面自适应高度: ```dart import 'package:flutter/material.dart'; import 'package:flutter_webview_plugin/flutter_webview_plugin.dart'; class MyWebView extends StatefulWidget { final String url; MyWebView({required this.url}); @override _MyWebViewState createState() => _MyWebViewState(); } class _MyWebViewState extends State<MyWebView> { double webViewHeight = 100.0; @override Widget build(BuildContext context) { return Scaffold( body: Container( height: webViewHeight, child: WebviewScaffold( url: widget.url, withZoom: false, withJavascript: true, withLocalStorage: true, hidden: true, initialChild: Container( color: Colors.white, child: Center( child: CircularProgressIndicator(), ), ), appBar: AppBar( title: Text('WebView Demo'), ), onStateChanged: (state) { if (state.type == WebViewState.finishLoad) { setState(() { webViewHeight = double.parse(state.height.toString()); }); } }, ), ), ); } } ``` 在上面的代码中,我们定义了一个 MyWebView 类,使用 WebviewScaffold 来渲染 WebView 页面。在 onStateChanged 回调函数中,我们监听 WebView 页面的状态改变,并在页面加载完成后获取 WebView 的高度,并将其应用到 WebView 上。这样,就可以实现 WebView 页面的自适应高度了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

马志武

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值