簡體   English   中英

flutter中的動態進度條

[英]dynamic progress bar in flutter

我想在 flutter 中創建一個進度條,就像這樣在此處輸入圖像描述

我從后端獲取一周的數據,但我必須以這種方式創建它。 周或調查的圖標應該是可觸摸的。

提前致謝。

還有很多事情要改變,但我認為一開始就足夠了

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const HomePage(),
    );
  }
}

class HomePage extends StatelessWidget {
  const HomePage({super.key});

  @override
  Widget build(BuildContext context) {
    List<String> points = [
      'Survey',
      'Week 1',
      'Week 2',
      'Week 3',
      'Week 4',
    ];

    var lineWidth =
        MediaQuery.of(context).size.width - 16.0; // screen width - 2 * padding
    var space = lineWidth / points.length; //space between dots
    var currentSteps = 3; // <---- change this one

    return Scaffold(
      body: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Center(
          child: SizedBox(
            height: 60.0,
            child: Stack(
              children: [
                //grey line
                Positioned(
                  top: 15,
                  left: 0,
                  right: 0,
                  child: Container(
                    height: 2.0,
                    width: double.infinity,
                    color: Colors.grey,
                  ),
                ),
                //red line
                Positioned(
                  top: 15,
                  left: 0,
                  child: Container(
                    height: 2.0,
                    width: space * (currentSteps - 1) + space / 2,
                    color: Colors.orange,
                  ),
                ),
                //circles
                Row(
                  children: points
                      .asMap()
                      .map((i, point) => MapEntry(
                            i,
                            SizedBox(
                              width: space,
                              child: Column(
                                children: [
                                  Stack(
                                    children: [
                                      Container(
                                        height: 30.0,
                                        width: 30.0,
                                        decoration: BoxDecoration(
                                          shape: BoxShape.circle,
                                          border: Border.all(
                                            width: 1.5,
                                            color: i == currentSteps - 1
                                                ? Colors.orange
                                                : Colors.transparent,
                                          ),
                                        ),
                                        child: Center(
                                          child: Container(
                                            height: 20.0,
                                            width: 20.0,
                                            decoration: BoxDecoration(
                                              shape: BoxShape.circle,
                                              color: i < currentSteps
                                                  ? Colors.orange
                                                  : Colors.grey,
                                            ),
                                          ),
                                        ),
                                      ),
                                      if (i < currentSteps - 1)
                                        const SizedBox(
                                          height: 30.0,
                                          width: 30.0,
                                          child: Center(
                                            child: Icon(
                                              Icons.check,
                                              size: 16.0,
                                              color: Colors.white,
                                            ),
                                          ),
                                        ),
                                    ],
                                  ),
                                  const SizedBox(height: 4.0),
                                  Text(
                                    point,
                                    textAlign: TextAlign.left,
                                    style: TextStyle(
                                      color: i < currentSteps
                                          ? Colors.orange
                                          : Colors.grey,
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          ))
                      .values
                      .toList(),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

您可以嘗試使用type: StepperType.horizontal的基本Stepper小部件。 至於你喜歡的用戶界面im_stepper package。如果不能滿足你可以用Stack和Row創建自定義小部件。

嘗試 flutter 的基本Stepper小部件。如果它不能正常工作,那么您需要創建自定義小部件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM