how can I give color for the price in title? I have long text for the product، I need to give Different color for the price only, like this image
I used RichText but its not good
this below code is sample for RichText widget :)
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: Scaffold(
body: Center(
child: RichText(
text: const TextSpan(
text: 'خصومات تصل علی',
style: TextStyle(color: Colors.black),
children: <TextSpan>[
TextSpan(
text: ' 60% ',
style: TextStyle(fontWeight: FontWeight.bold,color: Colors.red)),
TextSpan(
text: ' علی ',
style: TextStyle(color: Colors.black)),
],
),
),
),
),
);
}
}
Related
I have a list of text span like this
var spans = [
TextSpan(text: content, style: style),
TextSpan(text: content, style: style)
//and 100 more
]
I am loading this inside SingleChildScrollView like this.
SingleChildScrollView(
controller: scrollController,
child: Text.rich(
TextSpan(children: spans),
),
)
Now My question is how can I shift focus between the TextSpan list?. Say I want to load TextSpan at position 10 from spans list at top of screen without manually scrolling.
You could add a WidgetSpan next to each of your TextSpan, so you can assign it a key property which would be a GlobalKey so you can access its BuildContext to call the method Scrollable.ensureVisible().
Here's a code sample inspired by an answer to a similar question:
class ScrollToTextSpanPage extends StatefulWidget {
const ScrollToTextSpanPage({Key? key}) : super(key: key);
#override
State<ScrollToTextSpanPage> createState() => _ScrollToTextSpanPageState();
}
class _ScrollToTextSpanPageState extends State<ScrollToTextSpanPage> {
final _keys = List<GlobalKey>.generate(
_kTextBlocks.length,
(_) => GlobalKey(),
);
void _goToSpan(int spanIndex) {
Scrollable.ensureVisible(
_keys[spanIndex].currentContext!,
alignment: 0.2,
duration: const Duration(milliseconds: 300),
);
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Scroll to TextSpan'),
),
floatingActionButton: FloatingActionButton.extended(
onPressed: () => _goToSpan(8),
label: const Text('Go to span 8'),
icon: const Icon(Icons.navigate_next),
),
body: SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: Text.rich(
TextSpan(
children: [
for (int i = 0; i < _kTextBlocks.length; i++) ...[
WidgetSpan(child: SizedBox(key: _keys[i])),
TextSpan(
text: 'Span $i\n',
style: const TextStyle(fontWeight: FontWeight.bold),
children: [
TextSpan(
text: '${_kTextBlocks[i]}\n\n',
style: const TextStyle(fontWeight: FontWeight.normal),
),
],
),
],
],
),
),
),
);
}
}
Try the sample on DartPad
This question already has answers here:
The AppBarDesign can't be assigned to the parameter type 'PreferredSizeWidget'
(6 answers)
Closed 1 year ago.
Once I tried to add appBar to the feed page it is raising error.
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:wecare_u/constants/Constantcolors.dart';
class FeedHelpers with ChangeNotifier {
ConstantColors constantColors = ConstantColors();
Widget appBar(BuildContext context) {
return AppBar(
backgroundColor: constantColors.darkColor,
centerTitle: true,
actions: [
IconButton(
icon: Icon(Icons.camera_enhance_rounded,
color: constantColors.greenColor),
onPressed: () {})
],
title: RichText(
text: TextSpan(
text: 'Social ',
style: TextStyle(
color: constantColors.whiteColor,
fontWeight: FontWeight.bold,
fontSize: 20.0,
),
children: <TextSpan>[
TextSpan(
text: 'Feed',
style: TextStyle(
color: constantColors.blueColor,
fontWeight: FontWeight.bold,
fontSize: 20.0,
))
]),
),
);
}
}
Above is my Feed_helpers.dart file which contains the code for the page.
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:wecare_u/Designs/Feed_helpers.dart';
class Feed extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: Provider.of<FeedHelpers>(context, listen: false).appBar(context),
);}}
This is my feed.dart file, When I tried to add appBar in this page it is raising error as
'The argument type 'Widget' can't be assigned to the parameter type 'PreferredSizeWidget?'.'
Try to wrap AppBar widget with PreferredSize Widget.
PreferredSize(
preferredSize: Size.fromHeight(20.0), // height of appbar
child: AppBar(
..........
),
),
There's a syntax error at "Center(". what could be wrong?
Here's the message i'm receiving: "Creates a widget that centers its child."
import 'package:flutter/material.dart';
class SplashPage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
Center(
child: Text(
'n',
style: TextStyle(
color: Colors.black,
fontSize: 90,
fontFamily: 'Aclonica',
),
),
),
);
}
}
You should add "body:" before "Center(", because Scaffold doesn't have a positional parameters
The Scaffold takes the main widget in a parameter called body. So you should pass the Center widget in the body parameter of the Scaffold.
So something like,
Scaffold(
backgroundColor: Colors.white,
body: Center(
...
)
)
Try This:
import 'package:flutter/material.dart';
class SplashPage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
child: Column(
mainAxisAlignment : MainAxisAlignment.center,
crossAxisAlignment : CrossAxisAlignment.center,
children : [
Text(
'n',
style: TextStyle(
color: Colors.black,
fontSize: 90,
fontFamily: 'Aclonica',
),])
),
);
}
}
Try This:
import 'package:flutter/material.dart';
class SplashPage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Column(
mainAxisAlignment : MainAxisAlignment.center,
crossAxisAlignment : CrossAxisAlignment.center,
children : [
Text(
'n',
style: TextStyle(
color: Colors.black,
fontSize: 90,
fontFamily: 'Aclonica',
),])
),
);
}
}
When using a widget, it's best to read through it's documentation once. According to the Scaffold's documentation here, the content of the Scaffold should be placed within the body property, which is a positional parameter (means you need to state the param when initializing the Scaffold):
import 'package:flutter/material.dart';
class SplashPage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Center( // Just need to add the `body` property here
child: Text(
'n',
style: TextStyle(
color: Colors.black,
fontSize: 90,
fontFamily: 'Aclonica',
),
),
),
);
}
}
I didn't understand what the error is. I am returning a Scaffold widget. There are no syntax errors detected by visual studio code. I am getting an error while running the app.
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("here is my text")),
body: Text(
'No, we need bold strokes. We need this plan.',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.blue.withOpacity(0.7),
height: 1,
fontSize: 20,
decoration: TextDecoration.underline,
decorationColor: Colors.green,
decorationStyle: TextDecorationStyle.wavy,
backgroundColor: Colors.red),
),
);
}
}
If I wrap the scaffold widget in the material app it is working. Can anyone explain to me the reason?
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text("here is my text")),
body: Text(
'No, we need bold strokes. We need this plan.',
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.blue.withOpacity(0.7),
height: 1,
fontSize: 20,
decoration: TextDecoration.underline,
decorationColor: Colors.green,
decorationStyle: TextDecorationStyle.wavy,
backgroundColor: Colors.red),
),
),
);
}
}
The top most widget must be wrapped in a MaterialApp widget. This is what generates the context chain that it's looking for.
Media Query is a property of MaterialApp so if you don't use the MaterialApp, you can't access media query. The proper way is to use it in your main class.
I have cleaned Flutter example from Scafflod and button and was expecting I get the same content, but without title bar and button:
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'My app title',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
#override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
#override
Widget build(BuildContext context) {
return 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,
),
],
),
);
}
}
Unfortunately, what I got is not fit any gates:
What is it? How it happen to turn so big? How to fix?
You can wrap the text inside "Material" widget. Also, using "style:" parameter fixes the problem.
Material(
color: Colors.transparent,
child: Text(
"Your Text Here",
style: TextStyle(
fontFamily: 'custom font', // remove this if don't have custom font
fontSize: 20.0, // text size
color: Colors.white, // text color
),
),
)
Scaffold is setting the basic material design visual layout structure, which much include default text styles, etc. If you don't want to use Scaffold for whatever reason, then you can apply the relevant text styles and colours from the current theme using Theme.of(context). For example:
Text(
'You have pushed the button this many times:',
style: Theme.of(context).primaryTextTheme.headline,
textAlign: TextAlign.center,
),
Text(
'Sign in / Sign up with',
style: TextStyle(
fontFamily: appFontFamily,
fontSize: 20,
decoration: TextDecoration.none,////set decoration to .none
fontWeight: FontWeight.bold,
color: defaultTitleColor,
),
)