Flutter Slider (activeTrackHeight and inactiveTrackHeight) - android

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

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

The instance member 'setState' can't be accessed in an initializer

im Very new in flutter . i dont know what to do to fix this .
im trying to Use Flutter Plugin :
flutter_numpad_widget
Here my Full code:
import 'package:flutter/material.dart';
import 'package:flutter_numpad_widget/flutter_numpad_widget.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => _MyAppState();
}
bool _confirmEnabled = false;
class _MyAppState extends State<MyApp> {
int maxRawLength;
final NumpadController _numpadController = NumpadController(
format: NumpadFormat.NONE,
hintText: "Ketikkan NIP",
onInputValidChange: (bool valid) => setState(() {
_confirmEnabled = valid;
}),
);
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Numpad Example',
theme: ThemeData(
primarySwatch: Colors.amber,
buttonTheme: ButtonThemeData(
textTheme: ButtonTextTheme.normal,
buttonColor: Colors.blueGrey[300],
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(30))))),
home: Scaffold(
appBar: AppBar(
title: Text('Numpad Example'),
),
body: Container(
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(16.0),
child: NumpadText(
style: TextStyle(fontSize: 30),
controller: _numpadController,
),
),
Expanded(
child: Numpad(
controller: _numpadController,
buttonTextSize: 40,
),
)
],
),
),
));
}
}
im following the documentation here :
onInputValidChange
but in this line its keep getting me Error "The instance member 'setState' can't be accessed in an initializer.":
onInputValidChange: (bool valid) => setState(() {
_confirmEnabled = valid;
}),
im Already searching in few days and gets nothing.
thanks for your help priciateit
To add some explanation to your problem and I think in general is also valid:
You should init all your state properties in initState. If you have like bool flags or primitive properties that's fine but objects, in general, you should init in ```initState````. In your case:
import 'package:flutter/material.dart';
import 'package:flutter_numpad_widget/flutter_numpad_widget.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
#override
_MyAppState createState() => _MyAppState();
}
bool _confirmEnabled = false;
class _MyAppState extends State<MyApp> {
int maxRawLength;
final NumpadController _numpadController; // this is the declaration
#override
void initState() {
super.initState();
_numpadController = NumpadController( // here is the init
format: NumpadFormat.NONE,
hintText: "Ketikkan NIP",
onInputValidChange: (bool valid) => setState(() {
_confirmEnabled = valid;
}),
);
}
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Numpad Example',
theme: ThemeData(
primarySwatch: Colors.amber,
buttonTheme: ButtonThemeData(
textTheme: ButtonTextTheme.normal,
buttonColor: Colors.blueGrey[300],
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(30))))),
home: Scaffold(
appBar: AppBar(
title: Text('Numpad Example'),
),
body: Container(
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(16.0),
child: NumpadText(
style: TextStyle(fontSize: 30),
controller: _numpadController,
),
),
Expanded(
child: Numpad(
controller: _numpadController,
buttonTextSize: 40,
),
)
],
),
),
));
}
}
You should declare your state inside The state widget like this:
class _MyAppState extends State<MyApp> {
int maxRawLength;
bool _confirmEnabled = false; // here
....
onInputValidChange: (bool valid) => setState(() {
_confirmEnabled = valid;
}),
...

Black-Screen with the FlatButton in the AppBar

My App contains basically 2 parts -> Appbar (with 1 Button) and BottomNavigationBar (with some buttons that works properly). The problem came when I pressed the Appbar button (goes to a black screen instead of show the "manual_page.dart")
this is the content of the 2 files (the home_page.dart and manual_page.dart):
home_page.dart
import 'package:flutter/material.dart';
import 'package:opening_a_pdf/manual_page.dart';
import 'package:opening_a_pdf/first_page.dart';
import 'package:opening_a_pdf/second_page.dart';
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 _selectedPage = 0;
List<Widget> pageList = List<Widget>();
#override
void initState() {
pageList.add(FirstPage());
pageList.add(SecondPage());
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color(0xFFFAFAFA),
appBar: AppBar(
backgroundColor: Colors.black,
title: const Text('Aplicación en Desarrollo'),
actions: <Widget>[
FlatButton(
textColor: Colors.white,
child: Text(
'MANUAL',
style: TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold,
),
),
onPressed: (){
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Voice()),
);
}
)
],
),
body: IndexedStack(
index: _selectedPage,
children: pageList,
),
bottomNavigationBar: BottomNavigationBar(
// type: BottomNavigationBarType.fixed,
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
backgroundColor: Colors.black,
icon: Icon(Icons.compare_arrows),
title: Text('Conectividad'),
),
BottomNavigationBarItem(
backgroundColor: Colors.black,
icon: Icon(Icons.blur_on),
title: Text('Captura Datos'),
),
BottomNavigationBarItem(
backgroundColor: Colors.black,
icon: Icon(Icons.graphic_eq),
title: Text('Voz'),
),
BottomNavigationBarItem(
backgroundColor: Colors.black,
icon: Icon(Icons.list),
title: Text('Comandos'),
),
BottomNavigationBarItem(
backgroundColor: Colors.black,
icon: Icon(Icons.settings),
title: Text('Ajustes'),
),
],
currentIndex: _selectedPage,
selectedItemColor: Colors.amber[800],
onTap: _onItemTapped,
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
void _onItemTapped(int index) {
setState(() {
_selectedPage = index;
});
}
}
manual_page.dart
import 'package:flutter/material.dart';
// ignore: camel_case_types
class Voice extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Sección de Órdenes por Voz"),
),
body: Stack(
fit: StackFit.expand,
children: <Widget>[
Positioned(
bottom: 0,
width: MediaQuery.of(context).size.width,
child: Center(
child: MaterialButton(
onPressed: () {},
color: Colors.red,
),
),
)
],
),
);
}
}
Try to initial the height of container in the second screen before Stack
There are no errors in the code. Works correctly. Maybe the fault is in the main () or in the emulator.
Code in main:
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
I executed your code and found no problem with it:
But you can put empty Container() as the child of MaterialButton().
Corrected code:
MaterialButton(
onPressed: () {},
color: Colors.red,
child:Container(),
),

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

