Flutter theme not being applied to checkbox - flutter

I have a checkbox that is not accepting the theme data shown below:
final acceptTerms = Checkbox(
value: _acceptIsChecked,
tristate: false,
onChanged: (bool? acceptIsChecked) {
setState(() {
_acceptIsChecked = acceptIsChecked!;
});
},
);
My theme for the checkbox inside MaterialApp:
theme: ThemeData(
primarySwatch: createMaterialColor(Color(0xFFFF0000)),
backgroundColor: Colors.black,
fontFamily: 'Arial',
textTheme: const TextTheme(
headline1: TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold),
headline2: TextStyle(fontSize: 15.0, fontStyle: FontStyle.italic),
),
primaryColor: Colors.white,
checkboxTheme: CheckboxThemeData(
checkColor: MaterialStateProperty.all(Colors.white),
fillColor: MaterialStateProperty.all(Colors.orange),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(45)
)
)
),
I'm expecting to see the checkbox fill color to be orange and the radius to be round vs square but it's defaulting to a blue fill color and staying square.
I am unable to figure out why. I can add the properties directly to the checkbox widget but I'd rather use a theme but currently it seems that isn't possible?
Full code from the register.dart file
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class RegisterPage extends StatefulWidget {
RegisterPage({Key, key, this.title}) : super(key: key);
final String? title;
#override
_RegisterPageState createState() => _RegisterPageState();
}
class _RegisterPageState extends State<RegisterPage> {
TextStyle style =
const TextStyle(fontSize: 20.0);
final _scaffoldKey = GlobalKey<ScaffoldState>();
bool _waiting = false;
final _singleton = Singleton();
bool _acceptIsChecked = false;
#override
void initState() {
super.initState();
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
}
#override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
void showInSnackBar(BuildContext context, String value) {
var sb = SnackBar(content: Text(value));
_scaffoldKey.currentState!.showSnackBar(sb);
}
Widget okButton = MaterialButton(
color: Theme.of(context).primaryColor,
child: const Text("OK", style: TextStyle(color: Colors.white)),
onPressed: () {
Navigator.of(context).pushReplacementNamed('/login');
},
);
List<Widget> _buildPage(BuildContext context) {
AlertDialog accountCreatedAlert = AlertDialog(
title: const Text("Success"),
backgroundColor: Theme.of(context).backgroundColor,
contentTextStyle: TextStyle(color: Theme.of(context).primaryColor),
content: const Text("Account Created! Please login."),
actions: [
okButton,
],
);
final acceptTerms = Checkbox(
value: _acceptIsChecked,
activeColor: Colors.white,
checkColor: Color(0xFF4C4184),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15),
),
tristate: false,
onChanged: (bool? acceptIsChecked) {
setState(() {
_acceptIsChecked = acceptIsChecked!;
});
},
);
var page = SingleChildScrollView(
child: Center(
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Image.asset(
"assets/logo.svg",
width: 400,
),
Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Transform.scale(
scale: 2.0,
child: Theme(
data: ThemeData(unselectedWidgetColor: Colors.grey),
child: acceptTerms
),
),
const SizedBox(width: 10),
const Text("Accept Terms of Service?",
style: TextStyle(color: Colors.white))
],
),
],
),
),
),
);
var l = List<Widget>.empty(growable: true);
l.add(page);
if (_waiting) {
var modal = Stack(
children: const [
Opacity(
opacity: 0.3,
child: ModalBarrier(dismissible: false, color: Colors.grey),
),
],
);
l.add(modal);
}
return l;
}
return Scaffold(
resizeToAvoidBottomInset : false,
body: Container(
width: double.infinity,
height: double.infinity,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/background.png"),
fit: BoxFit.cover,
),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Stack(children: _buildPage(context))
]
)
),
);
}
}

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(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.green,
backgroundColor: Colors.black,
fontFamily: 'Arial',
textTheme: const TextTheme(
headline1: TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold),
headline2: TextStyle(fontSize: 15.0, fontStyle: FontStyle.italic),
),
primaryColor: Colors.white,
checkboxTheme: CheckboxThemeData(
checkColor: MaterialStateProperty.all(Colors.white),
fillColor: MaterialStateProperty.all(Colors.orange),
shape:
RoundedRectangleBorder(borderRadius: BorderRadius.circular(45)),
),
),
home: const ShowCheckBox(),
);
}
}
class ShowCheckBox extends StatefulWidget {
const ShowCheckBox({Key? key}) : super(key: key);
#override
State<ShowCheckBox> createState() => _ShowCheckBoxState();
}
class _ShowCheckBoxState extends State<ShowCheckBox> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Transform.scale(
scale: 3,
child: Checkbox(
onChanged: (_) {},
value: true,
splashRadius: 50,
tristate: false,
),
),
),
);
}
}
The same code is working for me -
Upgrade your flutter sdk.
Open terminal and run the following command:
flutter upgrade
Image

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(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.green,
backgroundColor: Colors.black,
fontFamily: 'Arial',
textTheme: const TextTheme(
headline1: TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold),
headline2: TextStyle(fontSize: 15.0, fontStyle: FontStyle.italic),
),
primaryColor: Colors.white,
checkboxTheme: CheckboxThemeData(
checkColor: MaterialStateProperty.all(Colors.white),
fillColor: MaterialStateProperty.all(Colors.orange),
shape:
RoundedRectangleBorder(borderRadius: BorderRadius.circular(45)),
),
),
home: const ShowCheckBox(),
);
}
}
class ShowCheckBox extends StatefulWidget {
const ShowCheckBox({Key? key}) : super(key: key);
#override
State<ShowCheckBox> createState() => _ShowCheckBoxState();
}
class _ShowCheckBoxState extends State<ShowCheckBox> {
final Widget acceptTerms = Checkbox(
onChanged: (_) {},
value: true,
splashRadius: 50,
tristate: false,
);
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Transform.scale(
scale: 3,
child: acceptTerms,
),
),
);
}
}
This code is almost similar like your code, then to its working properly.Do one thing copy my code, paste it in your machine and then run it, that's only the solution I guess.
Output

