【问题标题】:I want the text and icon together in a line under one widget in body我希望文本和图标在正文中的一个小部件下排成一行
【发布时间】:2023-02-10 15:50:26
【问题描述】:

我希望文本和图标在正文中的一个小部件下排成一行。我试过了,但出现以下错误

位置参数太多,预期为 0,但收到了 1

谁能帮我这个?提前致谢!

void main() => runApp(const Myapp());

class Myapp extends StatefulWidget {
  const myapp({Key? key}) : super(key: key);
  _MyappState createState() => _MyappState();
}

class _MyappState extends State<Myapp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Padding(
          padding: EdgeInsets.only(top: 30),

          child: Stack(
            alignment: AlignmentDirectional.center,

            children: <Widget>[
              /** Positioned WIdget **/

              Positioned(
                top: 0.0,
                child: Image.asset(
                  'images/logo.png',
                  height: 150,
                  width: 150,
                ),
              ),
              //Positioned

              /** Positioned WIdget **/

              //Positioned
            ], //<Widget>[]
          ), //Stack
        ),

        Text.rich(
  TextSpan(
    children: [
      TextSpan(text: 'Click'),
      WidgetSpan(child: Icon(Icons.add)),
      TextSpan(text: 'to add'),
    ],
  ),
)
        
      ),
    );
  }
}

【问题讨论】:

  • 您在哪一行/小部件中遇到错误?

标签: flutter flutter-web-browser


【解决方案1】:

首先,错误“位置参数太多,预期为 0 但收到了 1”,这是因为 RichText 的位置在 Stack Widget 子项之外。 (方括号 []。)

要获得问题中提到的所需输出,请使用代码 cmets 中指定的行和附加代码。

void main() => runApp(const Myapp());

class Myapp extends StatefulWidget {
  const Myapp({Key? key}) : super(key: key);
  @override
  _MyappState createState() => _MyappState();
}

class _MyappState extends State<Myapp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Padding(
          padding: const EdgeInsets.only(top: 30),

          child: Stack(
            alignment: AlignmentDirectional.center,

            children: const <Widget>[
              /** Positioned WIdget **/
              // Positioned(
              //   top: 0.0,
              //   child: Image.asset(
              //     'images/logo.png',
              //     height: 150,
              //     width: 150,
              //   ),
              // ),

              /** Positioned WIdget **/
              
              /// This is where the RichTextWidget should be
              Text.rich(
                TextSpan(
                  children: [
                    TextSpan(text: 'Click'),
                    WidgetSpan(child: Icon(Icons.add)),
                    TextSpan(text: 'to add'),
                  ],
                ),
              ),
              /// Additionally, use a Row([Text,Icon,Text]) and use
              /// crossAxisAlignment Property to align the children
            ], //<Widget>[]
          ), //Stack
        ),
        /// You placed the below RickText outside the Stack, it should be above in the Widget[] of the stack.
        //  Text.rich(
        //   TextSpan(
        //     children: [
        //       TextSpan(text: 'Click'),
        //       WidgetSpan(child: Icon(Icons.add)),
        //       TextSpan(text: 'to add'),
        //       ],
        //     ),
        //   )
      ),
    );
  }
}

【讨论】:

  • 如果我在“堆栈”或“填充”中尝试文本代码,文本将放置在图像上。但我想要图片下方的文字。你能帮我吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-26
  • 2015-03-26
  • 2013-11-25
  • 2018-10-05
  • 1970-01-01
  • 2020-03-08
相关资源
最近更新 更多