Кнопка флаттера со значком и текстом выглядит странно - PullRequest
0 голосов
/ 01 июня 2018

Я пытаюсь создать приложение Flutter, которое имеет кнопку с текстом и значком в качестве метки, причем значок располагается справа от текста.Подход, описанный в этом посте , дает странно выглядящую кнопку, которая кажется расширенной до ширины приложения, см. Это изображение (ссылка, поскольку мне не разрешено прикреплять изображения):

Кнопка становится широкой при добавлении виджета «Строка» в качестве дочернего

Мне не ясно, какие виджеты макетов следует использовать для настройки кнопки «текст + изображение», которая должна быть отформатирована как кнопка простого текста.

Код для создания приведенного выше примера:

Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new Text(
              'You have pushed the button this many times:',
            ),
            new Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
            new RaisedButton(
              onPressed: _incrementCounter,
              child: new Row(
                children: <Widget>[
                  new Text("Button with text and icon!"),
                  new Icon(Icons.lightbulb_outline)
                ],
              ),
            ),
            new RaisedButton(
              onPressed: _incrementCounter,
              child: new Text("Button with only text")
              ),
          ],
        ),
      ),
      floatingActionButton: new FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: new Icon(Icons.add),
      ),
    );
  }
}

Ответы [ 2 ]

0 голосов
/ 27 сентября 2018

Вы можете просто использовать это RaisedButton.icon

Простое использование:

RaisedButton.icon(
   onPressed: () {},
   elevation: 2.0,
   shape: new RoundedRectangleBorder(
      borderRadius: new BorderRadius.circular(5.0),
   ),
   color: const Color(0xFFFFB822),
   icon: Icon(Icons.add_shopping_cart),
   label: Text("Add to Cart",
             style: TextStyle(
             fontWeight: FontWeight.w900,
    ),
  ),
)
0 голосов
/ 01 июня 2018

Свойство строки mainAxisSize: MainAxisSize.min добивается цели здесь.

@override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('Home'),
      ),
      body: new Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new RaisedButton(
              onPressed: () {},
              child: new Row(
                mainAxisAlignment: MainAxisAlignment.center,
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  new Text('Button with text and icon!'),
                  new Icon(Icons.lightbulb_outline),
                ],
              ),
            ),
            new RaisedButton(
              onPressed: () {},
              child: new Text('Button with only text'),
            )
          ],
        ),
      ),
    );
  }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...