Related

How to implement colours dropdown in flutter?

enter image description here
how to implement this dropdown in flutter?(please refer the image above)
when the dropdown is expanded it should show a list of colours in a circular container.
try this way
import 'package:flutter/material.dart';
const Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MainApp());
}
class MainApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(
scaffoldBackgroundColor: darkBlue,
),
debugShowCheckedModeBanner: false,
home: const Scaffold(
body: Center(
child: MyApp(),
),
),
);
}
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
#override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
Color pickColor = Colors.red;
List colorsList = [
Colors.red,
Colors.yellow,
Colors.black,
Colors.pink,
Colors.blue
];
#override
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 10),
decoration: BoxDecoration(
color: Colors.white, borderRadius: BorderRadius.circular(8)),
child: DropdownButton(
style: const TextStyle(color: Colors.black),
//Dropdown font color
dropdownColor: Colors.white,
//dropdown menu background color
//dropdown indicator icon
underline: Container(),
//make underline empty
value: pickColor,
onChanged: (value) {
pickColor = value! as Color;
setState(() {});
},
items: colorsList.map((itemone) {
return DropdownMenuItem(
value: itemone,
child: Row(
children: [
const Text(
'Color: ',
style: TextStyle(
color: Colors.black, fontWeight: FontWeight.w700),
),
Container(
height: 20,
width: 20,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: itemone))
],
));
}).toList(),
),
);
}
}

Tabbar Customisation in Flutter

I have studied about creating Tabbar in Flutter but as so far I know We can create Tabbar inside AppBar widget which is inside Scaffold widget.
But I want something like below :
So far I have as below:
I have created Tab screens but have to manage tab selections as above.
As you can see above, Appbar is different and tabbar is outside Appbar,
How can I manage this to customise this Tabs in Flutter? Thanks.
try this
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: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
const MyHomePage({
Key? key,
required this.title,
}) : super(key: key);
#override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
#override
Widget build(BuildContext context) {
return DefaultTabController(
length: 3,
child: Scaffold(
appBar: AppBar(
centerTitle: false,
title: Text(
"Facilities",
style: TextStyle(color: Colors.black, fontSize: 20),
),
actions: <Widget>[
Padding(
padding: EdgeInsets.only(right: 20.0),
child: GestureDetector(
onTap: () {},
child: Icon(
Icons.search,
size: 26.0,
),
)),
],
backgroundColor: Colors.white,
elevation: 0,
bottom: TabBar(
unselectedLabelColor: Colors.redAccent,
indicatorSize: TabBarIndicatorSize.tab,
indicator: BoxDecoration(
gradient: LinearGradient(
colors: [Colors.redAccent, Colors.orangeAccent]),
borderRadius: BorderRadius.circular(6),
color: Colors.redAccent),
tabs: [
Tab(
child: Align(
alignment: Alignment.center,
child: Text("Attraction"),
),
),
Tab(
child: Align(
alignment: Alignment.center,
child: Text("Events"),
),
),
Tab(
child: Align(
alignment: Alignment.center,
child: Text("Utils"),
),
),
]),
),
body: TabBarView(children: [
Icon(Icons.apps),
Icon(Icons.movie),
Icon(Icons.games),
]),
));
}
}

