Changing the language of my application only works once - flutter

I am making a Flutter application and I am making a settings page where I want to allow the user to choose the language they want. I have already tried many things but each time I have the same problem: I can only change the language once.
Here is my "settings" view:
class SettingViewState extends State<StatefulWidget> {
String _selectedLanguage = 'French';
#override
Widget build(BuildContext context) {
print('SettingViewState - build');
return Scaffold(
drawer: NavDrawableWidget(), // Hamburger menu
body: Column(
children: <Widget>[
Center(
child: Text('\n${AppSettings.strings.settingTitle}\n',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold
),
),
),
Container(
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
side: BorderSide(color: Colors.black12, width: 2),
),
child: ListTile(
title: Text(AppSettings.strings.languageTitle),
leading: Icon(Icons.language),
trailing: Theme(
data: Theme.of(context).copyWith(
canvasColor: Colors.blue[50],
),
child: DropdownButton<String>(
value: _selectedLanguage,
items: [
DropdownMenuItem(
value: 'French',
child: Row(
children: <Widget>[
Image.asset(AppSettings.APP_FRENCH_FLAG,width: 30, height: 20,),
SizedBox(width: 10),
Text(AppSettings.strings.frenchTitle),
],
),
),
DropdownMenuItem(
value: 'English',
child: Row(
children: <Widget>[
Image.asset(AppSettings.APP_ENGLISH_FLAG,width: 30, height: 20,),
SizedBox(width: 10),
Text(AppSettings.strings.englishTitle),
],
),
),
],
onChanged: (String? newValue) {
setState(() {
_selectedLanguage = newValue ?? _selectedLanguage;
AppSettings.changeLanguage(_selectedLanguage);
});
},
),
),
),
),
),
],
),
);
}
}
And here is my method in my config file to change the language:
static void changeLanguage(String newLanguage)
{
APP_LANGUAGE = newLanguage;
}
Thanks in advance if you take the time to answer me and help me

Is Riverpod an option for your state management?
https://riverpod.dev/
I believe you can achieve what you want similar to the following:
state_providers.dart
import 'package:flutter_riverpod/flutter_riverpod.dart';
final selectedLanguageProvider = StateProvider<String>((ref) => 'English');
setting_view.dart
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:yourapp/providers/state_providers.dart';
class SettingViewState extends ConsumerWidget {
const SettingViewState({
super.key,
});
#override
Widget build(BuildContext context, WidgetRef ref) {
final selectedLanguage = ref.watch(selectedLanguageProvider);
return Column(
children: <Widget>[
Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
side: const BorderSide(color: Colors.black12, width: 2),
),
child: ListTile(
title: const Text('Language'),
leading: const Icon(Icons.language),
trailing: Theme(
data: Theme.of(context).copyWith(
canvasColor: Colors.blue[50],
),
child: DropdownButton<String>(
value: selectedLanguage,
items: const [
DropdownMenuItem(
value: 'French',
child: Text('French'),
),
DropdownMenuItem(
value: 'English',
child: Text('English'),
),
],
onChanged: (String? newValue) {
ref.read(selectedLanguageProvider.notifier).state = newValue!;
},
),
),
),
),
],
);
}
}
It would also simplify accessing the state across Widgets as opposed to using a static settings file which I imagine you are doing. For example you can access the language state directly in an app bar widget
common_app_bar.dart
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:yourapp/providers/state_providers.dart';
class CommonAppBar extends ConsumerWidget implements PreferredSizeWidget {
const CommonAppBar({
Key? key,
}) : super(key: key);
#override
Size get preferredSize => const Size.fromHeight(kToolbarHeight);
#override
Widget build(BuildContext context, WidgetRef ref) {
final selectedLanguage = ref.watch(selectedLanguageProvider);
return AppBar(
backgroundColor: Colors.grey,
elevation: 0,
title: Text(selectedLanguage == 'English' ? 'Hello' : 'Bonjour'),
);
}
}

Related

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

Dropdown menu in flutter

