Wholde Widget Rebuild after using Textediting controller

104 views
Skip to first unread message

Dareboy Dareboy

unread,
May 21, 2020, 5:34:42 AM5/21/20
to Flutter Development (flutter-dev)

i have attached file

after selecting dropdown value, and then select date from datepicker, then when i once again select dropdown button, it will not work.

Is something i missed ?

main.dart

Souvik Dutta

unread,
May 21, 2020, 6:47:01 AM5/21/20
to Dareboy Dareboy, Flutter Development (flutter-dev)
Can you copy and paste the code from main.dart instead of attaching it?

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/e7047ec1-d2df-4b84-97ec-e7556f93200f%40googlegroups.com.
Message has been deleted

Dareboy Dareboy

unread,
May 22, 2020, 5:49:19 AM5/22/20
to Flutter Development (flutter-dev)
import 'package:dropdowonsample/contacts.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

void main() {
  runApp
(
   
MultiProvider(
      providers
: [
       
ChangeNotifierProvider.value(
          value
: ContactService(),
       
),
     
],
      child
: 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,
     
),
      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> {
 
List<Contacts> finalList = [];
 
final _controllerStartDate  = TextEditingController();

 
@override
 
void dispose() {
    finalList
.clear();
 
}

 
Widget _createDropDownButton(List<Contacts> _cont, BuildContext context) {
   
return Consumer<ContactService>(builder: (ctx, d, ch) {
     
return DropdownButton(
        key
: GlobalKey(),
        value
: d.getSelectedValue(), // _selectedValue,
        items
: _cont
           
.map(
             
(e) => DropdownMenuItem(
                value
: e.id,
                child
: Text(e.name),
                onTap
: () {},
             
),
           
)
           
.toList(),
        onChanged
: (value) {
         
Provider.of<ContactService>(context, listen: false)
             
.setIsSelected(value);
       
},
     
);
   
});
 
}

 
@override
 
Widget build(BuildContext context) {
   
return Scaffold(
      appBar
: AppBar(
        title
: Text(widget.title),
     
),
      body
: Center(
        child
: Column(
          mainAxisAlignment
: MainAxisAlignment.center,
          children
: <Widget>[
           
Text(
             
'You have pushed the button this many times:',
           
),
           
Consumer<ContactService>(
              builder
: (ctx, d, ch) {
               
return Text(
                  d
.getSelectedValue() == null ? "hello" : d.getSelectedValue(),
                  style
: Theme.of(context).textTheme.headline4,
               
);
             
},
           
),
           
FutureBuilder(
              future
: Provider.of<ContactService>(context, listen: false)
                 
.getPlans(),
              builder
: (context, snapshot) {
               
if (snapshot.connectionState == ConnectionState.done) {
                 
return _createDropDownButton(snapshot.data,
                      context
); // Text(snapshot.data.length.toString());
               
} else {
                 
return Text("Waiting");
               
}
             
},
           
),
           
TextFormField(
              key
: GlobalKey(),
              controller
: _controllerStartDate,
              decoration
: InputDecoration(
                labelText
: "Enter Start Date :",
             
),
              onTap
: () async {
               
DateTime date = DateTime(1900);
               
FocusScope.of(context).requestFocus(new FocusNode());
                date
= await showDatePicker(
                    context
: context,
                    initialDate
: DateTime.now(),
                    firstDate
: DateTime(1900),
                    lastDate
: DateTime(2100));
                _controllerStartDate
.text = date.toIso8601String();
             
},
           
),
         
],
       
),
     
),
   
);
 
}
}

Souvik Dutta

unread,
May 22, 2020, 6:15:01 AM5/22/20
to Dareboy Dareboy, Flutter Development (flutter-dev)
Insert your text editing controller into the dispose method.  Just add this inside your controller.
_controllerStartDate.dispose();
super.dispose();
And see this solves your problem.

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.

Souvik Dutta

unread,
May 22, 2020, 7:12:33 AM5/22/20
to Dareboy Dareboy, Flutter Development (flutter-dev)
Okay sorry don't add inside controller but add those lines inside dispose method

Suzuki Tomohiro

unread,
May 22, 2020, 8:00:47 AM5/22/20
to Flutter Development (flutter-dev)
> it will not work

Can you create a short video in Youtube to explain what is unexpected behavior to you?

On Fri, May 22, 2020 at 5:46 AM Dareboy Dareboy <dareb...@gmail.com> wrote:
Here is the Code
--------------------------------------
Tejas Kishanwala


On Thursday, 21 May 2020 16:17:01 UTC+5:30, Souvik Dutta wrote:
Can you copy and paste the code from main.dart instead of attaching it?

On Thu, 21 May, 2020, 3:04 pm Dareboy Dareboy, <dareb...@gmail.com> wrote:

i have attached file

after selecting dropdown value, and then select date from datepicker, then when i once again select dropdown button, it will not work.

Is something i missed ?

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutt...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.

Dareboy Dareboy

unread,
May 23, 2020, 4:19:10 AM5/23/20
to Flutter Development (flutter-dev)
please find link for the youtube video


Tejas Kishanwala

Suzuki Tomohiro

unread,
May 23, 2020, 6:54:57 AM5/23/20
to Dareboy Dareboy, Flutter Development (flutter-dev)
Would you try making this future as a field of the state?

Provider.of<ContactService>(context,listen: false)
                  
.getPlans()

To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/c5e23d85-f09c-4a0c-a22b-0b5d405f0ed7%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages