Text widget font not appearing correctly - flutter

I've created a separate page with a StatefulWidget inside my Flutter app, and it has a Text widget inside of it.
However, when testing my app, the text does not render as intended - instead it shows up in a weird font with yellow underlining.
Here's my code:
import 'package:flutter/material.dart';
class ListsPage extends StatefulWidget {
#override
State<StatefulWidget> createState() {
return _ListsPageState();
}
}
class _ListsPageState extends State {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Text('Log in page'),
);
}
}
Results image

Please remove Material Widget just return Text Widget Only, and please try to use the Stateless widget for better performance. Here's the example code
import 'package:flutter/material.dart';
import 'package:show_case/diementions/text_size.dart';
class TextWidget extends StatelessWidget {
const TextWidget({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Text(
'Login Page',
style: kLabelStyle,
);
}
}

This is because of the app styling found in a widget higher in the tree. The text widget searches up the tree for the app default text style or one in another widget. If you wrap the Text widget in a Scaffold, that will include different styling.
See the her comment in the following code for where to try setting the default style:
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return ProviderScope(
child: MaterialApp(
title: 'Flutter Demo',
theme: ThemeData( //HERE
primarySwatch: Colors.blue,
),
home: const HomeScreen(),
),
);
}
}
Also try wrapping the Text widget like this:
DefaultTextStyle(
style: TextStyle(decoration: TextDecoration.none),
child: Text('Log in page'),
)

Set style in textwidget
Text("", style: TextStyle(decoration: TextDecoration.none),)

Related

The constructor being called isn't a const constructor

I am getting an error for creating a Column Widget saying 'The constructor being called isn't a const constructor.' Having a tough time creating a Column Widget itself,
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'My First Flutter App',
theme: ThemeData(
scaffoldBackgroundColor: Colors.white,
),
home: const WelcomeScreen());
}
}
class WelcomeScreen extends StatelessWidget {
const WelcomeScreen({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return const Scaffold(
body: Column(
children: <Widget>[
Text("Login"),
]
)
);
}}
Just remove const from Scaffold widget
Column is a non-const constructor
Column({
Key? key,
You can use const where everything is available as compile time constant. In this case, Column breaks the condition. You can use const before children, it is possible with Text for now
class WelcomeScreen extends StatelessWidget {
const WelcomeScreen({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(children: const <Widget>[
Text("Login"),
]),
);
}
}
I will encourage you to lean more about const, you can check this question and on guides.
The error is at the level of the scaffold widget.
Just remove the const before scaffold. Then add const keyword before the children of your column widget.
That is how your code is supposed to be
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'My First Flutter App',
theme: ThemeData(
scaffoldBackgroundColor: Colors.white,
),
home: const WelcomeScreen());
}
}
class WelcomeScreen extends StatelessWidget {
const WelcomeScreen({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: const <Widget>[
Text("Login"),
...
]
)
);
}}
Remove the const from the Scaffold widget and it would work fine. But there will be another warning telling that the Column widget does not have const.
To fix that you can either put const in front of <Widget> or put const in front of the children of columns

changing theme through button input in Flutter

I made a button, and when that button is pressed, I want to change the color of the theme.
I am trying to modify the color with the value received from the button, but it does not work.
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
String themeColors=context.watch<DisplayList>().themeColor;
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
primaryColor: Colors.${themeColors}, //How do I fix this part?
),
Or is there another way to change the color?
themeColors variable already contains a string of the color to be changed.
You can't actually have a syntax like this one on Flutter: Colors.${themeColors}
To handle multiples themes, you need to create multiples ThemeData and switch them with a ValueNotifier.
I suggest you to use an already made community package like theme_provider which will help you to switch between themes very easily.
You will have to convert your widget to Stateful and use the setState method
class XYZ extends StatefulWidget {
const XYZ({Key? key}) : super(key: key);
#override
_XYZState createState() => _XYZState();
}
class _XYZState extends State<XYZ> {
var myAppBarThemeColor = Colors.red;
#override
Widget build(BuildContext context) {
print(myAppBarThemeColor);
return MaterialApp(
theme: ThemeData(appBarTheme: AppBarTheme(backgroundColor: myAppBarThemeColor)),
home: Scaffold(
appBar: AppBar(
title: Text('Hello'),
),
body: Center(
child: TextButton(
onPressed: () => setState(() => myAppBarThemeColor = Colors.green),
child: Text('Change AppBar Color'),
),
),
),
);
}
}
You can use findAncestorStateOfType to manage the state of the root widget.
class App extends StatefulWidget {
const App({Key? key}) : super(key: key);
static _AppState? of(BuildContext context) => context.findAncestorStateOfType<_AppState>();
#override
_AppState createState() => _AppState();
}
class _AppState extends State<App> {
late bool isDarkMode;
late ThemeModeStorage storage;
void toggleDarkMode() {
setState(() {
isDarkMode = !isDarkMode;
});
storage.writeBool(value: isDarkMode);
}
...
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeConfig(isDarkMode).themeData,
home: HomeScreen(),
);
}
}
So you can call this everywhere in your app.
App.of(context)?.toggleDarkMode();

