Flutter 流程图布局

2020-08-11  本文已影响0人  fordG

重点需求在自适应高度, 不能设置高度, 线第一个和最后一个两头不能有###

一开始想不去获取高度,通过自适应来画出线来, 没找到办法, 有指导的麻烦代码教教我!


import 'package:bgbz/net/net.dart';
import 'package:flutter/material.dart';

typedef CallBackHeight = void Function(double value);
class HeightReporter extends StatelessWidget {
  final Widget child;
  CallBackHeight callBackHeight;

  HeightReporter({this.child});
  double _height;
  double getHeight() {
    return _height;
  }

  @override
  Widget build(BuildContext context) {
    Widget tmp = new GestureDetector(
      child: child,
      onTap: () {
        print('Height is ${context.size.height}');
      },
    );

    Net.delay(200, (){
      _height = context.size.height;
      if(callBackHeight != null){
        callBackHeight(context.size.height);
      }
    });
    return tmp;
  }
}

import 'package:bgbz/components/height_reporter.dart';
import 'package:bgbz/utils/style.dart';
import 'package:flutter/material.dart';
import 'package:bgbz/model/detail/log_info.dart';

enum FlowWidthStyle {
  all,
  top,
  bottom,
}

enum FlowWidthStatus{
  done,
  ready
}

class FlowWidth extends StatefulWidget {
  final LogInfo logInfo;
  final FlowWidthStyle flowStyle; //两端线都需要
  final FlowWidthStatus flowStatus; //状态

  FlowWidth({this.logInfo, this.flowStyle, this.flowStatus});
  @override
  _FlowWidthState createState() => _FlowWidthState();
}

class _FlowWidthState extends State<FlowWidth> {
  double defaultHeight = 10;
  double screenWidth;
  double circleWidth = 5;
  @override
  Widget build(BuildContext context) {
    screenWidth = MediaQuery.of(context).size.width;
    return Container(
      color: Colors.white,
      margin: EdgeInsets.only(top: 0, left: 15, right: 15),
      width: screenWidth - 30,
      child: Container(
        child: Row(
          children: [
            Container(
              width: 5,
              height: defaultHeight,
              child: Column(
                children: _childsWidget(),
              ),
            ),
            Container(
              padding: EdgeInsets.only(left: 10),
              width: 90,
              child: Text(
                  widget.logInfo.bWF_CurOperatorTime.replaceAll('-', '.'),
                  style: Style.cnNameTitle),
            ),
            _fitChild(widget.logInfo)
          ],
        ),
      ),
    );
  }

  Widget _fitChild(LogInfo logInfo) {
    HeightReporter heightReporter = HeightReporter(
        child: Container(
      width: screenWidth - 140,
      padding: EdgeInsets.only(left: 10, top: 10, bottom: 10),
      child: Text(logInfo.bWF_CurUsername + '\t' + logInfo.bWF_Opinion,
          style: Style.detailValueTitle),
    ));
    if(defaultHeight == 10){
      heightReporter.callBackHeight = (double height) {
        setState(() {
          defaultHeight = height;
        });
      };
    }

    return heightReporter;
  }

  Widget _line() {
    return Container(
      height: (defaultHeight - circleWidth) / 2,
      width: 2,
      color: Style.lineColor,
    );
  }

  Widget _circle() {
    return Container(
      height: 5,
      width: 5,
      decoration: BoxDecoration(
          color: widget.flowStatus == FlowWidthStatus.done ? Colors.black38 : Colors.blue, borderRadius: BorderRadius.circular(2.5)),
    );
  }

  List<Widget> _childsWidget() {
    if (widget.flowStyle == FlowWidthStyle.all) {
      return [_line(), _circle(), _line()];
    } else if (widget.flowStyle == FlowWidthStyle.top) {
      return [Padding(padding: EdgeInsets.only(top: (defaultHeight - circleWidth) / 2), child: _circle()), _line()];
    } else {
      return [_line(), _circle()];
    }
  }
}


Column

if((index - 1) == 0)
                return FlowWidth(logInfo: list[index - 1], flowStyle: FlowWidthStyle.top, flowStatus: FlowWidthStatus.ready);
              else if((index) == list.length)
                return FlowWidth(logInfo: list[index - 1], flowStyle: FlowWidthStyle.bottom, flowStatus: FlowWidthStatus.done);
              else
                return FlowWidth(logInfo: list[index - 1], flowStyle: FlowWidthStyle.all, flowStatus: FlowWidthStatus.done);
            }
上一篇 下一篇

猜你喜欢

热点阅读