BottomNavigationBar covered by body colour in Flutter - flutter

I am using the convex_bottom_bar as my bottom navigation bar in Flutter.
With Style: TabStyle.react if I set a colour of the container widget in the body it covers the convex ‘curve’ of the active tab of the navigation bar. If no colour is set the curve can be seen fine.
main.dart
import 'package:convex_bottom_bar/convex_bottom_bar.dart';
import 'package:flutter/material.dart';
import 'custom.dart';
import 'game_start.dart';
import 'news.dart';
import 'settings.dart';
import 'share.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: MainPage(title: 'Flutter Convex BottomBar Sample'),
);
}
}
class MainPage extends StatefulWidget {
MainPage({Key? key, required this.title}) : super(key: key);
final String title;
#override
_MainPageState createState() => _MainPageState();
}
class _MainPageState extends State<MainPage> {
int selectedIndex = 2;
List<Widget> listWidgets = [
News(),
Custom(),
GameStart(),
Share(),
Settings()
];
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: listWidgets[selectedIndex],
),
bottomNavigationBar: ConvexAppBar(
style: TabStyle.react,
items: [
TabItem(icon: Icons.star, title: 'News'),
TabItem(icon: Icons.fact_check, title: 'Custom'),
TabItem(icon: Icons.play_arrow, title: 'Play'),
TabItem(icon: Icons.share, title: 'Share'),
TabItem(icon: Icons.settings, title: 'Settings'),
],
onTap: onItemTapped,
color: Color(0xFFffc400),
activeColor: Color(0xFFffc400),
backgroundColor: Colors.black87,
height: 50,
initialActiveIndex: 2,
),
);
}
void onItemTapped(int index) {
setState(() {
selectedIndex = index;
});
}
}
game_start.dart
import 'package:flutter/material.dart';
class GameStart extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
color: Color(0xFF000000),
child: Center(
child: Text(
"Start Game",
style: TextStyle(
fontWeight: FontWeight.bold, fontSize: 22, color: Colors.green),
),
),
);
}
}
news.dart
import 'package:flutter/material.dart';
class News extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
color: Color(0xFF000000),
child: Center(
child: Text(
"News",
style: TextStyle(
fontWeight: FontWeight.bold, fontSize: 22, color: Colors.green),
),
),
);
}
}

The issue is with the plugin. The body container applies to the convex part. You can overcome this issue by stacking the body and convexappbar.
class _MainPageState extends State<MainPage> {
int selectedIndex = 2;
List<Widget> listWidgets = [
Container(color: Colors.black,),
Container(color: Colors.red),
Container(color: Colors.yellow),
Container(color: Colors.amber,),
Container(color: Colors.white,)
];
#override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
alignment: Alignment.bottomCenter,
children: [
Container(
child: listWidgets[selectedIndex],
),
ConvexAppBar(
style: TabStyle.react,
items: [
TabItem(icon: Icons.star, title: 'News'),
TabItem(icon: Icons.fact_check, title: 'Custom'),
TabItem(icon: Icons.play_arrow, title: 'Play'),
TabItem(icon: Icons.share, title: 'Share'),
TabItem(icon: Icons.settings, title: 'Settings'),
],
onTap: onItemTapped,
color: Color(0xFFffc400),
activeColor: Color(0xFFffc400),
backgroundColor: Colors.black87,
height: 50,
initialActiveIndex: 2,
elevation: 5
)
],
),
);
}
void onItemTapped(int index) {
setState(() {
selectedIndex = index;
});
}
}
The is no visual difference between the bottom navbar and body.
black87 doesn't mean it is grey. It is black with an opacity of 87% and the alpha fills based on the body colour. If possible try to switch the colour for the body and bottom navbar.

The issue is both GameStart'sContainer color and backgroundColor ofConvexAppBar are black, That's why it makes no difference in our eyes.
If you change the color of the container, you will be alble see the curve,
class GameStart extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Center(
child: Container(
color: Color.fromARGB(255, 17, 255, 49),
child: Center(
child: Text(
"Start Game",
style: TextStyle(
fontWeight: FontWeight.bold, fontSize: 22, color: Colors.green),
),
),
),
);
}
}

Related

Flutter theme not being applied to checkbox

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

Pass Navigator function (any callback) to a StatelessWidget to use it in onPressed

