How to set Flutter app theme as to dark by default? - android

I've created a simple login UI in flutter, but I don't know how to make the overall theme of the app as dark. What I mean is that in the future, if I add more functionality to the app, it should all be in the dark theme. Is there any way to do that?
I've used a separate dart file (login.dart) and all the widgets used in my login UI are in this file. I've set the ThemeData as dark in the main dart file (main.dart), but the app is still running in light theme.
Here's my code:
main.dart
import 'package:flutter/material.dart';
import 'package:bidder_login/login.dart';
void main(){
runApp(
MaterialApp(
theme: ThemeData(),
darkTheme: ThemeData.dark(),
debugShowCheckedModeBanner: false,
title: "Basic Login Demo",
home: LoginPage(),
),
);
}
login.dart
import 'package:flutter/material.dart';
class LoginPage extends StatefulWidget {
#override
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: ListView(
padding: EdgeInsets.symmetric(horizontal: 24.0),
children: <Widget>[
SizedBox(height: 80.0),
// Column(
// children: <Widget>[
// Image.asset('assets/login_app.png'),
// SizedBox(height: 25.0),
// Text("Material Login"),
// ],
// ),
//*Username starts here
SizedBox(height: 120.0),
TextField(
decoration: InputDecoration(
labelText: 'Username',
filled: true,
),
),
//*Password starts here
SizedBox(height: 12.0),
TextField(
decoration: InputDecoration(
labelText: 'Password',
filled: true,
),
obscureText: true,
),
ButtonBar(
children: <Widget>[
FlatButton(
child: Text('Cancel'),
onPressed: () {
},
),
RaisedButton(
child: Text('Next'),
onPressed: () {
},
)
],
)
],
),
),
);
}
}

You need to use ThemeMode
Describes which theme will be used by MaterialApp.
SAMPLE CODE
themeMode: ThemeMode.dark,//Always use the dark mode (if available) regardless of system preference.
themeMode: ThemeMode.light,//Always use the light mode regardless of system preference.
themeMode: ThemeMode.system,//Use either the light or dark theme based on what the user has selected in the system settings.
themeMode: ThemeMode.values,//A constant List of the values in this enum, in order of their declaration.
How To use ThemeMode in MaterialApp
MaterialApp(
debugShowCheckedModeBanner: false,
theme:
ThemeData(primarySwatch: Colors.blue, brightness: Brightness.light),
themeMode: ThemeMode.dark,
darkTheme: ThemeData(brightness: Brightness.dark),
home: SafeArea(
child:Scaffold(
) ),
);

The recommended method is to use ColorScheme.
var mode = ThemeMode.light; // or ThemeMode.dark
MaterialApp(
theme: ThemeData.from(colorScheme: ColorScheme.light()),
darkTheme: ThemeData.from(colorScheme: ColorScheme.dark()),
themeMode: mode,
home: //...
)

Related

Error with primary swatch in flutter project

i have a red line when i select color ,i think he can't recognize the chosen color
primarySwatch require a MaterialColor.
ThemeData
See Turn any color to Material Color for flutter
Map<int, Color> color = {
50:Color.fromRGBO(136,14,79, .1),
100:Color.fromRGBO(136,14,79, .2),
200:Color.fromRGBO(136,14,79, .3),
300:Color.fromRGBO(136,14,79, .4),
400:Color.fromRGBO(136,14,79, .5),
500:Color.fromRGBO(136,14,79, .6),
600:Color.fromRGBO(136,14,79, .7),
700:Color.fromRGBO(136,14,79, .8),
800:Color.fromRGBO(136,14,79, .9),
900:Color.fromRGBO(136,14,79, 1),
};
MaterialColor colorCustom = MaterialColor(0xFF880E4F, color);
And
theme: ThemeData(
primarySwatch: colorCustom,
bottomAppBarColor: colorCustom
),
Change the default primary theme color of Flutter widget components to any other custom theme color at once by using ThemeData. The default color of the Flutter app is blue color.
MaterialApp(
theme: ThemeData(
primarySwatch: Colors.purple
),
)
Full Example
import 'package:flutter/material.dart';
void main(){
runApp(MyApp());
}
class MyApp extends StatelessWidget{
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.purple
),
home: Home(),
);
}
}
class Home extends StatefulWidget{
#override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Flutter Color Theme"),
),
body: Container(
padding: EdgeInsets.all(20),
alignment: Alignment.center,
child: Column(
children:[
ElevatedButton(
onPressed: (){
},
child: Text("Elevated Button"),
),
ListTile(
leading: Checkbox(
value: true,
onChanged: (value){},
),
title:Text("This is checkbox")
),
ListTile(
leading: Radio(
groupValue: true,
value: true,
onChanged: (value){},
),
title:Text("This is Radio")
)
]
),
)
);
}
}
See here: Flutter Campus

