Error with primary swatch in flutter project - android

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

Related

Flutter Slider (activeTrackHeight and inactiveTrackHeight)

Why activeTrackHeight and inactiveTrackHeight length are different ?
I hope they are the same length.
How do I fix my code ?
please help.
I use Android studio 4.0
and Flutter 1.22.3
I tried some custom slider packages and searched for solutions in search engines, but couldn't find one.
sorry my poor English.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
sliderTheme: SliderThemeData(
trackHeight: 14,
thumbColor: Colors.white,
thumbShape: RoundSliderThumbShape(enabledThumbRadius: 7),
valueIndicatorColor: Colors.orange,
overlayColor: Colors.transparent,
activeTrackColor: Colors.black,
activeTickMarkColor: Colors.red,
inactiveTrackColor: Colors.black,
inactiveTickMarkColor: Colors.grey,
),
),
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> {
double val=1;
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'activeTrackHeight and inactiveTrackHeight',
),
Container(
width: 140,
height: 240,
child: Transform.scale(
scale: 4.0,
child: Slider(
value: val,
min: 0,
max: 2,
divisions: 2,
onChanged: (double value) {
setState (() {
val = value;
});
},
),
),
),
],
),
),
);
}
}
Add trackShape: RectangularSliderTrackShape() to your SliderThemeData:
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
sliderTheme: SliderThemeData(
trackShape: RectangularSliderTrackShape(),
trackHeight: 14,
thumbColor: Colors.white,
thumbShape: RoundSliderThumbShape(enabledThumbRadius: 7),
valueIndicatorColor: Colors.orange,
overlayColor: Colors.transparent,
activeTrackColor: Colors.black,
activeTickMarkColor: Colors.red,
inactiveTrackColor: Colors.black,
inactiveTickMarkColor: Colors.grey,
),
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

Flutter How to define a single style for MaterialButton widget everywhere in App?

How to define a single style or theme for MaterialButton, FlatButton, RaisedButton widgets everywhere in App.
A style may contain text color, size, font type, corner of circle shape.
child: MaterialButton(
color: Colors.red,
onPressed: () { },
child: Text(
'Proceed',
style: TextStyle(color: Colors.white),
),
You have to create custom widgets for each. you call it that way.
for example flatButton:
class ButtonFlatWidget extends StatelessWidget {
const ButtonFlatWidget(
{Key key, #required this.title, #required this.onPress, this.titleColor})
: super(key: key);
final String title;
final Colors titleColor;
final Function onPress;
#override
Widget build(BuildContext context) {
return FlatButton(
child: Text(
title,
style: TextStyle(color: titleColor ?? Colors.blueAccent, fontSize: 16),
),
onPressed: onPress,
);
}
}
then you use the ButtonFlatWidget widget wherever you want.
but I just want to add text color and style. if you say You have to write different methods for each and call them. this is a tough event.
import 'package:flutter/material.dart';
import 'package:syncfusion_flutter_charts/charts.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
buttonTheme: ButtonThemeData(
height:60
buttonColor: Colors.blueAccent,
shape: RoundedRectangleBorder(),
textTheme: ButtonTextTheme.accent,
....
),
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
ButtonThemeData.buttonColor: Same as ThemeData.
ButtonThemeData.splashColor: Same as ThemeData used in FlatButton, OutlineButton or RaisedButton.
ButtonThemeData.highlightColor: Same as ThemeData.
ButtonThemeData.hoverColor: Same as ThemeData.
ButtonThemeData.minWidth: Default minimum width.
ButtonThemeData.height: Default height.
ButtonThemeData. padding: Default padding.
ButtonThemeData.shape: Default shape.
are some of the common options..

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'),
],
),
),

Creating flutter expandable with toggle button

I got some difficulties implementing UI design in flutter. I wanna make an expanded widget like this, can I have a clue?
collapsed:
expanded
I have tried to use Expandable but I just can't get it looks like my design. Thanks
You can use AnimatedContainer.
For example :
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool expanded = false;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Example')),
body: Column(
children: [
Container(color: Colors.green, height: 100),
AnimatedContainer(
duration: const Duration(milliseconds: 200),
height: expanded ? 120 : 0,
child: Container(height: 120, color: Colors.red),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
FloatingActionButton(
child:
Icon(expanded ? Icons.arrow_upward : Icons.arrow_downward),
onPressed: () => setState(() {
expanded = !expanded;
}),
),
],
),
],
),
);
}
}

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

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: //...
)

Categories

Resources