I am lacking experience in Flutter to understand how to pass a callback funciton into a Widget:
Custom button :
class IconButtonWidget extends StatelessWidget {
final Future<void> callback;
const IconButtonWidget({Key? key, required this.callback}) : super(key: key);
#override
Widget build(BuildContext context) {
return TextButton.icon(
icon: const Icon(
Icons.add_alarm,
size: 30,
),
label: const Text(
'New alarm',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
letterSpacing: 1.0),
),
onPressed: () async {
await callback;
},
);
}
}
I have a simple Home Screen where I use it (see commented out line):
class _HomeState extends State<Home> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: const <Widget>[
Center(
child: IconButtonWidget(
// callback: () {Navigator.pushNamed(context, '/alarms');},
),
),
SizedBox(
height: 20.0,
),
],
)),
);
}
}
I want to pass a function to the Widget, but don't understand how i can do that.
Previously it was used like this (before I separated the widget in a separate class)
child: TextButton.icon(
icon: const Icon(
Icons.add_alarm,
size: 30,
// color: Colors.teal,
),
label: const Text(
'New alarm',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
letterSpacing: 1.0),
),
onPressed: () async {
dynamic result =
await Navigator.pushNamed(context, '/alarms');
},
),
),
Please refer to below code
import 'package:flutter/material.dart';
const Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(
scaffoldBackgroundColor: darkBlue,
),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: IconButtonWidget(callback: () async {
print("Call back Function");
}),
),
),
);
}
}
class IconButtonWidget extends StatelessWidget {
final Function callback;
const IconButtonWidget({Key? key, required this.callback}) : super(key: key);
#override
Widget build(BuildContext context) {
return TextButton.icon(
icon: const Icon(
Icons.add_alarm,
size: 30,
),
label: const Text(
'New alarm',
style: TextStyle(
fontWeight: FontWeight.bold, fontSize: 20, letterSpacing: 1.0),
),
onPressed: () {
callback();
},
);
}
}
class IconButtonWidget extends StatelessWidget {
final VoidCallback onPressed;
const IconButtonWidget({Key? key, required this.onPressed}) : super(key: key);
#override
Widget build(BuildContext context) {
return TextButton.icon(
icon: const Icon(
Icons.add_alarm,
size: 30,
),
label: const Text(
'New alarm',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
letterSpacing: 1.0),
),
onPressed: onPressed,
);
}
}
.........
Center(
child: IconButtonWidget(
onPressed: () async {
dynamic result =
await Navigator.pushNamed(context, '/alarms');
},
),
),
.........

Why is the Text widget 's line height changes from English to Chinese?

With this code
Center(
child: Text(
'hello 你好',
style: TextStyle(backgroundColor: Colors.red, fontSize: 24),
),
)
It seems the Chinese will got a larger height than English ? Can i make them have a same height ?
The different text height is caused by different font family.
Would you change font that supports Chinese and English?
https://fonts.google.com/?subset=chinese-simplified&preview.text=%E4%BD%A0%E5%A5%BDHello&preview.text_type=custom
Or if you want to make a text height same, below is the example.
Using 'strutStyle' parameter in Text, althouhg it is not convenient, you can modifying each text height is same.
https://medium.com/#najeira/control-text-height-using-strutstyle-4b9b5151668b
https://github.com/flutter/flutter/issues/38875
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,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
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
void initState() {
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: _buildBody(),
floatingActionButton: FloatingActionButton(
onPressed: () {},
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
Widget _buildBody() {
return Column(
children: [
Center(
child: Text(
'hello 你好',
style: TextStyle(backgroundColor: Colors.red, fontSize: 24),
),
),
SizedBox(height: 20),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
child: Text(
"hello",
style: TextStyle(
fontSize: 24,
),
strutStyle: StrutStyle(
height: 1.5,
fontSize: 24,
),
),
color: Colors.red,
),
Container(
child: Text(
"你好",
style: TextStyle(
fontSize: 24,
),
strutStyle: StrutStyle(
height: 1.5,
fontSize: 24,
),
),
color: Colors.red,
),
],
),
],
);
}
}

Flutter Drawer when menu icon is tapped