I have tried to build a dropdown button and a menu with it, where the value will be selected from the dropdown menu. The code is as below:
String valueChoose;
List listItem = ["A", "B", "C", "D", "E"];
DropdownButton(
hint: Text('Associate'),
dropdownColor: Colors.white,
icon: Icon(Icons.arrow_drop_down),
iconSize: 20.0,
style: TextStyle(
fontSize: 22.0,
color: Colors.black,
),
value: valueChoose,
onChanged: (newValue) {
setState(() {
valueChoose = newValue;
});
},
items: listItem.map((valueItem){
return DropdownMenuItem(
value: valueItem,
child: Text(valueItem),
);
}).toList(),
),
The error I'm facing is in the set state, where I've assigned newValue to the valueChoose.
A value of type 'Object?' can't be assigned to a variable of type 'String'.
Try changing the type of the variable, or casting the right-hand type to 'String'.
That is the error showing up for the newValue assinged in the set state. Please help regarding this, thanks in advance!
Below is the code, including the AlertDailog:
class HomeScreen extends StatefulWidget {
#override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
String ? valueChoose;
List listItem = [
"A", "B", "C", "D", "E"
];
void assignPopup(BuildContext context) {
var alertDialog = AlertDialog(
content:
Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Row(
children:[
Container(
child: Text(
'Action',
),
),
]
),
Row(
children:[
Container(
child: Card(
elevation: 5.0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
child: TextField(
decoration: InputDecoration(
labelText: 'Please add any comments',
),
),
),
),
]
),
Row(
children:[
Container(
child: Text(
'Assign To',
),
),
]
),
Row(
children: [
Container(
child: Card(
elevation: 5.0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
child: DropdownButton<String>(
hint: Text('Associate'),
dropdownColor: Colors.white,
icon: Icon(Icons.arrow_drop_down),
iconSize: 40.0,
style: TextStyle(
fontSize: 18.0,
color: Colors.black,
),
value: valueChoose,
onChanged: (newValue) {
setState(() {
valueChoose = newValue;
});
},
items: listItem.map<DropdownMenuItem<String>>((valueItem){
return DropdownMenuItem(
value: valueItem,
child: Text(valueItem),
);
}).toList(),
),
),
),
],
),
],
),
);
showDialog(
context: context,
builder: (BuildContext context) {
return alertDialog;
}
);
}
#override
Widget build(BuildContext context) {
return Scaffold(
...
...
Container(
child: TextButton(
onPressed: (){
assignPopup(context);
},
child: Text(
'Assign',
),
),
),
);
}
}
From the data that you provided I have created a example where I have used the alertdialog and inside it there is a drop down
import 'package:flutter/material.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,
),
home: HomeScreen(),
);
}
}
class HomeScreen extends StatefulWidget {
#override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
String valueChoose;
List listItem = ["A", "B", "C", "D", "E"];
void assignPopup(BuildContext context) {
showDialog(
context: context,
builder: (BuildContext context) {
return StatefulBuilder(builder: (context, setState) {
return AlertDialog(
content: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Row(children: [
Container(
child: Text(
'Action',
),
),
]),
Row(children: [
Expanded(
child: Container(
child: Card(
elevation: 5.0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
child: TextField(
decoration: InputDecoration(
labelText: 'Please add any comments',
),
),
),
),
),
]),
Row(children: [
Container(
child: Text(
'Assign To',
),
),
]),
Row(
children: [
Container(
child: Card(
elevation: 5.0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
child: DropdownButton<String>(
hint: Text('Associate'),
dropdownColor: Colors.white,
icon: Icon(Icons.arrow_drop_down),
iconSize: 40.0,
style: TextStyle(
fontSize: 18.0,
color: Colors.black,
),
value: valueChoose,
onChanged: (newValue) {
setState(() {
valueChoose = newValue;
});
},
items: listItem
.map<DropdownMenuItem<String>>((valueItem) {
return DropdownMenuItem(
value: valueItem,
child: Text(valueItem),
);
}).toList(),
),
),
),
],
),
],
),
);
});
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
child: TextButton(
onPressed: () {
assignPopup(context);
},
child: Text(
'Assign',
),
),
),
),
);
}
}
So here in order to change dropdown value you have to use the StatefulBuilder which will change your dropdown value. you can check the above example and make changes as per your needs.
Please run the code to check the desired output.
Let me know if it works.
Specify the types of DropdownButton and that map
String? valueChoose;
List listItem = ["A", "B", "C", "D", "E"];
DropdownButton<String>(
hint: Text('Associate'),
dropdownColor: Colors.white,
icon: Icon(Icons.arrow_drop_down),
iconSize: 20.0,
style: TextStyle(
fontSize: 22.0,
color: Colors.black,
),
value: valueChoose,
onChanged: (newValue) {
setState(() {
valueChoose = newValue;
});
},
items: listItem.map<DropdownMenuItem<String>>((valueItem) {
return DropdownMenuItem(
value: valueItem,
child: Text(valueItem),
);
}).toList(),
)
Declare variables outside the build method.
String? valueChoose;
List listItem = ["A", "B", "C", "D", "E"];
Inside the build method add the Dropdown widget, So the complete state class code would look like this,
class _DropdownViewState extends State<DropdownView> {
String? valueChoose;
List listItem = ["A", "B", "C", "D", "E"];
#override
Widget build(BuildContext context) {
return DropdownButton<String>(
hint: Text('Associate'),
dropdownColor: Colors.white,
icon: Icon(Icons.arrow_drop_down),
iconSize: 20.0,
style: TextStyle(
fontSize: 22.0,
color: Colors.black,
),
value: valueChoose,
onChanged: (String? newValue) {
setState(() {
valueChoose = newValue;
});
},
items: listItem.map<DropdownMenuItem<String>>((valueItem){
return DropdownMenuItem(
value: valueItem,
child: Text(valueItem),
);
}).toList(),
);
}

Is there any way to put custom toolbar on the keypad?

I want to put a custom toolbar on the keypad like the image above. Is it possible in flutter? or should I write code on the iOS or Android side?
You can copy paste run full code below
Please see working demo below
You can use package https://pub.dev/packages/keyboard_overlay
Step 1: Use with HandleFocusNodesOverlayMixin
Step 2: Use FocusNodeOverlay for focusNode
Step 3: Use GetFocusNodeOverlay and set _focusNodeOverlay = GetFocusNodeOverlay(
Step 4: TextField use TextField(focusNode: _focusNodeOverlay,
code snippet
class _MyHomePageState extends State<MyHomePage>
with HandleFocusNodesOverlayMixin {
FocusNodeOverlay _focusNodeOverlay;
#override
void initState() {
_focusNodeOverlay = GetFocusNodeOverlay(
child: TopKeyboardUtil(
Container(
color: Colors.white,
height: 45,
width: MediaQueryData.fromWindow(ui.window).size.width,
child: Row(
children: [
GestureDetector(
child: Icon(Icons.save),
onTap: () => print("click"),
),
...
Spacer(),
Container(
width: 60,
child: Center(
child: DoneButtonIos(
backgroundColor: Colors.white,
textColor: Colors.green,
label: 'Post',
onSubmitted: () {
print("submit");
},
platforms: ['android', 'ios'],
),
),
),
],
),
),
),
);
working demo
full code
import 'package:flutter/material.dart';
import 'package:keyboard_overlay/keyboard_overlay.dart';
import 'dart:ui' as ui;
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>
with HandleFocusNodesOverlayMixin {
FocusNodeOverlay _focusNodeOverlay;
#override
void initState() {
_focusNodeOverlay = GetFocusNodeOverlay(
child: TopKeyboardUtil(
Container(
color: Colors.white,
height: 45,
width: MediaQueryData.fromWindow(ui.window).size.width,
child: Row(
children: [
GestureDetector(
child: Icon(Icons.save),
onTap: () => print("click"),
),
GestureDetector(
child: Icon(Icons.computer),
onTap: () => print("click"),
),
GestureDetector(
child: Icon(Icons.home),
onTap: () => print("click"),
),
Spacer(),
Container(
width: 60,
child: Center(
child: DoneButtonIos(
backgroundColor: Colors.white,
textColor: Colors.green,
label: 'Post',
onSubmitted: () {
print("submit");
},
platforms: ['android', 'ios'],
),
),
),
],
),
),
),
);
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextField(
focusNode: _focusNodeOverlay,
style: TextStyle(color: Colors.grey),
decoration: InputDecoration(
labelText: 'Type Something',
labelStyle: TextStyle(color: Colors.black),
fillColor: Colors.orange,
hintStyle: TextStyle(
color: Colors.grey,
),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.black, width: 1.0),
),
),
),
],
),
),
);
}
}
Yes there is a way around in the flutter to achieve this.
Create a widget of the toolbar you want to add.
Set it visible on input focus.
For reference I am sharing the code how I achieve that.
class InputDoneView extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
color: Style.lightGrey,
child: Align(
alignment: Alignment.topRight,
child: Padding(
padding: const EdgeInsets.only(top: 1.0, bottom: 1.0),
child: CupertinoButton(
padding: EdgeInsets.only(right: 24.0, top: 2.0, bottom: 2.0),
onPressed: () {
FocusScope.of(context).requestFocus(new FocusNode());
},
child: Text(
"Done",
style: TextStyle(color: Style.primaryColor,fontWeight: FontWeight.normal)
),
),
),
),
);
}
}
To call this in your main view when input field is focused in and out.
showOverlay(BuildContext context) {
if (overlayEntry != null) return;
OverlayState overlayState = Overlay.of(context);
overlayEntry = OverlayEntry(builder: (context) {
return Positioned(
bottom: MediaQuery.of(context).viewInsets.bottom, right: 0.0, left: 0.0, child: InputDoneView());
});
overlayState.insert(overlayEntry);
}
removeOverlay() {
if (overlayEntry != null) {
overlayEntry.remove();
overlayEntry = null;
}
}

