2017-07-12 8 views
1

最初の画像でここに示すように、Flutterで子ビューの縮小を達成する方法を知っていますか?私は非常によく仕事を行うgridViewを使用しました。GridViewのように子ビューを縮小するには

2番目の画像では、私は行を使用しましたが、コンテナとコンストレインで試しましたが、うまく機能しませんでした。新しいコンテナ( マージン:CONST EdgeInsets.all(16.0)、

 child:new Row(
     mainAxisAlignment: MainAxisAlignment.spaceEvenly, 
     children: <Widget>[ 
      new Container(
      padding: const EdgeInsets.all(2.0), 
      child: new FoodTile(name: "Tile one", onPressed:(bool state){}), 
     ), 
      new Container(
      padding: const EdgeInsets.all(2.0), 
      child: new FoodTile(name: "Tile two", onPressed:(bool state){}), 
     ), 
      new Container(
      padding: const EdgeInsets.all(2.0), 
      child: new FoodTile(name: "Tile three", onPressed:(bool state){}), 
     ) 

     ], 
    ) 
    )); 
; D

GridView

return new GridView.count(
    crossAxisCount: 3, 
    padding: const EdgeInsets.all(16.0), 
    mainAxisSpacing: 4.0, 
    shrinkWrap: true, 
    crossAxisSpacing: 4.0, 

    children: widget.foodItems.map((FoodViewModel food){ 
     return new FoodTile(
      name: food.name, 
      icon: food.icon 
      , onPressed: (bool state) { 
       food.isSelected = state; 
       widget?.onFoodItemTaped(food, state); 
      }, 
     ); 
    }).toList(), 
); 

RowView ここで任意の制約なしに行のコードは、( 子を新しいSingleChildScrollViewを返す

Complete source code to the tile

答えて

1

FoodTileのインスタンスをFlexibleまたはExpandedにラップして、が柔軟なレイアウトモデルを適用し、均等に配置するようにする必要があります。

+0

Thxを。しかし、正方形を保つためにLayoutBuildに貼り付けなければなりませんでした。 (Container)( )制約:new BoxConstraints.tightFor(width:constraints.maxWidth、height:constraints.maxWidth)、 padding:const EdgeInsets():新しいLayoutBuilder(ビルダー:(ビルド:ビルドコンテキストコンテキスト、BoxConstraints制約){ 新しいコンテナを返します。 .}))、) – swissonid

+0

Thxそれを行ったThx:.all(2.0)、 子:新しいFoodTile(名前: "hallo 2"、onPressed :(ブール状態){})、 )しかし、正方形を保つためにLayoutBuildに貼り付けなければなりませんでした。 – swissonid

+0

AspectRatioがそのためにうまくいくかもしれません –

0

最終結果がどのように見えるのか疑問に思っているだけです。私はまだ "LayoutBuilder-Solution"に少し不満ですが、それは私のために正しく知っています。それをしなかった

return new SingleChildScrollView(
    child: new Container(
     margin: const EdgeInsets.all(16.0), 

     child: new Row(
     mainAxisAlignment: MainAxisAlignment.spaceEvenly, 
     mainAxisSize: MainAxisSize.min, 
     children: <Widget>[ 
      new Expanded(child: new LayoutBuilder(builder: (BuildContext context, BoxConstraints constraints){ 
      return new Container(
       constraints: new BoxConstraints.tightFor(width: constraints.maxWidth, height: constraints.maxWidth), 
       padding: const EdgeInsets.all(2.0), 
       child: new FoodTile(name:"hallo",onPressed: (bool state){}), 
      ); 
      })), 
      new Expanded(child: new LayoutBuilder(builder: (BuildContext context, BoxConstraints constraints){ 
      return new Container(
       constraints: new BoxConstraints.tightFor(width: constraints.maxWidth, height: constraints.maxWidth), 
       padding: const EdgeInsets.all(2.0), 
       child: new FoodTile(name:"hallo 2",onPressed: (bool state){}), 
      ); 
      })), 
      new Expanded(child: new LayoutBuilder(builder: (BuildContext context, BoxConstraints constraints){ 
      return new Container(
       constraints: new BoxConstraints.tightFor(width: constraints.maxWidth, height: constraints.maxWidth), 
       padding: const EdgeInsets.all(2.0), 
       child: new FoodTile(name:"hallo 3",onPressed: (bool state){}), 
      ); 
      })), 
     ], 
    ), 

enter image description here

+0

拡張されたコンテナを関数にリファクタリングすることができます。何かが好きです:https://gist.github.com/sethladd/df690b2eb5923a5699aabbdbb869b1b9#file-updated2-dart –

関連する問題