Trying to create a method to control font size. Flutter App

I'm trying to create a font size control, the idea is that the user can change the font size of the entire app through the Slider, drag this bar and adjust it like 14px, 16px, 18px, 20px... minimum and maximum. I also read that the best way to make the changes on several screens will be using the provider, what is your opinion on this choice?
This is the starting code.
class Settings extends StatefulWidget {
const Settings({Key? key}) : super(key: key);
#override
State<Settings> createState() => _SettingsState();
}
class _SettingsState extends State<Settings> {
double _rating = 20;
#override
void initState() {
SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersive);
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
elevation: 0,
backgroundColor: Colors.transparent,
iconTheme: IconThemeData(color: Colors.blue[900]),
title: const Text(
'Settings',
style: TextStyle(
color: Colors.black,
),
),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ListTile(
title: Text('Button'),
trailing: Icon(
Icons.arrow_forward_ios,
color: Colors.blue,
),
onTap: () {
showModalBottomSheet<void>(
context: context,
builder: (BuildContext context) {
return Container(
height: 200,
color: Colors.white,
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'Change font',
style: TextStyle(
),
),
),
Slider(
value: _rating,
min: 0,
max: 28,
divisions: 4,
label: _rating.round().toString(),
onChanged: (newRating) {
setState(() => _rating = newRating);
},
),
],
),
),
);
}
);
},
),
],
),
);
}
}
I have created a provider example it might help you
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() {
runApp(MultiProvider(providers: [
ChangeNotifierProvider(create: (_) => SliderValue()),
], child: MyApp()));
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: SizableText(),
);
}
}
class SliderValue with ChangeNotifier {
double _value = 5;
double get value => _value;
void increment(double val) {
_value = val;
notifyListeners();
}
}
class SizableText extends StatefulWidget {
const SizableText({Key? key}) : super(key: key);
#override
State<SizableText> createState() => _SizableTextState();
}
class _SizableTextState extends State<SizableText> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("AppBar")),
body: Center(
child: Column(
children: <Widget>[
Container(
margin: EdgeInsets.only(right: 10),
decoration: new BoxDecoration(
color: Colors.blue,
borderRadius: new BorderRadius.all(new Radius.circular(5.0)),
boxShadow: [
new BoxShadow(
color: Colors.black38,
offset: new Offset(0.0, 2.0),
blurRadius: 10)
]),
child: new Slider(
value: context.watch<SliderValue>().value,
activeColor: Colors.white,
inactiveColor: Colors.white,
onChanged: (double s) {
context.read<SliderValue>().increment(s);
},
divisions: 10,
min: 0.0,
max: 10.0,
),
),
Text1(text: 'Hello'),
Text1(text: 'Hi'),
],
),
),
);
}
}
class Text1 extends StatelessWidget {
Text1({this.text});
final String? text;
#override
Widget build(BuildContext context) {
return Text(text ?? '',
style: TextStyle(fontSize: 10 * context.watch<SliderValue>().value));
}
}
Basic idea is stored fonsize value in somewhere that Text can reach, state management will update the value of fonsize and notify to theres subscription. Im not using provider so im use an other state management is get to do this.
// ignore_for_file: prefer_const_constructors_in_immutables
import 'package:flutter/material.dart';
import 'package:get/get.dart';
class HomeController extends GetxController {
var fontSizeObx = RxDouble(12);
setFontsize(double value) => fontSizeObx.value = value;
}
class HomeRoute extends StatelessWidget {
HomeRoute({Key? key}) : super(key: key);
final controller = Get.put(HomeController());
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Obx(
() => Column(
children: [
Text(
'Hello world',
style: TextStyle(fontSize: controller.fontSizeObx.value),
),
Slider(
value: controller.fontSizeObx.value,
onChanged: controller.setFontsize,
divisions: 10,
min: 10.0,
max: 100.0,
)
],
),
),
),
);
}
}
You can try this
double _value = 5;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("AppBar")),
body: Center(
child: Column(
children: <Widget>[
Container(
margin: EdgeInsets.only(right: 10),
decoration: new BoxDecoration(
color: Colors.blue,
borderRadius: new BorderRadius.all(new Radius.circular(5.0)),
boxShadow: [new BoxShadow(color: Colors.black38,
offset: new Offset(0.0, 2.0), blurRadius: 10)]),
child: new Slider(
value: _value,
activeColor: Colors.white,
inactiveColor: Colors.white,
onChanged: (double s) {
setState(() {
_value = s;
});
},
divisions: 10,
min: 0.0,
max: 10.0,
),
),
Text("Hello World", style: TextStyle(fontSize: 10 * _value)),
],
),
),
);
}