How to get rid of address bar and bottom bar in Flutter 'WebBrowser' Plugin?

My App is a Flutter WEB app.
This is the plugin I am using to display some HTML coming from the server:
https://pub.dev/packages/web_browser
Here is the code I am using:
WebBrowser(
initialUrl: 'https://${Provider.of<CP>(context).getPaymentURL()+Provider.of<CP>(context).getPaymentPATH()}?O=${Provider.of<CP>(context).getOrder().orderUID}&Ro=${Provider.of<CP>(context).getOrder().res_key}',
),
My screen looks like below:
I would like to rid of the address bar and bottom bar but not sure how. Can someone help please?
You can copy paste run full code below
You can use attribute interactionSettings and set WebBrowserInteractionSettings's topBar and bottomBar to SizedBox.shrink()
code snippet
return WebBrowser(
initialUrl: 'https://dart.dev/',
interactionSettings: WebBrowserInteractionSettings(
topBar: SizedBox.shrink(), bottomBar: SizedBox.shrink()));
working demo
full code
import 'package:flutter/material.dart';
import 'package:web_browser/web_browser.dart';
class MyWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return WebBrowser(
initialUrl: 'https://dart.dev/',
interactionSettings: WebBrowserInteractionSettings(
topBar: SizedBox.shrink(), bottomBar: SizedBox.shrink()));
}
}
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(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: AppBar(
title: Text("Demo"),
),
body: MyWidget()),
);
}
}

How to render widgets under notch in Flutter?

By default, Flutter respects the SafeArea around the notch.
After SystemChrome.setEnabledSystemUIOverlays([]) (to remove time, overlay etc.) then setting SafeArea to false on all sides, there is still a blank area around the notch:
How can I make the app render without considering the notch? i.e. the AppBar should display under the black notch area.
Code:
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
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> {
#override
Widget build(BuildContext context) {
SystemChrome.setEnabledSystemUIOverlays([]);
return SafeArea(
top: false,
bottom: false,
left: false,
right: false,
child: Scaffold(
resizeToAvoidBottomInset: false,
appBar: AppBar(title: Text(widget.title)),
body: Center(
child: Text(
'hello stack overflow',
),
),
),
);
}
}

Stateful Widget child is not updated

There are two stateful widgets.
The state of MyHomePage contains the counter.
The MyHomePage wraps content in a second stateful widget SubPage.
The SubPage has a child widget with data from the MyHomePage.
To clarify the problem, the first textwidget which is inside of the SubPage child doesn't update when the counter changes.
The textwidget outside of the SubPage increments as expected.
What do we have to do if we want the content of the inner stateful widget updated?
We have to use a stateful widget there. In the real application this widget has a real use-case.
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
#override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: Column(
children: <Widget>[
SubPage(
child: 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 Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
floatingActionButton: new FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: new Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
class SubPage extends StatefulWidget {
SubPage({Key key, this.child}) : super(key: key);
final Widget child;
#override
SubPageState createState() => new SubPageState(child);
}
class SubPageState extends State<SubPage> {
final Widget child;
SubPageState(this.child);
#override
Widget build(BuildContext context) {
print("subpage build");
return this.child;
}
}
You don't have to set child as field of state. Actually it is cause of this bug. Here working code
class SubPage extends StatefulWidget {
SubPage({Key key, this.child}) : super(key: key);
final Widget child;
#override
SubPageState createState() => new SubPageState();
}
class SubPageState extends State<SubPage> {
#override
Widget build(BuildContext context) {
return widget.child;
}
}