Show widget when button is clicked (Dart, Flutter)

how can I show a widget (for example some more buttons) when a button is clicked.
FlatButton(
onPressed: () {
//render the new widgets
},
child: Icon(
Icons.close,
color: Colors.white,
),
));
This is the parent class
class StopStream extends StatelessWidget
You can conditionally show / hide a widget(s) with a help of a variable.
You need a StatefulWidget to change the state of a widget i.e to dynamically show / hide (widgets).
Please see the following code, I use a bool showWidget to show or hide more FlatButton's in a Row :
import 'package:flutter/material.dart';
final Color darkBlue = const 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: MyWidget(),
),
),
);
}
}
class MyWidget extends StatefulWidget {
#override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
bool showWidget = false;
#override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
showWidget
? Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
FlatButton(
onPressed: () {},
child: const Icon(Icons.ac_unit),
),
FlatButton(
onPressed: () {},
child: const Icon(Icons.accessible),
),
FlatButton(
onPressed: () {},
child: const Icon(Icons.backpack),
),
FlatButton(
onPressed: () {},
child: const Icon(Icons.cached),
),
],
)
: Container(),
FlatButton(
onPressed: () {
setState(() {
showWidget = !showWidget;
});
},
child: const Icon(
Icons.close,
color: Colors.white,
),
),
],
);
}
}
Have a state variable (either in State or in something like Provider or Riverpod) that will be toggled by your onPressed: callback. Then allow that variable to control whether or not the widget in question is shown or omitted from the widget tree.
You can use a bool variable Forexample in my case isSwitched and set its value to false. When the user clicks on the button, set its value to true and use conditional operator to show more widgets as follows:
class CoverScreen extends StatefulWidget {
#override
_CoverScreenState createState() => _CoverScreenState();
}
class _CoverScreenState extends State<CoverScreen> {
bool isSwitched = false;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
toolbarHeight: 70,
title: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Container(
child: Text('WELCOME'),
),
],
)),
body: Padding(
padding: const EdgeInsets.all(10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(children: <Widget>[
Container(
padding: const EdgeInsets.all(15),
child: Text(
'COVER LETTER:',
style: TextStyle(
color: Colors.teal,
fontSize: 25,
fontWeight: FontWeight.w500),
)),
Container(
child: Switch(
value: isSwitched,
onChanged: (value) {
setState(() {
isSwitched = value;
print(isSwitched);
});
},
activeTrackColor: Colors.teal,
activeColor: Colors.white,
)),
]),
Column(children: <Widget>[
isSwitched
? Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.all(10),
child: Text(
'NAME:',
style: TextStyle(
color: Colors.teal,
fontSize: 20,
fontWeight: FontWeight.w500),
)),
Container(
child: TextField(
decoration: new InputDecoration(
contentPadding:
const EdgeInsets.all(20.0),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.teal, width: 2.0),
),
hintText: 'Enter NAME'),
),
),
])
: Container(),
])
])),
);
}
}
The output for the above code is as follows:
Before button click
After Button click

