代码之家  ›  专栏  ›  技术社区  ›  atreeon

如何在flatter中收缩和扩展Wrap小部件?

  •  0
  • atreeon  · 技术社区  · 8 年前

    我试过FittedBox(scaledDown),它减小了大小,但是小部件根本没有包装(!)所有的孩子都在同一条线上。

    这是默认行为(如果包装小部件是在具有特定大小的容器中定义的)。 enter image description here

    这是密码。。。

      Widget _build(BuildContext context) {
        var words = ["a", "abc", "de", "ef", "g", "i", "is", "s", "th"];
    
        return new Scaffold(
          body: Column(children: <Widget>[
            Text('height 100'),
            Container(
              color: Colors.red,
              height: 100.0,
              child: Wrap(
                alignment: WrapAlignment.spaceEvenly,
                children: _buildWordButtons(words),
              ),
            ),
            Text('height 50'),
            Container(
              color: Colors.green,
              height: 50.0,
              child: Wrap(
                alignment: WrapAlignment.spaceEvenly,
                children: _buildWordButtons(words),
              ),
            ),
            Text('FittedBox, scaleDown, height 200'),
            Container(
              color: Colors.blue,
              height: 200.0,
              child: FittedBox(
                fit: BoxFit.scaleDown,
                child: Wrap(
                  alignment: WrapAlignment.spaceEvenly,
                  children: _buildWordButtons(words),
                ),
              ),
            ),
          ]),
        );
      }
    
      List<Widget> _buildWordButtons(List<String> words) {
        var buttons = List<Widget>();
    
        for (var word in words) {
          buttons.add(Container(
            decoration: new BoxDecoration(border: new Border.all(color: Colors.black)),
            child: Text(
              word,
              style: TextStyle(fontSize: 50.0),
            ),
            margin: EdgeInsets.all(5.0),
          ));
        }
    
        return buttons;
      }
    
    0 回复  |  直到 8 年前
        1
  •  0
  •   Omatt    5 年前

    LayoutBuilder 小装置。使用这个小部件应该有助于其子部件确定其父部件的约束。既然你提到有一个宽敞的 Container .

    这是一个样本。我已经修改了提供的代码段。

    import 'package:flutter/material.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
            visualDensity: VisualDensity.adaptivePlatformDensity,
          ),
          home: MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key, this.title}) : super(key: key);
    
      final String title;
    
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      final words = ["a", "abc", "de", "ef", "g", "i", "is", "s", "th"];
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: Column(children: <Widget>[
            Text('height 100'),
            Container(
              color: Colors.red,
              height: 100.0,
              child: adaptiveText(),
            ),
            Text('height 50'),
            Container(
              color: Colors.green,
              height: 50.0,
              child: adaptiveText(),
            ),
            Text('height 200'),
            Container(
              alignment: Alignment.center,
              color: Colors.blue,
              height: 200.0,
              child: adaptiveText(),
            ),
          ]),
        );
      }
    
      adaptiveText() {
        return LayoutBuilder(
          builder: (context, constraints) {
            debugPrint('constraint maxHeight: ${constraints.maxHeight}');
            // adapt Text widget if parent widget size is <= 100
            if (constraints.maxHeight <= 100)
              return FittedBox(
                fit: BoxFit.scaleDown,
                child: Wrap(
                  alignment: WrapAlignment.spaceEvenly,
                  children: _buildWordButtons(words),
                ),
              );
            else
              return Wrap(
                alignment: WrapAlignment.spaceEvenly,
                children: _buildWordButtons(words),
              );
          },
        );
      }
    
      List<Widget> _buildWordButtons(List<String> words) {
        var buttons = List<Widget>();
    
        for (var word in words) {
          buttons.add(Container(
            decoration:
                new BoxDecoration(border: new Border.all(color: Colors.black)),
            child: Text(
              word,
              style: TextStyle(fontSize: 50.0),
              maxLines: 3,
            ),
            margin: EdgeInsets.all(5.0),
          ));
        }
    
        return buttons;
      }
    }
    

    演示