Pager indicator flutter

I am using page_indicator: ^0.1.3 plugin to show an indicator for the page view.
PageIndicatorContainer(
pageView: PageView.builder(
controller: PageController(),
itemBuilder: (context, position) {
);
},
scrollDirection: Axis.horizontal,
itemCount: widgetView.widgetItemList.length,
),
align: IndicatorAlign.bottom,
length: widgetView.widgetItemList.length,
indicatorColor: Colors.white,
indicatorSelectorColor: Colors.grey,
size: 5.0,
indicatorSpace: 10.0,
))
but it shows the indicator over the image like.
I did not find any option or plugin to set the indicator below image
like
PageIndicatorContainer doesn't seem to have a property where you could set the indicator outside PageView. A workaround for this case is to add padding on the pages of your PageView.
Complete sample
import 'package:flutter/material.dart';
import 'package:page_indicator/page_indicator.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> {
PageController controller;
GlobalKey<PageContainerState> key = GlobalKey();
#override
void initState() {
super.initState();
controller = PageController();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Column(
children: [
Flexible(
flex: 5,
child: PageIndicatorContainer(
key: key,
child: PageView.builder(itemCount: 4,itemBuilder: (context, position) {
return Container(
// Padding will help prevent overlap on the page indicator
padding: EdgeInsets.fromLTRB(5, 5, 5, 30),
child: Container(
color: Colors.greenAccent,
child: Center(child: Text('${position + 1}')),
),
);
}),
align: IndicatorAlign.bottom,
length: 4,
indicatorSpace: 20.0,
padding: const EdgeInsets.all(10),
indicatorColor: Colors.grey,
indicatorSelectorColor: Colors.blue,
shape: IndicatorShape.circle(size: 12),
),
),
Flexible(flex: 1, child: Container()),
Flexible(
flex: 5,
child: Container(
color: Colors.cyan,
))
],
),
);
}
}

Categories

Resources