How to append Slider under the text within button when i press the raised Button?

Immediately after my app start it will show a button.
As soon as this button is pressed I want to build a slider within the same button to control the volume for this sound.
All I need is to make this slider appear and not the mechanics to control the volume.
what i want to acheive is here..
my button code
void main() => runApp(Home());
class Home extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: "RelaX",
home: Container(
child: Scaffold(
appBar: AppBar(
elevation: 20.0,
backgroundColor: Color(0xFF001E3D),
title: Text(
'Relax',
style: GoogleFonts.raleway(
fontSize: 30.0,
color: Color(0xFF55b9f3),
),
),
centerTitle: true,
),
body: Column(
children: <Widget>[
RaisedButton(
child: Text("View Slider"),
onPressed: () => print("view slider")),
],
),
),
),
);
}
}
You may use the Visibility widget to set the visibility of the slider. Please see the code below. I'm using Container along with Inkwell to achieve the same effect as RaisedButton.
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
home: MyApp(),
));
}
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
double _currentSliderValue = 0;
bool _sliderVisible = false;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Raised Button"),
),
body: Center(
child: ClipRRect(
borderRadius: const BorderRadius.all(
Radius.circular(20),
),
child: Container(
decoration: BoxDecoration(
color: Colors.blue[200],
borderRadius: const BorderRadius.all(
Radius.circular(20),
),
),
child: Material(
elevation: 2,
child: InkWell(
onTap: () {
setState(() {
_sliderVisible = !_sliderVisible;
});
},
child: Container(
width: 125.0,
height: 125.0,
child: Column(
children: [
const SizedBox(
height: 15,
),
Icon(
Icons.nightlight_round,
color: Colors.indigo[900],
size: 48,
),
const SizedBox(
height: 5,
),
Visibility(
visible: _sliderVisible,
child: Slider(
value: _currentSliderValue,
min: 0,
max: 10,
onChanged: (double value) {
setState(() {
_currentSliderValue = value;
});
},
activeColor: Colors.indigo[900],
inactiveColor: Colors.indigo[900],
),
)
],
),
),
),
color: Colors.transparent,
),
),
),
),
);
}
}