【问题标题】:Stack with circular progress bar and elevated button in flutter在颤动中堆叠圆形进度条和升高的按钮
【发布时间】:2022-01-12 06:25:57
【问题描述】:

我想在屏幕截图中实现以下 UI,并通过以下代码在两部不同的手机上实现。但同样不适用于许多其他设备。有没有办法处理CircularProgressIndicator 的大小(目前我正在使用Transform.scale(),它没有给我一般的结果),以便它适合堆栈内的兄弟容器?

编辑: 使用CircularProgressIndicator的原因是为了显示进度。

Widget build(BuildContext context) {
    final mediaq = MediaQuery.of(context).size;
    return Stack(alignment: Alignment.center, children: [
      Transform.scale(
        scale: mediaq.width <= 360 ? 2.35 : 2.70,
        child: const CircularProgressIndicator(
          backgroundColor: Colors.white,
          color: Colors.orange,
          strokeWidth: .75,
          value: 1,
        ),
      ),
      Container(
        decoration: const BoxDecoration(
          shape: BoxShape.circle,
          gradient: LinearGradient(
            colors: [Colors.red, Colors.orange],
            begin: Alignment.topLeft,
            end: Alignment.bottomRight,
          ),
        ),
        child: ElevatedButton(
            style: ElevatedButton.styleFrom(
              primary: Colors.transparent,
              fixedSize: Size(mediaq.width * .24, mediaq.height * .12),
              shape: const CircleBorder(side: BorderSide.none),
            ),
            onPressed: () {},
            child: const Text("child")),
      ),
    ]);
  }

【问题讨论】:

  • 能否附上其他设备的截图?还有设备信息。
  • @FaiiziiAwan 上面截图的设备是像素 4a。宽度为 392.72,高度为 753.45。
  • 您说在其他设备上也无法正常工作。那些设备是什么?结果是什么?它与期望的结果有何不同?
  • @FaiiziiAwan iPhone SE 例如不显示任何内容。宽:320,高:568。

标签: flutter flutter-layout


【解决方案1】:

重新编写你的代码,这是我的输出

Transform.scale(
    scale: mediaq.width> 375 ? 3.10 : 2.35,
    child: const CircularProgressIndicator(
      backgroundColor: Colors.white,
      color: Colors.white,
      strokeWidth: .75,
      value: 1,
    ),
  ),

输出: Iphone 12 Pro Max(宽:428,高:926)

Iphone SE(第二代)(宽:375,高:667)

你也可以试试这个package 的响应式用户界面。

【讨论】:

  • 在 iPad 的情况下,这是行不通的。
  • 我只是比较了 2 个屏幕尺寸。如果你想要完整的响应式用户界面,那么你必须使用 screenutils 或类似的包,或者你必须编写自己的响应式代码。示例: scale: mediaq.width > 375 && mediaq.width 1000? 5.00:2.35,
【解决方案2】:

您可以使用 flutter_screenutil 包来使您的 UI 更具响应性,而不是使用 MediaQuery。

我使用 flutter_screenutil 包完成了你的屏幕。 请试试这个。我假设了高度、宽度和半径。您可以根据您的要求修改这些。我还附上了我得到的 UI。

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
        designSize: const Size(375, 667),
        minTextAdapt: true,
        builder: () {
          return MaterialApp(
            title: 'Flutter Demo',
            theme: ThemeData(
              primarySwatch: Colors.blue,
            ),
            home: const ButtonTest(),
          );
        });
  }
}

class ButtonTest extends StatefulWidget {
  const ButtonTest({Key? key}) : super(key: key);

  @override
  _ButtonTestState createState() => _ButtonTestState();
}

class _ButtonTestState extends State<ButtonTest> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      body: Center(
        child: Container(
          height: 94.h, // You can change here
          decoration: BoxDecoration(
            shape: BoxShape.circle,
            border: Border.all(
              color: Colors.white,
            ),
          ),
          child: Padding(
            padding: EdgeInsets.all(
              2.0.w, // You can change here
            ),
            child: GestureDetector(
              onTap: () {
                print("Button Pressed");
              },
              child: Container(
                height: 92.h, // You can change here
                width: 92.h, // You can change here
                decoration: const BoxDecoration(
                  shape: BoxShape.circle,
                  gradient: LinearGradient(
                    colors: [
                      Colors.red,
                      Colors.orange,
                    ],
                  ),
                ),
                child: const Center(
                    child: Text(
                  'child',
                  style: TextStyle(
                    color: Colors.white,
                    fontWeight: FontWeight.bold,
                  ),
                )),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

【讨论】:

    猜你喜欢
    • 2019-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 2017-02-21
    • 1970-01-01
    相关资源
    最近更新 更多