首页 > 解决方案 > 在 Gridview.count 之后放置一个按钮 - Flutter

问题描述

我有四张卡片现在我想要一个按钮在最后并居中它会怎样

现有的网格视图

标签: android-layoutflutter

解决方案


  1. 在列中插入 GridView.counter
  2. 将 shrinkWrap 和主要属性设置为 true
  3. 将您的按钮放在 Column 内部和 gridview 下方,而不是放在按钮内部。
  4. 将按钮放在 Center Widget / set crossAxisAlignment: CrossAxisAlignment.center

    import 'package:flutter/material.dart';
    
    class Test extends StatefulWidget {
      @override
      _TestState createState() => _TestState();
    }
    
    class _TestState extends State<Test> with SingleTickerProviderStateMixin{
    
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text("Page - 2"),
          ),
          body: SafeArea(
            child: Column(
              children: <Widget>[
                GridView.count(
                  shrinkWrap: true,
                  primary: true,
                  crossAxisCount: 2,
                  children: <Widget>[
                    Container(
                      color: Colors.red,
                    ),
                    Container(
                      color: Colors.green,
                    ),
                    Container(
                      color: Colors.blue,
                    ),
                    Container(
                      color: Colors.yellow,
                    ),
    
                  ],
                ),
                Divider(
                  color: Colors.grey.shade600,
                ),
                Center(
                  child: RaisedButton(
                    child: Text("Button"),
                    onPressed: (){},
                  ),
                )
              ],
            ),
          ),
        );
      }
    }
    

推荐阅读