How to customize Dropdown Button and items in flutter?

Today I tried to design a dropdown button with customized items in it where I can select all items or deselect all items with one click. But I didn't understand the approach how to do it. So please help me guys how to approach the required design and below I placed my design and required design.
and here is my code
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(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String dropdownvalue = 'Apple';
var items = ['Apple','Banana','Grapes','Orange','watermelon','Pineapple'];
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("DropDownList Example"),
),
body: Container(
padding: EdgeInsets.all(10.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text("DropDownButton"),
Container(
height: 40,
padding: EdgeInsets.all(5.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30.0),
border: Border.all(
color: Colors.grey, style: BorderStyle.solid, width: 0.80),
),
child: DropdownButtonHideUnderline(
child: DropdownButton(
elevation: 0,
value: dropdownvalue,
icon: Icon(Icons.keyboard_arrow_down),
items:items.map((String items) {
return DropdownMenuItem(
value: items,
child: Text(items)
);
}
).toList(),
onChanged: (String? newValue){
setState(() {
dropdownvalue = newValue!;
});
},
),
),
),
],
),
],
),
),
);
}
}
You can use dropdown_button2 package for this:
import 'package:flutter/material.dart';
import 'package:dropdown_button2/dropdown_button2.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: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String dropdownvalue = 'Apple';
var items = [
'Apple',
'Banana',
'Grapes',
'Orange',
'watermelon',
'Pineapple'
];
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("DropDownList Example"),
),
body: Container(
padding: EdgeInsets.all(10.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text("DropDownButton"),
Container(
height: 40,
padding: EdgeInsets.all(5.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30.0),
border: Border.all(color: Colors.grey, style: BorderStyle.solid, width: 0.80),
),
child: DropdownButtonHideUnderline(
child: DropdownButton2(
hint: Text(
'Select Item',
style: TextStyle(
fontSize: 14,
color: Theme.of(context).hintColor,
),
),
items: items
.map((item) => DropdownMenuItem<String>(
value: item,
child: Text(
item,
style: const TextStyle(
fontSize: 14,
),
),
))
.toList(),
value: dropdownvalue,
onChanged: (String? newValue) {
setState(() {
dropdownvalue = newValue!;
});
},
buttonHeight: 40,
buttonWidth: 140,
itemHeight: 40,
),
)),
],
),
],
),
),
);
}
}
final result:

How do I change the color of raisedButton onPressed?

I'm trying to change the color of the raisedButton when pressed, I have been having trouble with this for a while, the code I have right now for the whole button widget is
children: <Widget>[
Expanded(
child: Container(
child: new SizedBox(
width: 15,
height: 60,
child: new RaisedButton(
color: pressed ? Color(0xFF1D1E33) : Colors.blue,
hoverColor: Colors.blueAccent,
focusColor: Colors.blueAccent,
child: Text(
'Male',
style: TextStyle(
fontSize: 26,
fontWeight: FontWeight.bold,
color: Colors.white),
textAlign: TextAlign.center,
),
onPressed: () => setState((){pressed = !pressed;})),
),
I also had a boolean as shown below but I received an error when running the code saying "Failed assertion: boolean expression must not be null"
bool get pressed => null;
set pressed(bool pressed) {}
You can copy paste run full code below
code snippet
bool _pressed = false;
bool get pressed {
return _pressed;
}
set pressed(bool pressed) {
_pressed = pressed;
}
working demo
full code
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(
primarySwatch: Colors.blue,
),
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> {
int _counter = 0;
bool _pressed = false;
bool get pressed {
return _pressed;
}
set pressed(bool pressed) {
_pressed = pressed;
}
void _incrementCounter() {
setState(() {
_counter++;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
child: new SizedBox(
width: 100,
height: 60,
child: new RaisedButton(
color: pressed ? Color(0xFF1D1E33) : Colors.blue,
hoverColor: Colors.blueAccent,
focusColor: Colors.blueAccent,
child: Text(
'Male',
style: TextStyle(
fontSize: 26,
fontWeight: FontWeight.bold,
color: Colors.white),
textAlign: TextAlign.center,
),
onPressed: () => setState(() {
pressed = !pressed;
})))),
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}