Flutter小部件的大小取决于另一个

时间:2019-03-08 15:17:05

标签: build flutter widget size

我目前正在尝试制作一个自定义的窗口小部件。 元素列表右侧的一个简单的“ A至Z滚动条”,用于跳转到索引。 我的问题是,实际上我需要使用列表窗口小部件的大小来确定字母的字体大小。

为说明: enter image description here

我将列表添加为自定义类的子级,以使其达到上下文的高度。我试图在“ build”方法中使用 context.size.heigth 来获取大小,但是由于未构建窗口小部件,因此出现了错误。然后,我尝试执行一个异步方法,就像我在这里读到的:Stack overflow post

唯一能正确获取 context.size.height 的位置是我的手势检测器的 onVerticalDragUpdate

有我的实际代码:

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {  
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: new AtoZSlider(child: _builList()),
    );
  }
}

Widget _builList() { //test list
  return ListView.builder(
    padding: EdgeInsets.all(8.0),
    itemExtent: 20.0,
    itemCount: 1000,
    itemBuilder: (BuildContext context, int index) {
      return Text('entry $index');
    },
  );
}

class AtoZSlider extends StatefulWidget {
  final Widget child;

  AtoZSlider({this.child});

  @override
  _AtoZSlider createState() => new _AtoZSlider();
}

class _AtoZSlider extends State<AtoZSlider> {
  double _offsetContainer;
  double _heightscroller;
  bool _isHeightSetup;
  var _text;
  var _alphabet;

  @override
  void initState() {
    super.initState();
    _offsetContainer = 0.0;
    _isHeightSetup = false;
    _heightscroller = 14.0; //default value that i want to calculate with context size
    _alphabet = [
      '#',
      'A',
      'B',
      'C',
      'D',
      'E',
      'F',
      'G',
      'H',
      'I',
      'J',
      'K',
      'L',
      'M',
      'N',
      'O',
      'P',
      'Q',
      'R',
      'S',
      'T',
      'U',
      'V',
      'W',
      'X',
      'Y',
      'Z'
    ];
    _text = _alphabet[0];
  }

  @override
  Widget build(BuildContext context) {
    return  Stack(alignment: Alignment.topRight, children: [
      widget.child,
      GestureDetector(
        child: Container(
            child: Text(_text, style: new TextStyle(fontSize: _heightscroller),),
            height: _heightscroller,
            margin: EdgeInsets.only(top: _offsetContainer)),
        onVerticalDragUpdate: (DragUpdateDetails details) {
          setState(() {

            if ((_offsetContainer + details.delta.dy) >= 0 &&
                (_offsetContainer + details.delta.dy) <=
                    (context.size.height - _heightscroller)) { // to move my scroller on the vertical axis and not out of band using context.size.height
              _offsetContainer += details.delta.dy; 

            }

          });
        },
      )
    ]);
  }

}

是否有某种方法可以根据“列表大小高度”来初始化我的“小部件文本字体大小”?

非常感谢您提供任何帮助。

2 个答案:

答案 0 :(得分:2)

小部件不能取决于另一个小部件的大小。 另一方面,它可能取决于约束

为此,您可以使用LayoutBuilder获取约束并相应地构建窗口小部件:

Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(title: Text("Hello World")),
    body: LayoutBuilder(
      builder: (context, constraints) {
        if (constraints.biggest.width > 100) {
          return Text('Hello');
        }
        return Container();
      },
    ),
  );
}

答案 1 :(得分:0)

您可以通过将 $User_data = User::where('email', '=', $email) ->leftJoin('secutitequestion', function($join) { $join->on('users.id', '=', 'secutitequestion.user_id'); }) ->select('secutitequestion.securite_question1' ,'secutitequestion.securite_question2' ,'secutitequestion.securite_question3' ,'secutitequestion.user_id') ->first(); //<-- this is the change 的{​​{1}}减去整个屏幕Listview来推断height的大小。您可以通过在分配之前进行定义来获得AppBar的高度:

height

那么您的AppBar身高将是:

   class _MyHomePageState extends State<MyHomePage> {

   AppBar myBar = AppBar(title: Text(widget.title),);

   @override
   Widget build(BuildContext context) {  
      return Scaffold(
        appBar: myBar,
      )

每个字母的大小为:

Listview

您应该使字母的尺寸小于上述尺寸,以在字母之间留出一定的间距。