I want the text and icon together in a line under one widget in body. I tried and I got the following error
Too many positional arguments, 0 expected but 1 received
Can Any one help me with this? Thanks in advance!
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'),
],
),
)
),
);
}
}
3
Answers
First, the error "Too many positional arguments, 0 expected but 1 received", is due to the fact that the placement of RichText is outside the Stack Widget children. (The square brackets [].)
To achieve the desired output as you mentioned in the question, use a row specified in code comments with the attached code.
If you want to place a button with title and icon then you can use
TextButton.icon
instead of putting it inside a row or putting insideRichText
.Here’s the button example:
Then you can put it inside a stack and assign a background image in it.
You can use
Positioned.fill
and then try modifying the parameters until it matches your requirment.