I am currently trying out drawer widget. I have constructed an appbar with a hamburger menu icon, title text and a search icon.
Here is my code:
`
import 'package:flutter/material.dart';
import 'package:hexcolor/hexcolor.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
#override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
#override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: PreferredSize(
child: Container(
color: Colors.white,
padding:
EdgeInsets.only(left: 10, right: 10, top: 10, bottom: 10),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Icon(Icons.menu),
Text(
'Appbar',
style: TextStyle(fontWeight: FontWeight.bold),
),
Icon(Icons.search)
],
),
),
preferredSize: Size.fromHeight(40)),
backgroundColor: Hexcolor('#e9f1fe'),
body: Center(
child: Text('Experimenting drawer'),
)));
}
}
`
Here is the output :,
Also I have a separate dart file for a custom drawer I want to be displayed when the hamburger menu icon is tapped. How can I make that possible?
Here is the code for custom drawer:
import 'package:flutter/material.dart';
class DrawerScreen extends StatefulWidget {
#override
_DrawerScreenState createState() => _DrawerScreenState();
}
class _DrawerScreenState extends State<DrawerScreen> {
#override
Widget build(BuildContext context) {
return Scaffold(
drawer: Drawer(
child: Column(
children: [
ListTile(
leading: CircleAvatar(
radius: 28,
backgroundColor: Colors.blueGrey,
child: CircleAvatar(
radius: 50,
backgroundImage: AssetImage('assets/images/joker.jpg'),
),
),
title: Text(
'Joaquin Phoenix',
style: TextStyle(
color: Colors.black,
fontFamily: 'Roboto',
fontWeight: FontWeight.bold,
fontSize: 14,
),
),
subtitle: Text(
"You wouldn't get it",
style: TextStyle(
color: Colors.black,
fontFamily: 'Roboto',
fontWeight: FontWeight.w400,
fontSize: 10.0),
),
)
],
)),
backgroundColor: Colors.blue[50],
);
}
}
You can copy paste run two full code below
Step 1: _DrawerScreenState remove Scaffold
Step 2: For drawer background color, you can wrap with Theme
class _DrawerScreenState extends State<DrawerScreen> {
#override
Widget build(BuildContext context) {
return Theme(
data: Theme.of(context).copyWith(
canvasColor: Colors.blue[50],
),
child: Drawer(
Step 3: Use DrawerScreen() in main.dart
SafeArea(
child: Scaffold(
drawer: DrawerScreen(),
Step 4: wrap Icons.menu with Builder and open with openDrawer()
Builder(
builder: (context) => GestureDetector(
onTap: () {
Scaffold.of(context).openDrawer();
},
child: Icon(Icons.menu)),
),
working demo
full code main.dart
import 'package:flutter/material.dart';
import 'package:hexcolor/hexcolor.dart';
import 'drawer.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
#override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
#override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
drawer: DrawerScreen(),
appBar: PreferredSize(
child: Container(
color: Colors.white,
padding:
EdgeInsets.only(left: 10, right: 10, top: 10, bottom: 10),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Builder(
builder: (context) => GestureDetector(
onTap: () {
Scaffold.of(context).openDrawer();
},
child: Icon(Icons.menu)),
),
Text(
'Appbar',
style: TextStyle(fontWeight: FontWeight.bold),
),
Icon(Icons.search)
],
),
),
preferredSize: Size.fromHeight(40)),
backgroundColor: Hexcolor('#e9f1fe'),
body: Center(
child: Text('Experimenting drawer'),
)));
}
}
full code drawer.dart
import 'package:flutter/material.dart';
class DrawerScreen extends StatefulWidget {
#override
_DrawerScreenState createState() => _DrawerScreenState();
}
class _DrawerScreenState extends State<DrawerScreen> {
#override
Widget build(BuildContext context) {
return Theme(
data: Theme.of(context).copyWith(
canvasColor: Colors.blue[50],
),
child: Drawer(
child: Column(
children: [
ListTile(
leading: CircleAvatar(
radius: 28,
backgroundColor: Colors.blueGrey,
child: CircleAvatar(
radius: 50,
backgroundImage: AssetImage('assets/images/joker.jpg'),
),
),
title: Text(
'Joaquin Phoenix',
style: TextStyle(
color: Colors.black,
fontFamily: 'Roboto',
fontWeight: FontWeight.bold,
fontSize: 14,
),
),
subtitle: Text(
"You wouldn't get it",
style: TextStyle(
color: Colors.black,
fontFamily: 'Roboto',
fontWeight: FontWeight.w400,
fontSize: 10.0),
),
)
],
)),
);
}
}

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),
),
);
}
}