如何在Flutter中的“升高”按钮中将图标左对齐和文本中间对齐?

时间:2019-12-25 14:04:42

标签: flutter dart

我有一个带有图标和文本的按钮,两个按钮都在中间对齐,我希望图标在左边,按钮的文本在中间。有人可以调查一下并提供帮助吗?我该怎么做。

这是我的代码:

Widget _signInButton() {


return ButtonTheme(
  height: 30,

 minWidth: 250,

  child: RaisedButton(
    color: Colors.white,

    splashColor: Colors.grey,
    onPressed: () {
      signInWithGoogle().whenComplete(() {
        Navigator.of(context).push(
          MaterialPageRoute(
            builder: (context) {
              return FirstScreen();
            },
          ),
        );
      });
    },
    shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(40)),
    highlightElevation: 0,
   // borderSide: BorderSide(color: Colors.grey),
    child: Padding(
      padding: const EdgeInsets.fromLTRB(0, 10, 0, 10),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Image(image: AssetImage("Assets/google_logo.png"), height: 35.0),
          Expanded(
            flex: 1,
            child: Padding(
              padding: const EdgeInsets.only(left: 10),
              child: Text(
                'Sign in with Google',
                style: TextStyle(
                  fontSize: 15,
                  color: Colors.black87,

1 个答案:

答案 0 :(得分:0)

您可以在下面复制粘贴运行完整代码
您可以将Expanded flex 1用于图像,将Expanded flex 2用于文本

代码段

Expanded(
      flex: 1,
      child: Image(
          image: AssetImage("Assets/google_logo.png"),
          height: 35.0),
                    ),
Expanded(
     flex: 2,
        child: Padding(

工作演示

enter image description here

完整代码

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      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> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            _signInButton(),
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

Widget _signInButton() {
  return ButtonTheme(
      height: 30,
      minWidth: 250,
      child: RaisedButton(
          color: Colors.white,
          splashColor: Colors.grey,
          onPressed: () {
            /*signInWithGoogle().whenComplete(() {
      Navigator.of(context).push(
        MaterialPageRoute(
          builder: (context) {
            return FirstScreen();
          },
        ),
      );
    });*/
          },
          shape:
              RoundedRectangleBorder(borderRadius: BorderRadius.circular(40)),
          highlightElevation: 0,
          // borderSide: BorderSide(color: Colors.grey),
          child: Padding(
              padding: const EdgeInsets.fromLTRB(0, 10, 0, 10),
              child: Row(
                  mainAxisSize: MainAxisSize.min,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Expanded(
                      flex: 1,
                      child: Image(
                          image: AssetImage("Assets/google_logo.png"),
                          height: 35.0),
                    ),
                    Expanded(
                        flex: 2,
                        child: Padding(
                            padding: const EdgeInsets.only(left: 10),
                            child: Text('Sign in with Google',
                                style: TextStyle(
                                    fontSize: 15, color: Colors.black87))))
                  ]))));
}