flutter开发小技巧

简介: 粘性标题效果带有粘性标题的每个部分都应该是带有 SliverPinnedHeader 和 SliverList 的多条。然后将 pushPinnedChildren 设置为 true 应该会提供您正在寻找的粘性标题效果。

粘性标题效果

带有粘性标题的每个部分都应该是带有 SliverPinnedHeader 和 SliverList 的多条。然后将 pushPinnedChildren 设置为 true 应该会提供您正在寻找的粘性标题效果。

flutter - 如何将Gridview放在Column小部件中的Card小部件附近

正在尝试为我的应用创建用户界面。我需要这样设计:但是我最近的用户界面如下所示:在第二个屏幕截图中,我向下滚动是因为我的gridview小部件(具有4个卡式小部件)没有更接近其他小部件。其他小部件是:标题栏小部件,具有图形和子图小部件的卡片小部件。我将标题栏和图形卡小部件放在堆栈中以在上方显示它们。我将这两个组合的小部件与gridview小部件一起放在一列中。我用SingleChildScrollView小部件包装此列小部件,因为我希望页面可滚动。 因此,我的问题是:如何像第一个图像那样将gridview小部件放置得更靠近卡片小部件?这是我用于此UI的代码:

import 'package:flutter/material.dart';
import 'package:flutter_circular_chart/flutter_circular_chart.dart';
import 'package:intl/intl.dart';
import 'baskana_rapor_icon_icons.dart';
// ignore: must_be_immutable
class MainPage extends StatelessWidget {
  List<Widget> widgets = new List();
  List<CircularSegmentEntry> dataList = _loadData();
  int _totalCount;
  Widget s1, s2, total;
  final formatter = new NumberFormat("#,##");
  @override
  Widget build(BuildContext context) {
    widgets.add(_buildBody(context));
    return Scaffold(body: _buildBody(context));
  }
  Widget _buildBody(BuildContext context) {
    return LayoutBuilder(
      builder: (BuildContext context, BoxConstraints viewportConstraints) {
        return SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            //mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Stack(
                children: <Widget>[
                  Container(
                    height: MediaQuery.of(context).size.height,
                    width: MediaQuery.of(context).size.width,
                  ),
                  _buildTitleBar(context),
                  Positioned(bottom: 65, left: 35, child: _buildCard(context)),
                ],
              ),
              _buildGridButtons(context),
            ],
          ),
        );
      },
    );
  }
  Widget _buildGridButtons(BuildContext context) {
    int itemWidth = 80;
    int itemHeight = 40;
    return SafeArea(
      child: Column(
        children: [
          GridView.count(
            crossAxisCount: 2,
            childAspectRatio: (itemWidth / itemHeight),
            shrinkWrap: true,
            primary: true,
            children: [
              GestureDetector(
                onTap: () {
                  Navigator.pushNamed(context, '/MahalleRapor');
                },
                child: Card(
                  color: Colors.white,
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(
                      Radius.circular(25),
                    ),
                  ),
                  child: Stack(
                    alignment: Alignment.center,
                    children: [
                      Column(
                        mainAxisSize: MainAxisSize.min,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        children: [
                          Icon(
                            BaskanaRaporIcon.mahalle_raporu,
                            color: Colors.redAccent,
                            size: 30,
                          ),
                          Text(
                            'Mahalle Raporu',
                            style: TextStyle(
                              color: Colors.black87,
                              fontSize: 20,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                          SizedBox(
                            height: 5,
                          )
                        ],
                      ),
                      Positioned(
                        bottom: 0,
                        right: 1,
                        left: 1,
                        child: Divider(
                          color: Colors.redAccent,
                          endIndent: 45,
                          indent: 50,
                          thickness: 3,
                        ),
                      ),
                    ],
                  ),
                ),
              ),
              GestureDetector(
                onTap: () {
                  Navigator.pushNamed(context, '/BirimRapor');
                },
                child: Card(
                  color: Colors.white,
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(
                      Radius.circular(25),
                    ),
                  ),
                  child: Stack(
                    alignment: Alignment.center,
                    children: [
                      Column(
                        mainAxisSize: MainAxisSize.min,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        children: [
                          Icon(
                            BaskanaRaporIcon.birim_raporu,
                            color: Colors.green[300],
                            size: 30,
                          ),
                          Text(
                            'Birim Raporu',
                            style: TextStyle(
                              color: Colors.black87,
                              fontSize: 20,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                          SizedBox(
                            height: 5,
                          )
                        ],
                      ),
                      Positioned(
                        bottom: 0,
                        right: 1,
                        left: 1,
                        child: Divider(
                          color: Colors.green[300],
                          endIndent: 45,
                          indent: 50,
                          thickness: 3,
                        ),
                      ),
                    ],
                  ),
                ),
              ),
              GestureDetector(
                onTap: () {
                  Navigator.pushNamed(context, '/GelirGider');
                },
                child: Card(
                  color: Colors.white,
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(
                      Radius.circular(25),
                    ),
                  ),
                  child: Stack(
                    alignment: Alignment.center,
                    children: [
                      Column(
                        mainAxisSize: MainAxisSize.min,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        children: [
                          Icon(
                            BaskanaRaporIcon.gelir_gider,
                            color: Colors.yellow[700],
                            size: 30,
                          ),
                          Text(
                            'Gelir / Gider',
                            style: TextStyle(
                              color: Colors.black87,
                              fontSize: 20,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                          SizedBox(
                            height: 5,
                          )
                        ],
                      ),
                      Positioned(
                        bottom: 0,
                        right: 1,
                        left: 1,
                        child: Divider(
                          color: Colors.yellow[700],
                          endIndent: 45,
                          indent: 50,
                          thickness: 3,
                        ),
                      ),
                    ],
                  ),
                ),
              ),
              GestureDetector(
                onTap: () {
                  Navigator.pushNamed(context, '/BaskanaMesaj');
                },
                child: Card(
                  color: Colors.white,
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(
                      Radius.circular(25),
                    ),
                  ),
                  child: Stack(
                    alignment: Alignment.center,
                    children: [
                      Column(
                        mainAxisSize: MainAxisSize.min,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        children: [
                          Icon(
                            BaskanaRaporIcon.baskana_msg,
                            color: Colors.blueAccent,
                            size: 30,
                          ),
                          Text(
                            'Başkana Mesaj',
                            style: TextStyle(
                              color: Colors.black87,
                              fontSize: 20,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                          SizedBox(
                            height: 5,
                          )
                        ],
                      ),
                      Positioned(
                        bottom: 0,
                        right: 1,
                        left: 1,
                        child: Divider(
                          color: Colors.blueAccent,
                          endIndent: 45,
                          indent: 50,
                          thickness: 3,
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ],
          )
        ],
      ),
    );
  }
  // ignore: todo
  //TODO: Better implementation of UI
  final double buttonHeight = 50;
  Widget _buildCard(BuildContext context) {
    return Center(
      child: Container(
        height: 500 + buttonHeight,
        child: Stack(
          alignment: Alignment.center,
          overflow: Overflow.visible,
          children: [
            Card(
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(15),
                side: BorderSide(color: Colors.blueGrey, width: 0.5),
              ),
              child: Container(
                height: MediaQuery.of(context).size.height * .65,
                width: MediaQuery.of(context).size.width * .80,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisSize: MainAxisSize.max,
                  children: <Widget>[
                    Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Text(
                        "Genel Durum",
                        style: TextStyle(
                          fontSize: 20,
                          color: Colors.black,
                          letterSpacing: 0.3,
                        ),
                      ),
                    ),
                    Divider(
                      color: Colors.grey,
                      thickness: 0.3,
                      endIndent: 10,
                      indent: 10,
                    ),
                    _buildChart(dataList),
                    SizedBox(
                      height: 20,
                    ),
                    _buildSubGraph(),
                    SizedBox(
                      height: 40,
                    ),
                  ],
                ),
              ),
            ),
            Positioned(
              //top: -buttonHeight /2,
              bottom: 27,
              child: _buildDetailsButton(context),
            ),
          ],
        ),
      ),
    );
  }
  Widget _buildDetailsButton(BuildContext context) {
    return ButtonTheme(
      height: 50,
      minWidth: 100,
      child: RaisedButton(
        onPressed: () {
          Navigator.pushNamed(context, '/DetailPage');
        },
        color: Colors.white,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(30),
        ),
        child: Text("Detayları Gör"),
      ),
    );
  }
  Row _buildSubGraph() {
    String s1 = formatter.format((dataList[1].value / _totalCount) * 100);
    String s0 = formatter.format((dataList[0].value / _totalCount) * 100);
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceAround,
      children: [
        Column(
          //İşlemde
          children: [
            Text(
              dataList[1].rankKey,
              style: TextStyle(
                fontSize: 18,
                color: Colors.black87,
              ),
            ),
            Container(
              decoration: BoxDecoration(
                color: dataList[1].color,
                borderRadius: BorderRadius.only(
                  topRight: Radius.circular(5),
                  bottomRight: Radius.circular(5),
                  bottomLeft: Radius.circular(5),
                ),
              ),
              height: 50,
              width: 150,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Text(
                    '${dataList[1].value.toInt()}',
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 20,
                    ),
                  ),
                  SizedBox(
                    width: 50,
                  ),
                  Flexible(
                    child: Text(
                      '%' + s1,
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 16,
                        fontWeight: FontWeight.w300,
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
        Column(
          //Sonuçlanan
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            Text(
              dataList[0].rankKey,
              style: TextStyle(
                fontSize: 18,
                color: Colors.black87,
              ),
            ),
            Container(
              height: 50,
              width: 150,
              decoration: BoxDecoration(
                color: dataList[0].color,
                borderRadius: BorderRadius.only(
                  topRight: Radius.circular(5),
                  bottomRight: Radius.circular(5),
                  bottomLeft: Radius.circular(5),
                ),
              ),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Text(
                    '${dataList[0].value.toInt()}',
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 20,
                    ),
                  ),
                  SizedBox(
                    width: 50,
                  ),
                  Flexible(
                    child: Text(
                      '%' + s0,
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 16,
                        fontWeight: FontWeight.w300,
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ],
        )
      ],
    );
  }
  //Map<String, double> dataMap, List<Color> colorList,BuildContext context
  Widget _buildChart(List<CircularSegmentEntry> dataList) {
    _totalCount = _findTotalCount(dataList);
    s1 = _createText("TOPLAMDA", 20, Colors.grey[600], false);
    s1 = _createText("TALEP", 20, Colors.grey[600], false);
    total = _createText('$_totalCount', 24, Colors.black87, true);
    return Container(
      child: AnimatedCircularChart(
        size: Size(500, 250),
        initialChartData: <CircularStackEntry>[
          CircularStackEntry(
            <CircularSegmentEntry>[
              dataList[0],
              dataList[1],
            ],
          ),
        ],
        chartType: CircularChartType.Radial,
        startAngle: -90,
        holeRadius: 25,
        holeLabel:
            "TOPLAMDA \n\t\t\t\t\t\t\t $_totalCount \n\t\t\t TALEP", // $s1 \n\t\t\t\t\t\t\t $total \n\t\t\t $s2
      ),
    );
  }
  Widget _buildTitleBar(BuildContext context) {
    return Align(
      alignment: Alignment.topCenter,
      child: Column(
        children: <Widget>[
          Container(
            height: MediaQuery.of(context).size.height * .247,
            decoration: BoxDecoration(
              color: Colors.blue,
              shape: BoxShape.rectangle,
              borderRadius: BorderRadius.vertical(
                bottom: Radius.circular(25),
              ),
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Icon(
                  Icons.assessment,
                  color: Colors.white,
                ),
                Text(
                  "Genel Durum",
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 28,
                    fontWeight: FontWeight.bold,
                    letterSpacing: 0.5,
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
Widget _createText(String msg, double size, Color color, bool isBold) {
  return Text(
    msg,
    style: TextStyle(
      color: color,
      fontSize: size,
      fontWeight: isBold ? FontWeight.bold : FontWeight.normal,
    ),
  );
}
int _findTotalCount(List<CircularSegmentEntry> dataList) {
  int result = 0;
  for (int i = 0; i < dataList.length; i++) {
    result += (dataList[i].value).toInt();
  }
  return result;
}
List<CircularSegmentEntry> _loadData() {
  List<CircularSegmentEntry> dataList = [];
  CircularSegmentEntry chartData1 =
      new CircularSegmentEntry(150, Colors.greenAccent, rankKey: 'Sonuçlanan');
  CircularSegmentEntry chartData2 =
      new CircularSegmentEntry(150, Colors.blue, rankKey: 'İşlemde');
  dataList.add(chartData1);
  dataList.add(chartData2);
  return dataList;
}
复制代码

额外的问题:如果我可以做些更有效的设计,您能告诉我吗?谢谢!

最佳答案

也许是由于堆栈中的这段代码而发生的。此容器占用您的全屏尺寸。这就是为什么您的GridView项目会在屏幕大小之后立即生成。尝试删除此容器或减小其高度。

Container(
     height: MediaQuery.of(context).size.height,
     width: MediaQuery.of(context).size.width,
   ),
复制代码

更新:你可以试试看我没有尝试过,但这也许会有所帮助

Stack(
    children: <Widget>[
      Align(
        alignment: Alignment.topCenter,
        child: _buildTitleBar(context),
      ),
      Align(
        alignment: Alignment.topCenter,
        child: Container(
          margin: EdgeInsets.only(top: 100),
          child: _buildCard(context),
        ),
      )
    ],
  )



相关文章
|
1月前
|
Android开发 iOS开发 容器
鸿蒙harmonyos next flutter混合开发之开发FFI plugin
鸿蒙harmonyos next flutter混合开发之开发FFI plugin
|
30天前
|
开发者
鸿蒙Flutter实战:07-混合开发
鸿蒙Flutter混合开发支持两种模式:1) 基于har包,便于主项目开发者无需关心Flutter细节,但不支持热重载;2) 基于源码依赖,利于代码维护与热重载,需配置Flutter环境。项目结构包括AppScope、flutter_module等目录,适用于不同开发需求。
72 3
|
14天前
|
传感器 开发框架 物联网
鸿蒙next选择 Flutter 开发跨平台应用的原因
鸿蒙(HarmonyOS)是华为推出的一款旨在实现多设备无缝连接的操作系统。为了实现这一目标,鸿蒙选择了 Flutter 作为主要的跨平台应用开发框架。Flutter 的跨平台能力、高性能、丰富的生态支持和与鸿蒙系统的良好兼容性,使其成为理想的选择。通过 Flutter,开发者可以高效地构建和部署多平台应用,推动鸿蒙生态的快速发展。
121 0
|
17天前
|
Dart 安全 UED
Flutter&鸿蒙next中的表单封装:提升开发效率与用户体验
在移动应用开发中,表单是用户与应用交互的重要界面。本文介绍了如何在Flutter中封装表单,以提升开发效率和用户体验。通过代码复用、集中管理和一致性的优势,封装表单组件可以简化开发流程。文章详细讲解了Flutter表单的基础、封装方法和表单验证技巧,帮助开发者构建健壮且用户友好的应用。
58 0
|
1月前
|
开发框架 移动开发 Android开发
安卓与iOS开发中的跨平台解决方案:Flutter入门
【9月更文挑战第30天】在移动应用开发的广阔舞台上,安卓和iOS两大操作系统各自占据半壁江山。开发者们常常面临着选择:是专注于单一平台深耕细作,还是寻找一种能够横跨两大系统的开发方案?Flutter,作为一种新兴的跨平台UI工具包,正以其现代、响应式的特点赢得开发者的青睐。本文将带你一探究竟,从Flutter的基础概念到实战应用,深入浅出地介绍这一技术的魅力所在。
76 7
|
30天前
|
编解码 Dart API
鸿蒙Flutter实战:06-使用ArkTs开发Flutter鸿蒙插件
本文介绍了如何开发一个 Flutter 鸿蒙插件,实现 Flutter 与鸿蒙的混合开发及双端消息通信。通过定义 `MethodChannel` 实现 Flutter 侧的 token 存取方法,并在鸿蒙侧编写 `EntryAbility` 和 `ForestPlugin`,使用鸿蒙的首选项 API 完成数据的读写操作。文章还提供了注意事项和参考资料,帮助开发者更好地理解和实现这一过程。
56 0
|
30天前
|
Dart Android开发
鸿蒙Flutter实战:03-鸿蒙Flutter开发中集成Webview
本文介绍了在OpenHarmony平台上集成WebView的两种方法:一是使用第三方库`flutter_inappwebview`,通过配置pubspec.lock文件实现;二是编写原生ArkTS代码,自定义PlatformView,涉及创建入口能力、注册视图工厂、处理方法调用及页面构建等步骤。
48 0
|
2月前
|
JSON Dart Java
flutter开发多端平台应用的探索
flutter开发多端平台应用的探索
50 6
|
2月前
|
JSON Dart Java
flutter开发多端平台应用的探索 下 (跨模块、跨语言通信之平台通道)
flutter开发多端平台应用的探索 下 (跨模块、跨语言通信之平台通道)
|
2月前
|
安全 Android开发 开发者
探索安卓开发的未来:Kotlin的崛起与Flutter的挑战
在移动开发的广阔天地中,安卓平台始终占据着举足轻重的地位。随着技术的不断进步和开发者需求的多样化,Kotlin和Flutter成为了改变游戏规则的新玩家。本文将深入探讨Kotlin如何以其现代化的特性赢得开发者的青睐,以及Flutter凭借跨平台的能力如何挑战传统的安卓开发模式。通过实际案例分析,我们将揭示这两种技术如何塑造未来的安卓应用开发。
73 6