App Brightness overwritten by phones default Brightness in Flutter?

I am new to Flutter, i want to built an app that can control the Screen Brightness using flutter, i found a Plugin in screen 0.0.5 it is working , it can effect the screen brightness.
I am using Screen Plugin you can check it.
But My Problem is that when i hide the Application then the Brightness Set by my App is overwritten by Mobile's default Brightness?
How it is possible to full control the phone brightness?
code
import 'package:flutter/material.dart';
import 'package:screen/screen.dart';
void main() => runApp(MaterialApp(
theme: ThemeData(primarySwatch: Colors.red, brightness: Brightness.dark),
themeMode: ThemeMode.dark,
darkTheme: ThemeData(brightness: Brightness.dark),
debugShowCheckedModeBanner: false,
home: ScreenBrightness()));
//ManageScreenDemoState pageState;
class ScreenBrightness extends StatefulWidget {
#override
_ScreenBrightnessState createState() => _ScreenBrightnessState();
}
class _ScreenBrightnessState extends State<ScreenBrightness> {
#override
double _brightness;
bool _enableKeptOn;
#override
void initState() {
super.initState();
getBrightness();
getIsKeptOnScreen();
}
void getBrightness() async {
double value = await Screen.brightness;
setState(() {
_brightness = double.parse(value.toStringAsFixed(1));
});
}
void getIsKeptOnScreen() async {
bool value = await Screen.isKeptOn;
setState(() {
_enableKeptOn = value;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Screen Brightness")),
body: Column(
children: <Widget>[
// Notice
Container(
margin: const EdgeInsets.all(10),
padding: const EdgeInsets.all(10),
alignment: Alignment(0, 0),
height: 50,
decoration: BoxDecoration(color: Colors.orange),
child: Text(
"Do this example on a Real Phone, not an Emulator.",
style: TextStyle(color: Colors.white),
),
),
// Brightness Settings
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text("Brightness:"),
(_brightness == null)
? CircularProgressIndicator(
backgroundColor: Colors.grey,
)
: Slider(
activeColor: Colors.grey,
value: _brightness,
min: 0,
max: 1.0,
divisions: 20,
onChanged: (newValue) {
setState(() {
_brightness = newValue;
});
// set screen's brightness
Screen.setBrightness(_brightness);
},
),
Text((_brightness * 100).toStringAsFixed(1) + "%"),
],
),
),
// Kept-On Settings
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text("Kept on Screen:"),
Text(_enableKeptOn.toString()),
(_enableKeptOn == null)
? CircularProgressIndicator(
backgroundColor: Colors.blue,
)
: Switch(
activeColor: Colors.grey,
value: _enableKeptOn,
onChanged: (flag) {
Screen.keepOn(flag);
getIsKeptOnScreen();
},
)
],
),
)
],
),
);
}
}
Image
Try it:
MaterialApp(
theme: ThemeData.dark(),
)
Also check :
how to implement dark mode in flutter

How to add an Icon at the beginning of PopupMenuItem in flutter app

I have a PopupMenuButton widget in which I want to add an icon at the beginning of each PopupMenuItem. I've been trying to find a way to do this but I'm not finding any.
Below is the **main.dart** file.
import 'package:flutter/material.dart';
import 'package:practical_0/homepage.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: Homepage(),
);
}
}
Below is the home.dart file. This is where I have implemented the PopupMenuButton. I want the icon to appear at the beginning of PopupMenuItem before the text.
import 'package:flutter/material.dart';
enum WhyFarther { harder, smarter, selfStarter, tradingCharter }
class Homepage extends StatelessWidget {
final double heightFactor = 600/896;
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
return new Card(
new Row(
children: <Widget>[
PopupMenuButton<WhyFarther>(
onSelected: (WhyFarther result) { setState(() { _selection = result; }); },
itemBuilder: (BuildContext context) => <PopupMenuEntry<WhyFarther>>[
const PopupMenuItem<WhyFarther>(
value: WhyFarther.harder,
child: Text('Working a lot harder'),
),
const PopupMenuItem<WhyFarther>(
value: WhyFarther.smarter,
child: Text('Being a lot smarter'),
),
const PopupMenuItem<WhyFarther>(
value: WhyFarther.selfStarter,
child: Text('Being a self-starter'),
),
const PopupMenuItem<WhyFarther>(
value: WhyFarther.tradingCharter,
child: Text('Placed in charge of trading charter'),
),
],
),
],
)
),
),
);
}
}
I would actually suggest using a ListTile like this:
PopupMenuItem<WhyFarther>(
value: WhyFarther.harder,
child: ListTile(
leading: Icon(Icons.work),
title: Text('Working a lot harder'),
),
)
Checkout Flutter Gallery for a live example: https://gallery.flutter.dev/#/demo/menu
You can do it with a Row, like this :
PopupMenuItem<WhyFarther>(
value: WhyFarther.harder,
child: Row(
children: <Widget>[
Icon(Icons.work),
Text('Working a lot harder'),
],
),
),

Flutter : change position of text on the appbar

How can i change the position of my text "Sito Web" on the right of the appbar? I tryid with
alignment: Alignment.TopRight
but he move it the text on the top right corner down the appbar. Here a screen of what i mean https://ibb.co/X28TzNN change the position of "Sito Web" in the position of the red cirle. That's the codes, i tryid with this method too, but can't move in the appbar. Here the file with the appbar background_image_task-9.dart
import 'package:flutter/material.dart';
class BackgroundImage extends StatelessWidget{
final Widget body;
BackgroundImage({this.body});
#override
Widget build(BuildContext context){
return Scaffold(
appBar: AppBar(
elevation: 0,
title: Text('Blumax', style: TextStyle(
fontWeight: FontWeight.w500,
fontFamily: 'DancingScript',
fontSize: 40
),),
centerTitle: false,
),
body: Stack(
children: <Widget>[Container(
decoration: BoxDecoration(
image: DecorationImage(image: AssetImage("assets/blumax.jpg"), fit: BoxFit.cover),
),
),
body
]
)
);
}
}
And here the file with the container hyperlink.dart
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
class Hyperlink extends StatelessWidget {
final String _url;
final String _text;
Hyperlink(this._url, this._text);
_launchURL() async {
if (await canLaunch(_url)) {
await launch(_url);
} else {
throw 'Could not launch $_url';
}
}
#override
Widget build(BuildContext context) {
return InkWell(
child: Container(
alignment: Alignment(0.9, -1.0),
child: Text(
_text,
textAlign: TextAlign.right,
style: TextStyle(
fontFamily: 'RobotoMono',
fontSize: 20,
color: Colors.white,
decoration: TextDecoration.underline),
)),
onTap: _launchURL,
);
}
}
main.dart
import 'package:flutter/material.dart';
import 'background_image_task-9.dart';
import 'hyperlink.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Blumax',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: myColour
),
home: BackgroundImage(
body: Center(
child: Hyperlink('www.test.it', 'Sito Web',),
),
)
);
}
}
You can use Row widget and use mainAxisAlignment: MainAxisAlignment.spaceBetween to render BluMax and Sito Web elements on left and right of the appbar respectively. And since you want to open a url after tapping on sito web, you can wrap that element with GestureDetector and then call launchURL method that opens required url. A working sample code is as below:
appBar: AppBar(
title: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text('Blumax'),
GestureDetector(
child: Text('Sito Web'),
onTap: _openURL
)
],
),
centerTitle: false,
actions: <Widget>[
PopupMenuButton<int>(
itemBuilder: (context) =>
[
PopupMenuItem(
value: 1,
child: Text("First"),
),
PopupMenuItem(
value: 2,
child: Text("Second"),
),
],
)
],
),
This is rendered on screen as:
...
void _openURL() async {
const url = 'https://flutter.dev';
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
I just used a text Blumax for demo, but you may replace it per your need.
Hope this answers your question.

How to override Theme for a part of application?

I have a small Flutter application with Theme configured like this:
class MyApp extends StatelessWidget {
// This widget is the root of your application.
static final list = [
{
'name': 'test',
'password': 'foobar'
}
];
final store = Store(appStateReducers, initialState: AppState(list));
#override
Widget build(BuildContext context) {
return StoreProvider(
store: store,
child: MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.red,
appBarTheme: AppBarTheme(
color: Color.fromRGBO(250, 136, 54, 1)
),
textTheme: TextTheme(
body1: TextStyle(
color: Colors.red // text color is red for the whole applicatioin
)
)
),
initialRoute: '/',
routes: {
'/': (context) => NoAccountPageDart(),
'CreateAccount': (context) => CreateAccount(),
'Home': (context) => Home()
},
),
);
}
}
And on one of my screens I have a list of widgets where I want all text widgets to have another color. So I tried to use Theme widget folowing this guide for that like so:
//some code
child: Theme(
data: Theme.of(context).copyWith(
textTheme: Theme.of(context).textTheme.copyWith(
body1: TextStyle(color: Colors.white) // this is the color I want to use
)
),
child: Column(
children: <Widget>[
Text(accounts[index]['name']), // this is supposed to be white. But it's still red.
Text(accounts[index]['password'],
style: TextStyle(color: Colors.green))
],
),
));
//some code
But it didn't work. I have also tried to follow these answers on stackoverflow, and here how it looked in my code:
child: Theme(
data: Theme.of(context).copyWith(
textTheme: Theme.of(context).textTheme.apply(
bodyColor: Colors.white // this is the color I want to use
)
),
child: Column(
children: <Widget>[
Text(accounts[index]['name']), // this is supposed to be white. But it's still red.
Text(accounts[index]['password'],
style: TextStyle(color: Colors.green))
],
),
));
But this didn't work either. What am I doing wrong?
Yes, you can use Theme widget as parent of your Scaffold in which you want to override global theme of app.
For Ex : Your Global theme is
theme: ThemeData(
primarySwatch: Colors.blue,
buttonColor: Colors.red
),
So, you have to use it with the syntax like,
color:Theme.of(context).buttonColor;
By, Adding Theme widget to specific screen like,
Theme(
data: ThemeData(
buttonColor: Colors.purple
),
child: Scaffold(
appBar: AppBar(
title: Text("Demo"),
),
body: Container(
child: RaisedButton(
onPressed:(){},
child:Text("Save"),
),
),
)
)
For this particular screen your button color gets directly applied from your nearest scaffold ThemeData to the RaisedButton Color. you don't need to reference it using Theme.of(context).
This way you can create a global ThemeData and apply it to all the screens which needs some different theme configurations other than declared in MaterialApp ThemeData.
I hope it helps.

Categories

Resources